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 AChaDianIconList from "../../../WordGame/gameComScript/JuQing/AChaDianIconList";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";
// import ZhangFuMiMiConfig from "./ZhangFuMiMiConfig";
const { ccclass, property } = cc._decorator;

@ccclass
export default class ZhangFuMiMiScript extends WordGameBaseComponent {

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

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

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

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

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

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

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

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

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



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

    //当前进度
    chadianNum = 0;
    curSchedule = 0;

    //文本配置
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chaDianListConfig: any[] = [];
    successMaxNum: number = 0;


    shouhoujiangxiangIndex = 0
    isCheckZaDiban: boolean = false;
    isCheckZaTianHuaBan: boolean = false;
    isCheckZaQiang: boolean = false;
    isshowSuccess: any;
    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        this.openTouchEvent(this.node.getChildByName('bg'));
        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/ZhangFuMiMiConfig',(assest)=>{
            //this.jsonData = JSON.stringify(assest.json)

            this.jsonData =assest.json

            this.initParameters();
            this.initComponent();
        } )
        //Common5.playMusic('wanglian/背景音', 'WordGame');
    }

    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;
        this.chadianNum = 0;
      
        this.titleArrayConfig = this.jsonData.titleArray//ZhangFuMiMiConfig.getInstance().getGameConfig('titleArray') as string[];
        this.duihuaArrayConfig =  this.jsonData.duihuaArray//ZhangFuMiMiConfig.getInstance().getGameConfig('duihuaArray') as any[];
        this.chaDianListConfig =  this.jsonData.chaDianList//ZhangFuMiMiConfig.getInstance().getGameConfig('chaDianList') as any[];

        Common.Type = 0
        Common.subLevel = this.curLayerIndex;
        Common.GameSubTipConfigs =  this.jsonData.tipsArray//ZhangFuMiMiConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray// ZhangFuMiMiConfig.getInstance().getGameConfig('answersArray');

        EventMgr.onEvent_custom(ryw_Event.touchChaDianCheck, (data_) => {
            this.touchChaDianCheckCallback(data_.touchIndex);
        }, this);

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

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

    initComponent() {
        let title = this.titleArrayConfig[this.curLayerIndex];
        let lab = this.node.getChildByName("标题").getChildByName("lab");
        lab.getComponent(cc.Label).string = title;
        Game.ins.setGameTitle(title)
        let scr = this.aChaDianIconList.getComponent(AChaDianIconList);
        scr.setChaDianIconList(this.chaDianListConfig[0].chaDianIconList);

        let chaDianIconList = this.chaDianListConfig[0].chaDianIconList;
        for (const info of chaDianIconList) {
            this.successMaxNum += info.number;
        }
        this.refreshScheduleText();

  
        this.node.getChildByName("mask").active = true;
        this.showDuiHua(this.duihuaArrayConfig[0], () => {
            this.showDuiHua(this.duihuaArrayConfig[1], () => {
                this.node.getChildByName("mask").active = false;
            });
        });
    }



    //刷新进度文本
    refreshScheduleText() {
        this.scheduleText.getComponent(cc.Label).string = "拿铁咖啡:" + this.curSchedule + '/' + this.successMaxNum;
    }

    //茬点回调
    touchChaDianCheckCallback(touchIndex: number) {

        console.log(touchIndex, '茬点回调++touchIndex++==')
        let chaDianIconList = this.chaDianListConfig[0].chaDianIconList;
        let chaDianLog = this.chaDianListConfig[0].chaDianLog;
        this.chadianNum++;
        this.curSchedule += chaDianIconList[touchIndex].number;
        this.refreshScheduleText();
        this.showDuiHua(chaDianLog[touchIndex]);

        if (touchIndex == 5) {
            this.nanzhuSpineNode.getComponent(sp.Skeleton).setAnimation(0, 'nanjingzhi', false)
        }
        if (this.chadianNum == chaDianLog.length) {
            this.scheduleOnce(() => {
                this.endGameView(1);
            }, 3);
        }
    }

    //点击事件回调
    normalTouchEndCheckCallback(targetNode: any) {
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        switch (targetNode.name) {
            case "手机界面":
                this.node.getChildByName("mask").active = true;
                let kuaidi = this.node.getChildByName("外卖员");
                cc.tween(kuaidi)
                    .set({ active: true, position: cc.v3(600, -78) })
                    .to(1, { position: cc.v3(100, -78) })
                    .call(() => {
                        this.node.getChildByName("mask").active = false;
                        Common5.playEffectCustom("zhangfumimi", "Sounds/zhangfumimi/手机订单到了后的提示音");
                    })
                    .start();
                break;
            case "手后拿铁66":

                if (this.shouhoujiangxiangIndex <= 4) {
                    let node_ = this.shouhouNatie.children[this.shouhoujiangxiangIndex]
                    let copty = cc.instantiate(this.shouhouNatieItem)
                    copty.parent = targetNode.parent



                    let endPos = Common5.getNodeToTargetPos(copty, node_)

                    let jumpTo = cc.jumpTo(0.3, cc.v2(endPos.x, endPos.y), 30, 1)


                    copty.scale = 0.1
                    cc.tween(copty)
                        .then(jumpTo)
                        .to(0.3, { scale: 1 }, { easing: 'easeInOut' })
                        .union()
                        .call(() => {
                            copty.stopAllActions()
                            copty.removeFromParent()
                            copty.destroy()
                            node_.active = true
                        })
                        .start()

                    this.shouhoujiangxiangIndex++
                    if (this.shouhoujiangxiangIndex == 5) {

                        this.node.getChildByName('role').getChildByName('手后拿铁66').active = false
                        this.node.getChildByName('role').getChildByName('手后拿铁77').active = false
                        this.node.getChildByName('role').getChildByName('拿铁4').active = false
                        this.natieChadian.active = true
                    }

                }

                break
            case "墙纸checkNode":
                let qiangnaChadian = this.node.getChildByName('后背景').getChildByName('墙壁拿铁')
                qiangnaChadian.active = true

                break
            default:
                break;
        }
    }

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

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

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

        if (qiPaoPos_ != -1) {
            let qiPao = this.node.getChildByName("qiPao").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(3)
                .call(() => {
                    qiPao.active = false
                })
                .start();
        }
        else {
            let logPrefab = this.node.getChildByName('logPrefab')
            let script_: logPrefabScript = logPrefab.getComponent('logPrefabScript')
            script_.setDailogShow(string_)
        }

        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 {
            if(this.isshowSuccess){
                return
            }
            this.isshowSuccess = true
            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
        let logPrefab = this.node.getChildByName('logPrefab')
        logPrefab.getChildByName('duihua').active = false;
    }




    checkZaDiban(target) {
        console.log(target, 'checkZaDiban')
        this.chuiziDibanSpine.active = true
        this.isCheckZaDiban = true
        this.scheduleOnce(() => {
            Common5.playEffectCustom("zhangfumimi", "Sounds/zhangfumimi/锤子砸击音效");
        }, 0.5)
        this.chuiziDibanSpine.getComponent(sp.Skeleton).setAnimation(0, 'qiao', false)
        this.chuiziDibanSpine.getComponent(sp.Skeleton).setCompleteListener(() => {
            this.chuiziDibanSpine.active = false
            let diban = this.node.getChildByName('地板底')

            diban.getChildByName('地板').active = false
            let natiechadian = diban.getChildByName('酱香咖啡-地板')
            let suidiban = diban.getChildByName('碎地板')

            natiechadian.active = true
            suidiban.active = true

            this.node.getChildByName('锤子').active = true
            this.checkChuizi()
        })
    }

    checkZaTianHuaBan(target) {

        this.isCheckZaTianHuaBan = true


        console.log(target, 'checkZaTianHuaBan+++')
        this.chuiziTianhuabanSpine.active = true

        this.scheduleOnce(() => {
            Common5.playEffectCustom("zhangfumimi", "Sounds/zhangfumimi/锤子砸击音效");
        }, 0.5)
        this.chuiziTianhuabanSpine.getComponent(sp.Skeleton).setAnimation(0, 'qiao', false)
        this.chuiziTianhuabanSpine.getComponent(sp.Skeleton).setCompleteListener(() => {
            this.scheduleOnce(() => {
                this.chuiziTianhuabanSpine.active = false
                let diban = this.node.getChildByName('后背景')

                let natiechadian = diban.getChildByName('天花板')
                let suidiban = diban.getChildByName('天花板-2')

                natiechadian.active = true
                cc.tween(natiechadian)
                    .to(1, { y: natiechadian.getPosition().y - 150 })
                    .call(() => {
                        natiechadian.getChildByName('天花板check').active = true
                    })
                    .start()

                suidiban.active = false

                this.node.getChildByName('锤子').active = true
                this.checkChuizi()
            }, 0.5)

        })
    }

    checkZaQiang(target) {
        console.log(target, 'checkZaQiang++++')
        this.chuiziQIangSpine.active = true
        this.isCheckZaQiang = true

        Common5.playEffectCustom("zhangfumimi", "Sounds/zhangfumimi/锤子砸击音效");
        this.chuiziQIangSpine.getComponent(sp.Skeleton).setAnimation(0, 'qiao', false)
        this.chuiziQIangSpine.getComponent(sp.Skeleton).setCompleteListener(() => {
            this.chuiziQIangSpine.active = false
            let diban = this.node.getChildByName('后背景')
            let suidiban = diban.getChildByName('临时墙壁')
            let che = diban.getChildByName('车')
            suidiban.active = false
            che.active = true
            Common5.playEffectCustom("zhangfumimi", "Sounds/zhangfumimi/车子滴滴声");
            let natielala = this.node.getChildByName('拿铁拉拉')
            cc.tween(che)
                .to(1.5, { x: 120, y: -83 })
                .delay(1.0)
                .call(() => {
                    Common5.playEffectCustom("zhangfumimi", "Sounds/zhangfumimi/车子喷射拿铁音效");
                    natielala.active = true
                    natielala.getComponent(sp.Skeleton).setAnimation(0, 'pennatie', false)
                    natielala.getComponent(sp.Skeleton).setCompleteListener(() => {

                        natielala.getChildByName('拿铁拉拉Chadian').active = true
                    })
                })
                .start()
            this.node.getChildByName('锤子').active = true
            this.checkChuizi()

        })

    }


    checkChuizi() {
        if (this.isCheckZaQiang && this.isCheckZaTianHuaBan && this.isCheckZaDiban) {
            this.node.getChildByName('锤子').active = false
        }
    }
    //update(dt) { }
}