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 ZhaoChuZhenXiong extends WordGameBaseComponent{
    @property(cc.Node)
    maskNode:cc.Node = null;  
    @property(cc.Node)
    mapNode:cc.Node = null;  
    @property(cc.Node)
    chadianList:cc.Node[] = [];  
    @property(cc.Node)
    chadianList2:cc.Node[] = [];  
    @property(cc.Node)
    chadianList3:cc.Node[] = [];  
    @property(cc.Node)
    layerNodeList:cc.Node[] = [];  
    
    
    @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.ScrollView)
    my_scrollView:cc.ScrollView = null;  
    @property(cc.Node)
    duihuaNode:cc.Node = null;  
    @property(cc.Node)
    qipaoNode:cc.Node = null;  

    
    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 = 6
    chadianMax3 = 2
    posScx = null
    isStepRight = false;
    curLayer = null
    curLayerIndex = 0
    clickCallBack = null


    onLoad(){
        DaDianScript.userEnterDaDian()
    }

    
    start(){
        super.start();
        cc.macro.ENABLE_MULTI_TOUCH = true;
        this.chadianNodeList.active = true
       
        // Common5.playMusicCustom('pingkurenjia','sound/背景音')
        this.posScx = this.my_scrollView.getContentPosition()
        
        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/ZhaoChuZhenXiongConfig',(assest)=>{
            this.jsonData = assest.json
            this.initParameters();
            this.initMapNodeTouchEvent()
        })

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

    }

    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.mapNode.getChildByName('结束场景2').active = false
        this.mapNode.getChildByName('chooseLayer').active = true
    }

    //初始化触摸监听
    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]
        this.chadianListRegist('chadianList1', this.chadianList)
    }



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

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

        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.chadianContent.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 == '保证书'){
                this.mapNode.getChildByName('layer1').getChildByName('maskNode').active = true
                this.mapNode.getChildByName('layer1').getChildByName('保证书').active = true
                checkFangda = true

            }else if(chadianConfig.chadianName == '孕检单'){
                this.mapNode.getChildByName('layer1').getChildByName('maskNode').active = true
                this.mapNode.getChildByName('layer1').getChildByName('孕检单').active = true
                checkFangda = true
            }else if(chadianConfig.chadianName == '要债信息'){
                this.mapNode.getChildByName('layer1').getChildByName('maskNode').active = true
                this.mapNode.getChildByName('layer1').getChildByName('手机放大').active = true
                checkFangda = true
            }else if(chadianConfig.chadianName == '撕裂的保证书'){
                this.mapNode.getChildByName('layer2').getChildByName('maskNode').active = true
                this.mapNode.getChildByName('layer2').getChildByName('保证书').active = true
                checkFangda = true
            }else if(chadianConfig.chadianName == '手机短信'){
                this.mapNode.getChildByName('layer2').getChildByName('maskNode').active = true
                this.mapNode.getChildByName('layer2').getChildByName('手机短信').active = true
                checkFangda = true
            }
            
            if( checkFangda == true){

                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.chadianContent.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("zhaochuzhenxiong", "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.curLayerIndex==2){
                            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.chadianContent.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("zhaochuzhenxiong", "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.curLayerIndex==2){
                        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("zhaochuzhenxiong", "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.qipaoNode
        qipaoNode.scale = 0
        qipaoNode.active = true
        qipaoNode.stopAllActions()
        qipaoNode.getChildByName("lab").getComponent(cc.Label).string = str
        if (str) {
            Common5.playEffectCustom("zhaochuzhenxiong", "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
                    Common5.playEffectCustom("zhaochuzhenxiong", "sound/报警");
                    this.scheduleOnce(()=>{
                        this.mapNode.getChildByName('maskNode').active = false
                        this.changeLayer('喂,你好,我要报一起入室伤人!',4.0, ()=>{
                            this.chadianListRegist('chadianList2', this.chadianList2)
                        })
                    },1.0)
                },2.5)
          
            }
        }else if(this.curLayerIndex == 1){
            //跳转到选择界面

            if(this.curFinishNum == this.chadianMax2){
                this.scheduleOnce(()=>{
                    this.mapNode.getChildByName('chooseLayer').active = true
                },2.0)
            }
        }else if(this.curLayerIndex == 2){
            if(this.curFinishNum == this.chadianMax3){
                this.scheduleOnce(()=>{
                    this.chadianNodeList.active = false
                    this.mapNode.getChildByName('结束场景').active = true
                    this.mapNode.getChildByName('结束场景').opacity = 0
                    cc.tween(this.mapNode.getChildByName('结束场景'))
                    .to(1.0, {opacity:255})
                 
                    .call(()=>{
                        Common5.playEffectCustom("zhaochuzhenxiong", "sound/结算语音");
                        this.scheduleOnce(()=>{
                            Game.ins.showSuccess()
                        },6.0)
                    })
                    .start()

                },3.0)
            }
        }
  
        
    }



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


    changeLayer(str,delayTime=3, callFunc){
        let changelayer = this.mapNode.getChildByName('changelayer')
        changelayer.active = true

        let lab = changelayer.getChildByName('lab').getComponent(cc.Label)
        lab.string = str

        if (str) {
            Common5.playEffectCustom("zhaochuzhenxiong", "sound/"+str);
        }

        cc.tween(changelayer)
        .delay(delayTime)
        .call(()=>{
            changelayer.active = false
            this.layerNodeList[this.curLayerIndex].active = false

            cc.tween(this.layerNodeList[this.curLayerIndex])
            .to(1.0, {opacity:0})
            .hide()
            .start()


            this.curLayerIndex++
            this.refreshTitle()
            this.layerNodeList[this.curLayerIndex].opacity = 0
            this.layerNodeList[this.curLayerIndex].active = true
            this.curLayer =  this.layerNodeList[this.curLayerIndex]
            cc.tween(this.layerNodeList[this.curLayerIndex])
            .to(1.0, {opacity:255})
            .call(()=>{
                if(callFunc){
                    callFunc()
                }
            })
            .start()

        
            
        })
        .start()
    }

    clickMaskNodeEvent(){
        this.mapNode.getChildByName('layer1').getChildByName('maskNode').active = false
        this.mapNode.getChildByName('layer1').getChildByName('保证书').active = false
        this.mapNode.getChildByName('layer1').getChildByName('孕检单').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
        this.mapNode.getChildByName('layer2').getChildByName('手机短信').active = false
        if(this.clickCallBack){
            this.clickCallBack()
        }
    }


    chooseLGClickEvent(){
        //继续
        this.mapNode.getChildByName('chooseLayer').active = false

        this.chadianNodeList.active = false
        this.curLayer.getChildByName('chadianParentNode').active = false

        this.showDuihua('为什么是老公?',3.0, ()=>{
            // this.mapNode.getChildByName('layer2').getChildByName('快递真相').active = true
            this.showQiPao('我才不是凶手,你搞错了!')

            this.curLayerIndex++
            this.refreshTitle()
            this.curLayer.getChildByName('chadianParentNode').removeAllChildren()
            this.curLayer.getChildByName('chadianParentNode').active = true

            this.curLayer.getChildByName('快递真相茬点').active = true
            this.curLayer.getChildByName('手机check').active = true

            this.chadianListRegist('chadianList3', this.chadianList3)
        })
    }

    chooseKDYClickEvent(){
        //失败
        this.mapNode.getChildByName('结束场景2').active = true
        Common5.playEffectCustom("zhaochuzhenxiong", "sound/失败语音");
        this.scheduleOnce(()=>{
            Game.ins.showFailFuHuo()
        },6.0)
      
    } 

    zhenxiangClickEvent(){
        if(!this.isStepRight){
            this.isStepRight = true
            this.showDuihua('快递员不可能放好快递再来行凶的',3.0, ()=>{
                this.mapNode.getChildByName('结束场景').active = true
                this.mapNode.getChildByName('结束场景').opacity = 0
                cc.tween(this.mapNode.getChildByName('结束场景'))
                .to(1.0, {opacity:255})
                .delay(5.0)
                .call(()=>{
                    Game.ins.showSuccess()
                })
                .start()
            })
        }
    }
    refreshTitle(){
        Common.subLevel = this.curLayerIndex;
        Game.ins.updateTishiBtn()
        let title = this.jsonData.titleArray[this.curLayerIndex]
        Game.ins.setGameTitle(title)
    }
}