import DaDianScript from "../../../../FrameWork/Base/DaDianScript";
import WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
import { ryw_Event } from "../../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../../FrameWork/Event/EventMgr";
import Common from "../../../../FrameWork/Util/Common";
import Common5 from "../../../../Platform/th/Common5";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
import CccGame from "../../../WordGame/gameComScript/CccGame/CccGame";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";

const { ccclass, property } = cc._decorator;

@ccclass
export default class YanJiZhiDaoScript extends WordGameBaseComponent {
    @property(cc.Node)
    layerNode: cc.Node[] = [];

    @property(cc.Node)
    operateNodeList: cc.Node[] = [];

    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule: number = 0;
    completedQuantity: number = 0;

    //文本配置
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chadianArrayConfig: any[] = [];
    wrongAnswerConfig: string[] = [];
    answersIndexConfig: number[] = [];

    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        //this.openTouchEvent(this.node.getChildByName('bg'));
        Common5.stopMusic();

        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle, 'script/YanJiZhiDaoConfig', (assest) => {
            this.jsonData = assest.json;
            this.initParameters();
            this.initComponent();
        })
    }

    //初始化参数
    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;
        this.completedQuantity = 0;

        this.titleArrayConfig = this.jsonData.titleArray;
        this.duihuaArrayConfig = this.jsonData.duihuaArray;
        this.chadianArrayConfig = this.jsonData.chadianArray;
        this.wrongAnswerConfig = this.jsonData.wrongAnswer;
        this.answersIndexConfig = this.jsonData.answersIndex;

        Common.Type = 3;
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.answersArray;

        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
            this.endGameView(0);
        }, this);
    }

    //初始化组件
    initComponent() {
        for (const node of this.layerNode) {
            node.active = false;
        }

        Game.ins.setGameTitle(this.titleArrayConfig[0]);
        //Common5.playMusicCustom(Common5.selectGameInfo.bundle, 'sound/背景音');

        this.refreshLayer();
    }

    refreshLayer() {
        let node = this.layerNode[this.curLayerIndex];
        node.active = true;

        this.node.getChildByName('inputBlock').active = true;
        this.showDuiHua(this.duihuaArrayConfig[12], () => {
            this.node.getChildByName('inputBlock').active = false;
        });
    }

    //触摸回调
    normalTouchCallback(targetNode: cc.Node) {
        const targetNameList = ['点击姚安娜2', '墨镜', '西装衣服', '丝袜', '地上裤子', '鸡爪', '梳子', '胶带', '水枪'];
        const determine = (target: cc.Node) => {
            const index = targetNameList.indexOf(target.name);
            this.unlockLevel(index);
            this.curSchedule++;
            console.log("+++++++++++++++++++++++++>" + this.curSchedule);
            if (this.curSchedule >= targetNameList.length) {
                this.scheduleOnce(() => {
                    this.endGameView(1);
                }, 2);
            } else {
                this.node.getChildByName("inputBlock").active = false;
                CccGame.playAnimation2(this.getLayerChild('评委'), '锤头', true);
            }
        }
        const itemLogic = {
            点击姚安娜: (target: cc.Node) => {
                this.showDuiHua(this.duihuaArrayConfig[0]);
            },
            点击向左: (target: cc.Node) => {
                this.showDuiHua(this.duihuaArrayConfig[2]);
            },
            点击朱梓骁: (target: cc.Node) => {
                this.showDuiHua(this.duihuaArrayConfig[1]);
            },
            点击姚安娜2: (target: cc.Node) => {
                this.node.getChildByName("inputBlock").active = true;
                CccGame.playAnimation2(this.getLayerChild('评委'), '大拇指', true);
                this.showDuiHua(this.duihuaArrayConfig[3], () => {
                    determine(target);
                });
            },
            西装衣服: (target: cc.Node) => {
                this.node.getChildByName("inputBlock").active = true;
                this.getLayerChild('chadianNodeList', '点击向左').active = false;
                CccGame.playAnimation2(this.getLayerChild('评委'), '大拇指', true);
                this.showDuiHua(this.duihuaArrayConfig[8], () => {
                    determine(target);
                });
            },
            丝袜: (target: cc.Node) => {
                this.node.getChildByName("inputBlock").active = true;
                this.getLayerChild('chadianNodeList', '点击向左').active = false;
                CccGame.playAnimation2(this.getLayerChild('评委'), '大拇指', true);
                this.showDuiHua(this.duihuaArrayConfig[9], () => {
                    determine(target);
                });
            },
            地上裤子: (target: cc.Node) => {
                this.node.getChildByName("inputBlock").active = true;
                this.getLayerChild('chadianNodeList', '点击姚安娜').active = false;
                CccGame.playAnimation2(this.getLayerChild('评委'), '大拇指', true);
                this.showDuiHua(this.duihuaArrayConfig[4], () => {
                    determine(target);
                });
            },
            墨镜: (target: cc.Node) => {
                this.node.getChildByName("inputBlock").active = true;
                this.getLayerChild('chadianNodeList', '点击姚安娜').active = false;
                CccGame.playAnimation2(this.getLayerChild('评委'), '大拇指', true);
                this.showDuiHua(this.duihuaArrayConfig[5], () => {
                    determine(target);
                });
            },
            鸡爪: (target: cc.Node) => {
                this.node.getChildByName("inputBlock").active = true;
                this.getLayerChild('chadianNodeList', '点击朱梓骁').active = false;
                this['鸡爪'] = true;
                CccGame.playAnimation2(this.getLayerChild('评委'), '大拇指', true);
                CccGame.playAnimation2(this.getLayerChild('朱梓骁'), '开心', true);
                this.showDuiHua(this.duihuaArrayConfig[6], () => {
                    determine(target);
                });
            },
            梳子: (target: cc.Node) => {
                this.node.getChildByName("inputBlock").active = true;
                this.getLayerChild('chadianNodeList', '点击朱梓骁').active = false;
                CccGame.playAnimation2(this.getLayerChild('梳子animation'), '梳子');
                this.scheduleOnce(() => {
                    this.getLayerChild('梳子animation').active = false;
                    if (this['鸡爪']) {
                        CccGame.playAnimation2(this.getLayerChild('朱梓骁'), '开心', true, '散发');
                    } else {
                        CccGame.playAnimation2(this.getLayerChild('朱梓骁'), '丧气', true, '散发');
                    }
                    CccGame.playAnimation2(this.getLayerChild('评委'), '大拇指', true);
                    this.showDuiHua(this.duihuaArrayConfig[7], () => {
                        determine(target);
                    });
                }, 1);
            },
            胶带: (target: cc.Node) => {
                this.node.getChildByName("inputBlock").active = true;
                CccGame.playAnimation2(this.getLayerChild('评委'), '大拇指', true);
                this['胶带'] = true;
                if (this['水枪']) {
                    CccGame.playAnimation2(this.getLayerChild('纪凌尘'), '水枪有胶带', true);
                } else {
                    CccGame.playAnimation2(this.getLayerChild('纪凌尘'), '没水枪有胶带', true);
                }
                this.showDuiHua(this.duihuaArrayConfig[10], () => {
                    determine(target);
                });
            },
            水枪: (target: cc.Node) => {
                this.node.getChildByName("inputBlock").active = true;
                CccGame.playAnimation2(this.getLayerChild('评委'), '大拇指', true);
                this['水枪'] = true;
                if (this['胶带']) {
                    CccGame.playAnimation2(this.getLayerChild('纪凌尘'), '水枪有胶带', true);
                } else {
                    CccGame.playAnimation2(this.getLayerChild('纪凌尘'), '水枪没胶带', true);
                }
                this.showDuiHua(this.duihuaArrayConfig[11], () => {
                    determine(target);
                });
            },
        }

        console.log("+++++++++++++++++++++>" + targetNode.name);
        itemLogic[targetNode.name]?.(targetNode);
    }

    //显示对话
    showDuiHua(curLog, func?) {
        console.log("curLog==", curLog);

        if (curLog.effectUrl) {
            Common5.playEffectCustom(Common5.selectGameInfo.bundle, curLog.effectUrl);
        }

        let string_ = curLog.str;
        let qiPaoPos_ = curLog.qiPaoPos;
        let delayTime_ = curLog.delayTime || 3;

        let node = this.layerNode[this.curLayerIndex];
        if (qiPaoPos_ != -1) {
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions();
            qiPao.getChildByName("str").getComponent(cc.Label).string = string_;
            cc.tween(qiPao)
                .set({ active: true, scale: 0 })
                .to(0.2, { scale: 1 })
                .delay(delayTime_)
                .set({ active: false })
                .start();
        }
        else {
            let logPrefab = this.node.getChildByName('logPrefab');
            let script_: logPrefabScript = logPrefab.getComponent('logPrefabScript');
            script_.setDailogShow(string_, delayTime_);
        }

        this.scheduleOnce(() => {
            if (func) {
                func();
            }
        }, delayTime_);
    }

    //获取当前场景下的节点
    getLayerChild(...path: string[]) {
        let child = this.layerNode[this.curLayerIndex];
        if (path.length > 1) {
            for (const name of path) {
                child = child.getChildByName(name);
            }
            return child;
        }
        return child.getChildByName(path[0]);
    }

    //跳转场景
    nextLayer() {
        this.curLayerIndex++;
        for (let i = 0; i < this.layerNode.length; i++) {
            if (this.layerNode[i].active) {
                cc.tween(this.layerNode[i])
                    .delay(1.0)
                    .to(1, { opacity: 0 })
                    .call(() => {
                        this.layerNode[i].active = false;
                        this.refreshLayer();
                    })
                    .start();
            } else {
                this.layerNode[i].active = false;
            }
        };
    }

    //结束弹窗
    endGameView(touchIndex) {
        this.node.getChildByName("inputBlock").active = true;
        Game.ins.stopTime();
        if (touchIndex == 0) {
            Game.ins.showFail();
        } else {
            Game.ins.showSuccess();
        }
    }

    //解锁提示
    unlockLevel(sublevel) {
        Game.ins.tipUnlock(sublevel)
    }

    //update(dt) { }
}