// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html


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/Game";

const {ccclass, property} = cc._decorator;

let GameConfig = [
    {name:'通风口',first:'boss', bossBQSkin:'疑问', bossspine:'待机', strBoss:'这是什么声音,难道有老鼠?', strStaff:''},
    {name:'锤子敲老板', first:'boss', bossBQSkin:'待机', bossspine:'晕',   strBoss:'怎么头晕乎乎的', strStaff:''},
    {name:'华莱士', first:'boss', bossBQSkin:'待机', bossspine:'拉肚子',  strBoss:'肚子好疼,我先去趟厕所',strStaff:''},
    {name:'闹钟',first:'staff', bossBQSkin:'疑问', bossspine:'待机',   strBoss:'是吗,难道是我看错了?', strStaff:'老板你没睡醒吧,现在才八点'},
    {name:'新闻', first:'staff', bossBQSkin:'待机', bossspine:'待机',  strBoss:'值得表扬,进去吧', strStaff:'老板我迟到是因为我扶老奶奶过马路耽误了时间'},
    {name:'剪刀', first:'boss', bossBQSkin:'疑问', bossspine:'待机', strBoss:'好端端的怎么突然停电了?', strStaff:''}, 
    {name:'洗浴中心',first:'boss', bossBQSkin:'待机', bossspine:'待机', strBoss:'下次起得早点起哦', strStaff:'好的老板'},
    {name:'外卖美女', first:'boss', bossBQSkin:'待机', bossspine:'色眯眯',strBoss:'美女,方不方便加个联系方式', strStaff:''},
    {name:'麻袋',first:'boss', bossBQSkin:'生气', bossspine:'待机', strBoss:'这事是谁干的,自己站出来我就不计较了', strStaff:''}, //乌鸦飞过
    {name:'下水道', first:'boss', bossBQSkin:'生气', bossspine:'待机', strBoss:'别让我抓到你了', strStaff:''},

]

let answersArray= [
    '1.将锤子拖到老板头上\n2.点击时钟,时间回到八点\n3.将外卖小哥手上的外卖拖给老板\n4.点击外卖箱盖子,将麻袋拖给老板\n5.上划外卖小哥变成美女\n6.将锤子拖到墙上的通风口\n7.上划外面的下水道盖子\n8.点击电箱,将剪刀拖到电箱\n9.点击电视,再点击放大之后的电视\n10.上划员工口袋,将VIP卡拖给老板',
]

let tipsArray= [
    '1、锤子拖给老板\n2、锤子砸通风口'
]

@ccclass
export default class ShangBanChiDaoScript extends WordGameBaseComponent {
    @property(cc.Node)
    sceneNode: cc.Node = null;
    @property(cc.Node)
    qipaoYuangong: cc.Node = null;
    @property(cc.Node)
    qipaoBoss: cc.Node = null;
    @property(cc.Node)
    yuangongNode: cc.Node[] = [];

    @property(cc.Node)
    bossSpine: cc.Node = null;
    @property(cc.Node)
    chuiBossSpine: cc.Node = null;
    // LIFE-CYCLE CALLBACKS:
    @property(cc.Node)
    guandaoSpine: cc.Node = null;

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

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

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

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

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

    
    // onLoad () {}

    curLayerIndex = 0
    firstPos = cc.v2(0,0)
    curYuanGong:cc.Node = null

    isAllClearWaiMai = false
    isVip = false
    isVipFinish = false
    isKaiDianxiang = false

