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

import WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
const {ccclass, property} = cc._decorator;




@ccclass
export default class ZaoTangYiYun extends WordGameBaseComponent{
    @property(cc.Node)
    maskNode:cc.Node = null;  
    @property(cc.Node)
    mapNode:cc.Node = null;  

    @property(cc.Node)
    chadianContent2:cc.Node = null;  
 
    @property(cc.Node)
    chadianNodeList:cc.Node = null;  
    
    @property(cc.Node)
    chadianContent:cc.Node = null;  
    @property(cc.Node)
    chadianPrefab:cc.Node = null;  
    @property(cc.Node)
    chooseNode:cc.Node = null;  
    
    @property(cc.Node)
    duihuaNode2:cc.Node = null;  

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

    @property(cc.Node)
    chadianList:cc.Node[] = [];  
    @property(cc.Node)
    chadianList2:cc.Node[] = [];  
    @property(cc.Node)
    chadianList3:cc.Node[] = []; 
    @property(cc.Node)
    chadianList4:cc.Node[] = []; 
    @property(cc.Node)
    layerNodeList:cc.Node[] = [];  
    @property(cc.Node)
    qipaoList:cc.Node[] = [];  
    
     
    
    gameInfo = null
    bundle = null
    mapNodeFixedScale:number = 0.6//固定缩放(图片太大了)
    touchStartTime:number = 0//触摸开始时间
    originalTouchDistance:number = 0 //起始双指触摸间距
    lastScale:number = 1//上次缩放值
    curScale:number = 1//当前缩放值
    touchId1:number = -1
    touchId2:number = -2

    curFinishNum:number = 0
    chadianMax1 = 5
    chadianMax2 = 3
    chadianMax3 = 3
    chadianMax4 = 3
    
    isStepRight = false;
    curLayer = null
    curLayerIndex = 0
    clickCallBack = null
    isShowResuit: any;
    curContent:cc.Node = null;


