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 XiYouWeiJi extends WordGameBaseComponent {
    @property(cc.Node)
    layerNode: cc.Node[] = [];

    @property(cc.Node)
    checkNodeList: cc.Node = null;

    @property(cc.Node)
    props: cc.Node = null;

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

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

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

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

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

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

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

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

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

        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);

    }

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

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

        for (const iterator of this.props.children) {
            let callbacks = CccGame.onNodeTouchMoreCheckEvent(iterator, this.checkNodeList.children);
            callbacks.setSuccessListener((data_) => {
                this.nodeCheckSuccessCallback(data_.targetNode);
            });
        }
        // this.node.getChildByName("inputBlock").active = true;
        // this.showDuiHua(this.duihuaArrayConfig[0], () => {
        //     this.node.getChildByName("inputBlock").active = false;
        // });
    }

    nodeCheckSuccessCallback(targetNode: any) {
        console.log("[GameReport]++++++++++++++++++++++>" + targetNode.checkNode.name);

        let index = this.checkNodeList.children.indexOf(targetNode.checkNode);
        if (this.hideNodeList[index]) {
            this.hideNodeList[index].active = false;
        }
        if (this.showNodeList[index]) {
            this.showNodeList[index].active = true;
        }
        this.unlockLevel(this.answersIndexConfig[index]);

        let log = this.chadianArrayConfig[index].log;
        if (log && log[0]) {
            this.showDuiHua(log[0], () => {
                if (log[1]) {
                    this.showDuiHua(log[1], () => {
                        this.determine();
                    });
                } else {
                    this.determine();
                }
                if (index == 8) {
                    CccGame.playAnimation(this.operateNodeList[0], '打丧尸');
                    this.scheduleOnce(() => {
                        if (!this['wukong']) {
                            CccGame.playAnimation(this.operateNodeList[1], '丧尸悟空倒地');
                        }
                        CccGame.playAnimation(this.operateNodeList[2], '丧尸2倒地');
                        CccGame.playAnimation(this.operateNodeList[3], '丧尸3倒地');
                        this.scheduleOnce(() => {
                            this.operateNodeList[1].active = false;
                            this.operateNodeList[2].active = false;
                            this.operateNodeList[3].active = false;
                            if (this['wukong']) {
                                CccGame.playAnimation(this.operateNodeList[0], '八戒跑步金耙', true);
                                cc.tween(this.operateNodeList[4])
                                    .set({ active: true, position: cc.v3(378, 10) })
                                    .to(1, { position: cc.v3(-330, -300) })
                                    .start();
                            } else {
                                this.endGameView(0);
                            }
                        }, 1);
                    }, 1);
                }
            });
        }
    }

    //触摸回调
    normalTouchCallback(targetNode: cc.Node) {
        console.log("+++++++++++++++++++++>" + targetNode.name);
        switch (targetNode.name) {
            case '丧尸悟空':
                this['wukong'] = true;
                this.unlockLevel(7);
                break;
            case '解药':
                this.unlockLevel(10);
                break;
            default:
                break;
        }
    }

    //判定
    determine() {
        this.curSchedule++;
        console.log("+++++++++++++++++++++>" + this.curSchedule);
        if (this.curSchedule == this.chadianArrayConfig.length) {
            this.endGameView(1);
        };
    }


    //显示对话
    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_);
    }

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

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

    openTouchEvent(node) {
        node.on(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.on(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    closeTouchEvent(node: cc.Node) {
        node.off(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.off(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    touchStartNode(event) {
        let target = event.target;
        console.log('touchStartNode');
    }

    touchMoveNode(event) {

    }

    touchEndNode(event) {
        let target = event.target;
        let logPrefab = this.node.getChildByName('logPrefab');
        logPrefab.getChildByName('duihua').active = false;
    }

    onClick(event) {
        let target = event.target as cc.Node;
        console.log("+++++++++++++++++++++>" + target.name);
        Common5.playEffect("点击音效");
        switch (target.name) {
            case "BtnAddTimes":
            default:
                break;
        }
    }

    //update(dt) { }
}