import Game from "../../../Scripts/Game";
// import ZuZhuangMoTuoCheConfig from "./ZuZhuangMoTuoCheConfig";
import WordGameBaseComponent from "../../../FrameWork/Base/WordGameBaseComptent";
import DaDianScript from "../../../FrameWork/Base/DaDianScript";
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 ItemIconBList from "../../../WordGame/gameComScript/ItemIconList/ItemIconBList";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ZuZhuangMoTuoCheScript extends WordGameBaseComponent {

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

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

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

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

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

    //当前层级下标
    curLayerIndex = 0;

    //当前进度
    curSchedule: Set<number> = null;
    endIndex: number = 0;

    //文本配置
    duihuaArrayConfig: any[] = [];
    unitIconListConfig: any = null;


    //计时
    Timer = 0;

    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        this.openTouchEvent(this.node.getChildByName('bg'));
        Common5.stopMusic()
        // this.initParameters();
        // this.initComponent();
    
        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/ZuZhuangMoTuoCheConfig',(assest)=>{
            //this.jsonData = JSON.stringify(assest.json)

            this.jsonData =assest.json

            this.initParameters();
            this.initComponent();
        } )


    }

    //初始化参数
    initParameters() {
        this.curSchedule = new Set();
        this.endIndex = 0;
        this.duihuaArrayConfig = this.jsonData.duihuaArray//ZuZhuangMoTuoCheConfig.getInstance().getGameConfig('duihuaArray');
        this.unitIconListConfig = this.jsonData.unitIconList//ZuZhuangMoTuoCheConfig.getInstance().getGameConfig('unitIconList');
        Common.Type = 0;
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.tipsArray//ZuZhuangMoTuoCheConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray//ZuZhuangMoTuoCheConfig.getInstance().getGameConfig('answersArray');

        EventMgr.onEvent_custom(ryw_Event.itemIconTouchTrue, (data_) => {
            this.itemIconTouchCallback(data_.targetNode);
        }, this);

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

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

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

    //初始化组件
    initComponent() {
        let lab = this.node.getChildByName("标题").getChildByName("lab");
        lab.getComponent(cc.Label).string = this.jsonData.titleArray//ZuZhuangMoTuoCheConfig.getInstance().getGameConfig('titleArray');

        let title = this.jsonData.titleArray//ZuZhuangMoTuoCheConfig.getInstance().getGameConfig('titleArray');
        Game.ins.setGameTitle(title)

        let scrZhuang: ItemIconBList = this.ItemIconList.getComponent("ItemIconBList");
        scrZhuang.setIconListData(this.unitIconListConfig, this.checkNodeList.children, this.targetNodeList);

        //this.scheduleOnce(this.setTimeNodePos, 0);
        this.startDuiHua();
    }



    startDuiHua() {
        this.node.getChildByName("mask").active = true;
        let motoche = this.node.getChildByName("layerNode").getChildByName("摩托车");
        cc.tween(motoche)
            .delay(1.5)
            .set({ position: cc.v3(1000), active: true })
            .to(1, { position: cc.v3(0) })
            .call(() => {
                this.showDuiHua(this.duihuaArrayConfig[0]);
            })
            .delay(3.2)
            .to(1, { position: cc.v3(-1000) })
            .set({ active: false })
            .call(() => {
                this.showDuiHua(this.duihuaArrayConfig[2], () => {
                    this.node.getChildByName("layerNode").getChildByName("motuo").active = true;
                    this.node.getChildByName("mask").active = false;
                });
            })
            .start();
        for (const iterator of this.qianshenCheckNodeList) {
            iterator.active = false;
        }
        for (const iterator of this.ceshenCheckNodeList) {
            iterator.active = true;
        }
    }

    itemIconTouchCallback(targetNode: any) {
        if (targetNode.name == "touchNode") {
            console.log("+++++++++++++++++++>" + targetNode.touchIndex);
            this.curSchedule.add(targetNode.touchIndex);

            if (targetNode.touchIndex == 8) {
                let button = this.node.getChildByName("layerNode").getChildByName("button");
                let posY = button.y;
                cc.tween(button)
                    .set({ active: true })
                    .to(0.5, { y: posY - 5 })
                    .to(0.5, { y: posY + 5 })
                    .union()
                    .repeatForever()
                    .start();
            } else if (targetNode.touchIndex == 1) {
                if (!this.curSchedule.has(0)) {
                    this.endIndex = 2;
                }
            } else if (targetNode.touchIndex == 3) {
                if (!this.curSchedule.has(2)) {
                    this.endIndex = 2;
                }
            } else if (targetNode.touchIndex == 10) {
                this.qianshenCheckNodeList[0].active = false;
            } else if (targetNode.touchIndex == 3) {
                this.ceshenCheckNodeList[4].children[0].active = true;
            } else if (targetNode.touchIndex == 7) {
                this.ceshenCheckNodeList[4].children[1].active = true;
            } else if (targetNode.touchIndex == 4) {
                if (targetNode.checkNode.name == "B") {
                    this.node.getChildByName("mask").active = true;
                    let daqi = this.node.getChildByName("layerNode").getChildByName("daqi");
                    let motuo = this.node.getChildByName("layerNode").getChildByName("motuo");
                    daqi.active = true;
                    motuo.getChildByName("8轮胎打气前").active = false;
                    Common5.playEffectCustom("zuzhuangmotuoche", "sound/打气筒");
                    daqi.getComponent(sp.Skeleton).setAnimation(0, "daqi", false);
                    daqi.getComponent(sp.Skeleton).setCompleteListener(() => {
                        daqi.active = false;
                        motuo.getChildByName("8后轮胎").active = true;
                        this.node.getChildByName("mask").active = false;
                    });
                    this.curSchedule.add(101);
                }
            } else if (targetNode.touchIndex == 12) {
                this.node.getChildByName("mask").active = true;
                let role = this.node.getChildByName("layerNode").getChildByName("精神小伙");
                role.active = true;
                role.getComponent(sp.Skeleton).setAnimation(0, "dadianhua", false);
                this.scheduleOnce(() => {
                    role.getComponent(sp.Skeleton).setAnimation(0, "shuohua", true);
                    this.showDuiHua(this.duihuaArrayConfig[1], () => {
                        role.active = false;
                        if (!this.curSchedule.has(0)
                            || !this.curSchedule.has(5)
                            || !this.curSchedule.has(6)
                            || !this.curSchedule.has(8)
                            || !this.curSchedule.has(102)) {
                            this.endIndex = 1;
                        }
                        else if (!this.curSchedule.has(1)
                            || !this.curSchedule.has(2)
                            || !this.curSchedule.has(3)
                            || !this.curSchedule.has(7)
                            || !this.curSchedule.has(101)
                            || !this.curSchedule.has(9)
                            || !this.curSchedule.has(10)
                            || !this.curSchedule.has(103)) {
                            this.endIndex = 2;
                        }
                        this.scheduleOnce(() => {
                            this.endGameAnima(this.endIndex);
                        }, 1);
                    });
                }, 1);

            }
        } else if (targetNode.name == "9焊枪") {
            console.log("+++++++++++++++++++>" + targetNode.name);
            this.curSchedule.add(102);
        } else if (targetNode.name == "11-后视镜 歪") {
            console.log("+++++++++++++++++++>" + targetNode.name);
            this.curSchedule.add(103);
        }

    }

    endGameAnima(endIndex: number) {
        Common5.playEffectCustom("zuzhuangmotuoche", "sound/结尾曲");
        let end = this.node.getChildByName("end");
        end.active = true;
        switch (endIndex) {
            case 0:
                end.getComponent(sp.Skeleton).setAnimation(0, "zhengchang", false);
                break;
            case 1:
                end.getComponent(sp.Skeleton).setAnimation(0, "renchefenli", false);
                break;
            case 2:
                end.getComponent(sp.Skeleton).setAnimation(0, "zhuangzhuzi", false);
                break;
            default:
                break;
        }
        end.getComponent(sp.Skeleton).setCompleteListener(() => {
            if (endIndex == 0) {
                this.endGameView(1);
            } else {
                this.endGameView(0);
            }
        });
    }

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

        if (curLog.effectUrl) {
            Common5.playEffectCustom("zuzhuangmotuoche", curLog.effectUrl);
        }

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

        if (qiPaoPos_ != -1) {
            let qiPaoList = this.node.getChildByName("qiPao");
            let qiPao = qiPaoList.getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions()
            qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(delayTime_)
                .call(() => {
                    qiPao.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("mask").active = true;
        Game.ins.stopTime();
        if (touchIndex == 0) {
            this.scheduleOnce(() => {
                Game.ins.showFail();;
            }, 3);
        } else {
            this.scheduleOnce(() => {
                 Game.ins.showSuccess();;
            }, 3);
        }
    }

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

    onClick(event) {
        let target = event.target;
        console.log("+++++++++++++++++++>" + target.name);
        Common5.playEffect("点击音效");
        switch (target.name) {
            case "button":
                target.active = false;
                this.node.getChildByName("layerNode").getChildByName("motuo").active = false;
                this.node.getChildByName("layerNode").getChildByName("cheshen").active = true;
                this.node.getChildByName("caozuo").getChildByName("A").active = false;
                for (const iterator of this.qianshenCheckNodeList) {
                    iterator.active = true;
                }
                for (const iterator of this.ceshenCheckNodeList) {
                    iterator.active = false;
                }
                break;
            default:
                break;
        }
    }

    //update(dt) { }
}