    onLoad(){
        DaDianScript.userEnterDaDian()
    }

    
    start(){
        super.start();
        //cc.macro.ENABLE_MULTI_TOUCH = true;
        // this.chadianNodeList.active = true
        this.initMapNodeTouchEvent()

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

        })

        EventMgr.onEvent_custom(ryw_Event.WordGameRevive, () => {
            this.wordGameReviveCallback();
        }, this);
        
        this.startGame()
    }

    initParameters(){
        Common.Type = 0;
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.tipsArray;
        Common.GameSubAnswerConfigs = this.jsonData.answersArray;
        this.refreshTitle()
        this.bundle = Common5.gameConfig.zmGameConfig[Common5.selectGameNum].bundle

    }

    wordGameReviveCallback(){
        this.isShowResuit = false
        this.layerNodeList[this.curLayerIndex].active = true
        this.curLayer =  this.layerNodeList[this.curLayerIndex]

        let resultLayer = this.mapNode.getChildByName('resultLayer')
        let result1 = resultLayer.getChildByName('result1')
        let result2 = resultLayer.getChildByName('result2')
        let result3 = resultLayer.getChildByName('result3')
        result1.active = false
        result2.active = false
        result3.active = false
        resultLayer.active = false

        this.endGameView()
    }

    //初始化触摸监听
    initMapNodeTouchEvent(){

        let prefabWidth = this.mapNode.width
        let prefabHeight = this.mapNode.height

        let maskWidth = this.maskNode.width
        let maskHeight = this.maskNode.height

        let widthScale = maskWidth/prefabWidth
        let heightScale = maskHeight/prefabHeight

        
            
        if(prefabWidth<prefabHeight){
            this.mapNodeFixedScale = widthScale
        }else{
            this.mapNodeFixedScale = heightScale
        }
        
        this.mapNode.scale = this.mapNodeFixedScale
            
        this.curScale = this.mapNodeFixedScale
        this.lastScale = this.mapNodeFixedScale

        this.curLayer = this.layerNodeList[this.curLayerIndex]
        
    }



    
    //检测node是否进入target区域(新版)
    checkIsInAreaNewVersion(pos){
     
    }

    
    //特殊节点的点击事件(主要处理节点可以移动的问题)
    chadianListRegist(name, chadianList){
        
   
        this.chadianNodeList.active = true
        let chadianConfig = this.jsonData[name]
        this.chadianContent.removeAllChildren()
        this.chadianContent2.removeAllChildren()

        this.curContent = null
        if(chadianList.length<=4){
            this.curContent = this.chadianContent2
        }else{
            this.curContent = this.chadianContent
        }
        this.curFinishNum = 0
        for(let i=0;i<chadianList.length;i++){
            this.openTouchEvent(chadianList[i], chadianConfig[i])

            let node_  = cc.instantiate(this.chadianPrefab)
            node_.active = true
            this.curContent.addChild(node_)
        }

        


    }
    openTouchEvent(node, chadianConfig){
        var attrs = {
            isCanMove:false,
            chadianConfig: chadianConfig,
    
        };
        node.attr(attrs)
        node.on(cc.Node.EventType.TOUCH_START,this.touchStartSpecicalNode,this)
        node.on(cc.Node.EventType.TOUCH_MOVE,this.touchMoveSpecicalNode,this)
        node.on(cc.Node.EventType.TOUCH_CANCEL,this.touchEndSpecicalNode,this)
        node.on(cc.Node.EventType.TOUCH_END,this.touchEndSpecicalNode,this)
    }

    closeTouchEvent(node){
        node.off(cc.Node.EventType.TOUCH_START,this.touchStartSpecicalNode,this)
        node.off(cc.Node.EventType.TOUCH_MOVE,this.touchMoveSpecicalNode,this)
        node.off(cc.Node.EventType.TOUCH_CANCEL,this.touchEndSpecicalNode,this)
        node.off(cc.Node.EventType.TOUCH_END,this.touchEndSpecicalNode,this)
    }

    //private specialTouchTime = Date.now();
    touchStartSpecicalNode(event){
        Common5.playEffect("click")
        //this.specialTouchTime = Date.now();
    }
    touchMoveSpecicalNode(event){
        
        // let target = event.target;
        // if(!target.isCanMove){
        //     return
        // }
        // let posi = event.getLocation()//世界坐标
        // posi = target.parent.convertToNodeSpaceAR(posi)
        // target.setPosition(posi)
    }

    touchEndSpecicalNode(event){
        // event.target.zIndex = 0 
        
        let target = event.target;
        let chadianConfig = target.chadianConfig
        
       
        //chadianConfig.isFindChadian
        if (target.active) {
         
            if(!target['ischekChadian']){
                this.curFinishNum ++
                target['ischekChadian'] = true
            }else{
                console.log('已经触发',chadianConfig)
                return
            }
           
            this.closeTouchEvent(target)
            let checkFangda = false
            
            if(chadianConfig.chadianName == '手机'){
                Common5.playEffectCustom("zaotangyian", "sound/手机消息");
                this.mapNode.getChildByName('layer1').getChildByName('maskNode').active = true
                this.mapNode.getChildByName('layer1').getChildByName('手机放大').active = true
                checkFangda = true
            }else if(chadianConfig.chadianName == '催债短信'){
                Common5.playEffectCustom("zaotangyian", "sound/手机消息");
                this.mapNode.getChildByName('layer2').getChildByName('maskNode').active = true
                this.mapNode.getChildByName('layer2').getChildByName('债务放大').active = true
                checkFangda = true
            }else if(chadianConfig.chadianName == '手机聊天'){
                Common5.playEffectCustom("zaotangyian", "sound/手机消息");
                this.mapNode.getChildByName('layer3').getChildByName('maskNode').active = true
                this.mapNode.getChildByName('layer3').getChildByName('手机放大').active = true
                checkFangda = true
            }


            if(checkFangda){

                this.clickCallBack = ()=>{
                    Common5.playEffect("点击茬点音效")
                    let chooseNode:cc.Node = cc.instantiate(this.chooseNode)
                    chooseNode.parent = this.curLayer.getChildByName('chadianParentNode')
                    chooseNode.active = true
                    Common5.setNodeToTargetPos(chooseNode,target)
             
                    let curChadian = this.curContent.children[this.curFinishNum-1]
                    let chadianSpr = curChadian.getChildByName('茬点框').getComponent(cc.Sprite)
                    let str = chadianConfig.duihuaStr
                    let imgUrl = chadianConfig.chadianImg
                    let kuangNode = curChadian.getChildByName('茬点框')
                    cc.tween(kuangNode)
                    .call(()=>{
                        Common5.getSpriteFrameFromBundle("zaotangyian", "texture/茬点/"+ imgUrl, chadianSpr);
                        let lab = curChadian.getChildByName('lab').getComponent(cc.Label)
                        curChadian.getChildByName('lab').active = true
                        lab.string = chadianConfig.chadianName
                        if(this.curLayerIndex==0){
                            this.showDuihua(str)
                        }else{
                            this.showQiPao(str)
                        }
                       
                        this.checkIsFinish()
                    })
                    .to(0.2, {scale:1.2})
                    .to(0.2,  {scale:1.0})
                    .start()
                }
                return
            }else{
                Common5.playEffect("点击茬点音效")
                let chooseNode:cc.Node = cc.instantiate(this.chooseNode)
                chooseNode.parent = this.curLayer.getChildByName('chadianParentNode')
                chooseNode.active = true
                Common5.setNodeToTargetPos(chooseNode,target)
         
                let curChadian = this.curContent.children[this.curFinishNum-1]
                let chadianSpr = curChadian.getChildByName('茬点框').getComponent(cc.Sprite)
                let str = chadianConfig.duihuaStr
                let imgUrl = chadianConfig.chadianImg
                let kuangNode = curChadian.getChildByName('茬点框')
                cc.tween(kuangNode)
                .call(()=>{
                    Common5.getSpriteFrameFromBundle("zaotangyian", "texture/茬点/"+ imgUrl, chadianSpr);
                    let lab = curChadian.getChildByName('lab').getComponent(cc.Label)
                    curChadian.getChildByName('lab').active = true
                    lab.string = chadianConfig.chadianName
                    if(this.curLayerIndex==0){
                        this.showDuihua(str)
                    }else{
                        this.showQiPao(str)
                    }
                   
                    this.checkIsFinish()
    
                })
                .to(0.2, {scale:1.2})
                .to(0.2,  {scale:1.0})
                .start()
            }


            
        }else{
           
        }
    }

    showDuihua(str, delaytime=2,callfunc=null){
        this.duihuaNode.active = true
        this.duihuaNode.stopAllActions()
        let labstr= this.duihuaNode.getChildByName('lab').getComponent(cc.Label)
        if (str) {
            Common5.playEffectCustom("zaotangyian", "sound/"+str);
        }

        labstr.string = str
        cc.tween(this.duihuaNode)
        .delay(delaytime)
        .call(()=>{
            this.duihuaNode.active = false
            if(callfunc){
                callfunc()
            }
        })
        .start()
    }
    showQiPao(str){
        let qipaoNode = this.qipaoList[this.curLayerIndex]
        qipaoNode.scale = 0
        qipaoNode.active = true
        qipaoNode.stopAllActions()
        qipaoNode.getChildByName("lab").getComponent(cc.Label).string = str
        if (str) {
            Common5.playEffectCustom("zaotangyian", "sound/"+str);
        }
     
        cc.tween(qipaoNode)
            .to(0.2,{scale:1})
            .call(()=>{
              
            })
            .delay(2)
            .to(0.2,{scale:0})
        .start()
    }


    checkArea(node,checkNodeTab){
        let checkIndex = null
        for(let i=0;i<checkNodeTab.length;i++){
            let target = this.mapNode.getChildByName(checkNodeTab[i])

            if(target && target.active && Common5.checkContainsNode(target,node)){
                checkIndex = target
                break
            }
        }
        return checkIndex
    }


    checkIsFinish(){
        if(this.curLayerIndex == 0){
            //跳转到第2个界面
            if(this.curFinishNum == this.chadianMax1){
                this.scheduleOnce(()=>{
                    this.chadianNodeList.active = false
                    this.curLayer.getChildByName('chadianParentNode').active = false
                    this.mapNode.getChildByName('maskNode').active = true
                    this.showDuihua('我们去调查一下几位嫌疑人', 3, ()=>{
                        this.changeLayer('怎么了,发生了什么事吗?')
                        this.chadianListRegist('chadianList2', this.chadianList2)
                    })
                },3.5)
          
            }
        }else if(this.curLayerIndex == 1){
            //跳转到选择界面
            if(this.curFinishNum == this.chadianMax2){
                this.scheduleOnce(()=>{
                    this.chadianNodeList.active = false
                    this.curLayer.getChildByName('chadianParentNode').active = false
                
                    this.changeLayer('我还有事呢,能不能快点')
                    this.chadianListRegist('chadianList3', this.chadianList3)
                },3.5)
          
            }
        }else if(this.curLayerIndex == 2){
            if(this.curFinishNum == this.chadianMax3){
                this.scheduleOnce(()=>{
                    this.chadianNodeList.active = false
                    this.curLayer.getChildByName('chadianParentNode').active = false
               
                    this.changeLayer('没想到我刚来就发生了这种事!')
                    this.chadianListRegist('chadianList4', this.chadianList4)

                },3.5)
            }
        }else if(this.curLayerIndex == 3){
            if(this.curFinishNum == this.chadianMax4){
                this.scheduleOnce(()=>{
               

                    this.chadianNodeList.active = false
                    this.curLayer.getChildByName('chadianParentNode').active = false
                    this.mapNode.getChildByName('maskNode').active = true
                    this.showDuihua('都很可疑啊', 3, ()=>{
                        this.mapNode.getChildByName('maskNode').active = false
                        this.layerNodeList[this.curLayerIndex].active = false
                        this.curLayerIndex++
                        this.refreshTitle()
                        this.layerNodeList[this.curLayerIndex].active = true
                        this.curLayer =  this.layerNodeList[this.curLayerIndex]
                        this.endGameView()
                    })


                },3.0)
            }
        }
  
        
    }

    endGameView(){
        let layer = this.layerNodeList[this.curLayerIndex]
        layer.getChildByName('11qian').active = false
        layer.getChildByName('22qian').active = false
        layer.getChildByName('33qian').active = false
        layer.getChildByName('浴帘1').active = false
        layer.getChildByName('浴帘2').active = false
        layer.getChildByName('浴帘3').active = false
        layer.getChildByName('11hou').active = true
        layer.getChildByName('22hou').active = true
        layer.getChildByName('33hou').active = true
        layer.getChildByName('她是1').active = true
        layer.getChildByName('她是2').active = true
        layer.getChildByName('她是3').active = true

        layer.getChildByName('baobaoCheckEnd').active = true
        // layer.getChildByName('药瓶End').active = true

    }


    unlockLevel(sublevel){
        Game.ins.tipUnlock(sublevel)
    }


    changeLayer(str){
        this.mapNode.getChildByName('maskNode').active = false
        this.layerNodeList[this.curLayerIndex].active = false
        this.curLayerIndex++
        this.refreshTitle()
    
        this.layerNodeList[this.curLayerIndex].active = true
        this.curLayer =  this.layerNodeList[this.curLayerIndex]
        this.showQiPao(str)
    }

    clickMaskNodeEvent(){
        this.mapNode.getChildByName('layer1').getChildByName('maskNode').active = false
        this.mapNode.getChildByName('layer1').getChildByName('手机放大').active = false
        if(this.clickCallBack){
            this.clickCallBack()
        }
    }

    clickMaskNodeEvent2(){
        this.mapNode.getChildByName('layer2').getChildByName('maskNode').active = false
        this.mapNode.getChildByName('layer2').getChildByName('债务放大').active = false

        if(this.clickCallBack){
            this.clickCallBack()
        }
    }

    clickMaskNodeEvent3(){
        this.mapNode.getChildByName('layer3').getChildByName('maskNode').active = false
        this.mapNode.getChildByName('layer3').getChildByName('手机放大').active = false

        if(this.clickCallBack){
            this.clickCallBack()
        }
    }

    refreshTitle(){
        Common.subLevel = this.curLayerIndex;
        Game.ins.updateTishiBtn()
        let title = this.jsonData.titleArray[this.curLayerIndex]
        Game.ins.setGameTitle(title)
    }

    startGame(){
        let layer = this.layerNodeList[this.curLayerIndex]
        let zoulu = layer.getChildByName('走路spine')
        cc.tween(zoulu)
        .to(4, {x:0})
        .call(()=>{
            this.mapNode.getChildByName('maskNode').active = true
            Common5.playEffectCustom("zaotangyian", "sound/女摔倒");
            this.showDuihua('啊....',1)
        })
        .delay(2.0)
        .call(()=>{
            this.chadianListRegist('chadianList1', this.chadianList)
            this.mapNode.getChildByName('maskNode').active = false
            zoulu.active = false

            layer.getChildByName('包包茬点').active = true
            layer.getChildByName('手机茬点').active = true
            layer.getChildByName('污渍茬点').active = true
            layer.getChildByName('手指甲茬点').active = true
            layer.getChildByName('白脸茬点').active = true
            layer.getChildByName('影子').active = true
            layer.getChildByName('女人').active = true
     
        })
        .start()
    }


    showDuihua2(str, delaytime=2,callfunc=null){
    
        this.duihuaNode2.active = true
        this.duihuaNode2.stopAllActions()
        let labstr= this.duihuaNode2.getChildByName('lab').getComponent(cc.Label)
        if (str) {
            Common5.playEffectCustom("zaotangyian", "sound/"+str);
        }

        labstr.string = str
        cc.tween(this.duihuaNode2)
        .delay(delaytime)
        .call(()=>{
            this.duihuaNode2.active = false
            if(callfunc){
                callfunc()
            }
        })
        .start()
    }


    yaopingClickEvent(event){
        let target = event.target
        target.active = false
        if(this.isShowResuit){
            return
        }
        this.isShowResuit = true

        this.showDuihua2('真相大白啦!原来她是因为哮喘发作,摔倒在地上,导致头部出血后晕了过去,最后失救而亡',8.0, ()=>{
            Game.ins.showSuccess()
        })
    }


    chooseLayerEvent(event, data){

        if(this.isShowResuit){
            return
        }
        this.isShowResuit = true

        let resultLayer = this.mapNode.getChildByName('resultLayer')
        let result1 = resultLayer.getChildByName('result1')
        let result2 = resultLayer.getChildByName('result2')
        let result3 = resultLayer.getChildByName('result3')
        resultLayer.active = true

        let curNode = null
        let str1 = ''
        if(data == '1'){
            result1.scale = 0.1
            result1.active = true
            str1 = '我真的是被冤枉的'
            curNode = result1
        }else if(data == '2'){
            result2.scale = 0.1
            result2.active = true
            str1 = '拿不出证据,我让你吃不了兜着走'
            curNode = result2
        }else if(data == '3'){
            result3.scale = 0.1
            result3.active = true
            str1 = '我现在已经改过自新了'
            curNode = result3
        }


        cc.tween(curNode)
        .to(1, {scale:1}, {easing:"elasticOut"})
        .call(()=>{
            this.showDuihua(str1)
        })
        .delay(4.0)
        .call(()=>{
            Game.ins.showFailFuHuo()
        })
        .start()

    }
}