    firstDuihua = '现在已经八点半了,我看看谁还没到公司'
    endDuihua = '奇怪,今天居然没一个人迟到'
    isQiaoTF: boolean = false;
    isQiaoLaoban: boolean = false;
    isTouchDianshi: boolean = false;
    isTouchBiao: boolean = false;
    //'别以为我这么好糊弄'
    start () {
        Common.Type = 0;
        Common.subLevel = this.curLayerIndex;
        Common.GameSubTipConfigs = tipsArray
        Common.GameSubAnswerConfigs = answersArray

        EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
            Game.ins.stopTime();
            Game.ins.showFail();;
        }, this);

        
        super.start();
        DaDianScript.userEnterDaDian();
 
      
        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCheckCallback(data_.targetNode);
        }, this);

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

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


        this.curPeopleResetAnim()
        this.resetBoss()
        this.showQiPao(this.qipaoBoss, this.firstDuihua)
    }

    getConfigByName(name){
        let config = null
        for(let i= 0;i<GameConfig.length;i++){
            if(GameConfig[i].name==name){
                config = GameConfig[i]
                break
            }
        }

        return config
    }
    normalTouchCheckCallback(targetNode){
        console.log(targetNode.name, 'normalTouchCheckCallback++++++++===')
        if(targetNode.name == '电箱touch'){
            this.dianXiangSpine.getComponent(sp.Skeleton).setAnimation(0,'开电箱有电线',false)
        }else if(targetNode.name == '口袋touch'){
            let xiyuka = this.sceneNode.getChildByName('洗浴中心大')

            xiyuka.getComponent(cc.Animation).play()
           
            xiyuka.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, ()=>{
                this.sceneNode.getChildByName('洗浴中心大').active = false
                this.sceneNode.getChildByName('洗浴中心').active = true
          
            })


        }else if(targetNode.name == '表touch'){
            this.isTouchBiao = true

        }else if(targetNode.name == '闹钟动画spine'){
            this.maskNode.active = true
            this.isTouchBiao = false
            this.naozhongSpine.active = true
            this.naozhongSpine.getComponent(sp.Skeleton).setAnimation(0,'闹钟',false)
            this.naozhongSpine.getComponent(sp.Skeleton).setCompleteListener(()=>{
                this.naozhongSpine.active = false
                let _config = this.getConfigByName('闹钟')
                this.sceneNode.getChildByName('badian').active = true

                this.playBossSpine(_config)
                this.showDuihua(_config, ()=>{
                    // this.resetBoss()
                    // this.peopleCaDui()
                    this.playYuanGongRun(()=>{
                        this.resetBoss()
                        this.peopleCaDui()  
                    })
                })
                //
            })
        }else if(targetNode.name == '电视touch'){

            this.isTouchDianshi = true
        }else if(targetNode.name == '电视屏幕'){
            this.isTouchDianshi = false
            targetNode.active = false
            this.maskNode.active = true
            let _config = this.getConfigByName('新闻')
            this.playBossSpine(_config)
            this.showDuihua(_config, ()=>{
                // this.resetBoss()
                // this.peopleCaDui()
                this.playYuanGongRun(()=>{
                    this.resetBoss()
                    this.peopleCaDui()  
                })
            })

        }else if(targetNode.name == '下水道盖touch'){
            this.maskNode.active = true
            this.xiashuidaoSpine.active = true
            let curPeople = this.yuangongNode[this.curLayerIndex]
            curPeople.active = false
            this.xiashuidaoSpine.getComponent(sp.Skeleton).setAnimation(0,'下水道',false)
            this.xiashuidaoSpine.getComponent(sp.Skeleton).setCompleteListener(()=>{
                this.xiashuidaoSpine.active = false
                let _config = this.getConfigByName('下水道')
                this.playBossSpine(_config)
                this.showDuihua(_config, ()=>{
                    // this.resetBoss()
                    // this.peopleCaDui()
                    this.playYuanGongRun(()=>{
                        this.resetBoss()
                        this.peopleCaDui()  
                    })
                })
            })
        }else if(targetNode.name == '开关touch'){
            
            this.scheduleOnce(()=>{
             
                this.sceneNode.getChildByName('关灯黑屏').active = false

                this.showQiPao(this.qipaoBoss, '别以为我这么好糊弄')

            },0.5)
        }else if(targetNode.name == '外卖员touch'){
            this.maskNode.active = true
            this.scheduleOnce(()=>{
                this.sceneNode.getChildByName('外卖箱子').active = false    
                let _config = this.getConfigByName('外卖美女')
                this.playBossSpine(_config)
                this.showDuihua(_config, ()=>{
                    this.resetBoss()
                    this.peopleCaDui()  
                })
                this.playYuanGongRun()
            },0.5)

        }
    }

    normalTouchMoveCheckCallback(targetNode){
        console.log(targetNode.name, 'normalTouchMoveCheckCallback 移动++++++++===')
        if(targetNode.name == '麻袋move'){
            this.maskNode.active = true
            let _config = this.getConfigByName('麻袋')
            this.bossSpine.getComponent(sp.Skeleton).setSkin('待机')
            this.bossSpine.getComponent(sp.Skeleton).setAnimation(0,'麻袋',false)
            this.scheduleOnce(()=>{
             
                this.bossSpine.getComponent(sp.Skeleton).setAnimation(0,'麻袋待机',true)
                this.playYuanGongRun()
                this.scheduleOnce(()=>{
                    this.playBossSpine(_config)
          
                    this.showDuihua(_config, ()=>{
                        this.bossSpine.getComponent(sp.Skeleton).setAnimation(0,'无语',true)
                        this.peopleCaDui()  
                        this.scheduleOnce(()=>{
                            this.resetBoss()
                        },1.0)
                    })
                },1.5)

            },0.5)

            let hualaishi = this.sceneNode.getChildByName('外卖员').getChildByName('哗莱士move')
            let madai = this.sceneNode.getChildByName('麻袋move')
            let waimai = this.sceneNode.getChildByName('外卖箱子').getChildByName('外卖盖子touch')
            if(hualaishi.active || madai.active || waimai.active){
                
            }else{
                this.sceneNode.getChildByName('外卖员').getChildByName('外卖员touch').active = true
            }
        }else if(targetNode.name == '剪刀move'){
            this.maskNode.active = true
            Common5.playEffectCustom("shangbanchidao", "sound/剪刀剪");
            this.dianXiangSpine.getComponent(sp.Skeleton).setAnimation(0,'剪电线',false)
            let _config = this.getConfigByName('剪刀')
            this.scheduleOnce(()=>{
                this.sceneNode.getChildByName('关灯黑屏').active = true
                this.playYuanGongRun()

                this.playBossSpine(_config)
                this.scheduleOnce(()=>{
                    this.sceneNode.getChildByName('关灯黑屏').active = false
                    this.showDuihua(_config, ()=>{
                        this.resetBoss()
                        this.peopleCaDui()  
                        
                    })
                },1.5)
            

                
            },0.3)
        }else if(targetNode.name == '哗莱士move'){
            this.maskNode.active = true
            let _config = this.getConfigByName('华莱士')
            this.playBossSpine(_config)
            
            this.showDuihua(_config, ()=>{
                cc.tween(this.bossSpine) 
                .to(0.5,{x:700})
                .call(()=>{
                    this.playYuanGongRun()
                    this.scheduleOnce(()=>{
                        this.resetBoss()
                        this.peopleCaDui()  
                    },1.5)
                })
                .start()

            })
            let hualaishi = this.sceneNode.getChildByName('外卖员').getChildByName('哗莱士move')
            let madai = this.sceneNode.getChildByName('麻袋move')
            let waimai = this.sceneNode.getChildByName('外卖箱子').getChildByName('外卖盖子touch')
            if(hualaishi.active || madai.active || waimai.active){
                
            }else{
                this.sceneNode.getChildByName('外卖员').getChildByName('外卖员touch').active = true
            }
        }else if(targetNode.name == '洗浴中心'){
            this.maskNode.active = true
            let _config = this.getConfigByName('洗浴中心')
            this.playBossSpine(_config)
            this.showDuihua(_config, ()=>{
                this.playYuanGongRun()
                this.scheduleOnce(()=>{
                    this.resetBoss()
                    this.peopleCaDui()  
                },1.5)

            })


        }
    }
    


    curPeopleResetAnim(){

        for(let i = 0;i<this.yuangongNode.length;i++){
            let curPeople = this.yuangongNode[i]
            curPeople.getComponent(sp.Skeleton).setAnimation(0,'卡待机',true)
    
        }

    }


    resetBoss(){
       
        let boss = this.bossSpine
        boss.setPosition(cc.v2(51.5, 15))
        boss.getComponent(sp.Skeleton).setSkin('待机')
        boss.getComponent(sp.Skeleton).setAnimation(0,'待机',true)

        console.log('resetBoss')
    }
    peopleCaDui(){
        this.curLayerIndex++
        let node = this.sceneNode.getChildByName('女员工初始位置')
 

        let curPeople = this.yuangongNode[this.curLayerIndex]
        if(curPeople){
            let posi = Common5.getNodeToTargetPos(curPeople, node)
            if(curPeople){
                cc.tween(curPeople)
                .to(0.3,{x:posi.x, y:posi.y})
                .call(()=>{
                    this.maskNode.active = false
                    if(this.isVip){
                        let xiyuka = this.sceneNode.getChildByName('洗浴中心')
                        xiyuka.active = true
                    }else{
                        let xiyuka = this.sceneNode.getChildByName('洗浴中心')
                        xiyuka.active = false
                    }
                })
                .start()
            }
        }else{
            //人走完了
            Game.ins.stopTime();
            this.showQiPao(this.qipaoBoss, this.endDuihua, ()=>{
                Game.ins.showSuccess();
            })
        }  
 
    }





    playBossSpine(duihuaConfig){
        let first = duihuaConfig.first
        let bossBQSkin = duihuaConfig.bossBQSkin
        let bossspine = duihuaConfig.bossspine
        let strBoss = duihuaConfig.strBoss
        let strStaff = duihuaConfig.strStaff
        let name = duihuaConfig.name
        let boss = this.bossSpine
        if(bossspine == '待机'){

        }else if(bossspine == '晕'){

        }else if(bossspine == '拉肚子'){

        }else if(bossspine == '麻袋'){

        }else if(bossspine == '色眯眯'){

        }
        boss.getComponent(sp.Skeleton).setSkin(bossBQSkin)
        boss.getComponent(sp.Skeleton).setAnimation(0,bossspine,true)

    }

    playYuanGongRun(callfunc?:Function){
        let xiyuka = this.sceneNode.getChildByName('洗浴中心')
        let isShow = false
        if(xiyuka.active){
            isShow = true
            xiyuka.active = false
            this.isVip = true
        }else{
            this.isVip = false
        }
        let curPeople = this.yuangongNode[this.curLayerIndex]
        curPeople.getComponent(sp.Skeleton).setAnimation(0,'跑走',false)
        this.scheduleOnce(()=>{
            curPeople.active = false
      
        },1.2)

        if(callfunc){
            callfunc()
        }
    }

    showDuihua(duihuaConfig, callfunc?:Function){
        let first = duihuaConfig.first
        let bossBQSkin = duihuaConfig.bossBQSkin
        let bossspine = duihuaConfig.bossspine
        let strBoss = duihuaConfig.strBoss
        let strStaff = duihuaConfig.strStaff
        let name = duihuaConfig.name



        if(first == 'boss'){
            this.showQiPao(this.qipaoBoss,strBoss, ()=>{
                if(strStaff.length>0){
                    this.showQiPao(this.qipaoYuangong, strStaff, callfunc)
                }else{
                    if(callfunc){
                        callfunc()
                    }
                }
            })

        }else if(first == 'staff'){
            this.showQiPao(this.qipaoYuangong, strStaff, ()=>{
                if(strBoss.length>0){
                    this.showQiPao(this.qipaoBoss,strBoss, callfunc)
                }else{
                    if(callfunc){
                        callfunc()
                    }
                }
            })
        }


    }


    showQiPao(qipaoNode,str, callfunc?:Function){
      
        qipaoNode.scale = 0
        qipaoNode.active = true
        qipaoNode.stopAllActions()
        qipaoNode.getChildByName("str").getComponent(cc.Label).string = str
        if (str) {
            Common5.playEffectCustom("shangbanchidao", "sound/"+str);
        }
        
        let delay = 3
        console.log(str.length, 'str+++++++')
        if(str.length<=5 ){
            delay = 1.5
        }
        if(str.length>=15 ){
            delay = 4
        }
        if(str.length>=20 ){
            delay = 5
        }
        cc.tween(qipaoNode)
            .to(0.2,{scale:1})
            .delay(delay)
            .to(0.2,{scale:0})
            .call(()=>{
                if(callfunc){
                    callfunc()
                }
            })
        .start()
    }




    // update (dt) {}
    qiaoyunlaoban(){
        console.log('敲晕老板2')
        let _config = GameConfig[1]
        //敲晕

        this.isQiaoLaoban = true
        this.chuiBossSpine.active = true
        Common5.playEffectCustom("shangbanchidao", "sound/锤子砸人");

        this.scheduleOnce(()=>{
            this.playBossSpine(_config)
            this.chuiBossSpine.active = false
            this.scheduleOnce(()=>{
                this.playYuanGongRun()
            },1)
            this.showDuihua(_config, ()=>{
                if( this.isQiaoLaoban  &&  this.isQiaoTF){

                }else{
                    this.sceneNode.getChildByName('锤子move').active = true
                }
             
                this.resetBoss()
                this.peopleCaDui()

            })
        },0.3)

    
    
    }

    tongfengkou(){
        console.log('敲通风口')
        this.maskNode.active = true
        let _config = GameConfig[0]
        this.isQiaoTF= true
        this.chuiziSpine.active = true
        this.sceneNode.getChildByName('通风口遮挡check').active = false
        let curPeople = this.yuangongNode[this.curLayerIndex]
        Common5.playEffectCustom("shangbanchidao", "sound/锤子砸通风管道");
        this.scheduleOnce(()=>{
            curPeople.active = false
            Common5.playEffectCustom("shangbanchidao", "sound/管道爬行");
            this.guandaoSpine.getComponent(sp.Skeleton).setAnimation(0,'爬管道',false)
            this.scheduleOnce(()=>{
                this.showDuihua(_config, ()=>{
                    if( this.isQiaoLaoban  &&  this.isQiaoTF){
    
                    }else{
                        this.sceneNode.getChildByName('锤子move').active = true
                    }
                    this.guandaoSpine.getComponent(sp.Skeleton).setAnimation(0,'管道',false)
                    this.resetBoss()
                    this.peopleCaDui()
    
                })
            },2.5)
        },0.7)
        //爬下水道

    }


    // update (dt) {}
}