import Common5 from "../../../../Platform/th/Common5";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
import ZhaoChaV3Base from "../../../fkwzCommon123/ZhaoCha_V3/script/ZhaoChaV3Base";


const {ccclass, property} = cc._decorator;

@ccclass
export default class ZhaoChaMap2205 extends ZhaoChaV3Base {
    bStartTimer:boolean = true
    _dt:number = 0
    _timeNum:number = 1080
    _curFindChaDianNum = 0
    // protected handFeiChaDianEvent:Function = null
    private gameConfig = {
        ChaNode:{
            NvRenShouJi:{desc:"女人手机",isFindOut:false,isCanTouch:false,needFDJ:false,startNode:"",endNode:"",sound:"发生了什么事情让女子求救", money:0}, 
            NanRenShouJi:{desc:"男人手机",isFindOut:false,isCanTouch:false,needFDJ:true,startNode:"",endNode:"shouJi", sound:"这,这究竟是怎么回事", money:0}, 
            GuangGaoPai:{desc:"电视机广告牌",isFindOut:false,isCanTouch:false,needFDJ:false,startNode:"",endNode:"", sound:"什么?难道说被抢劫的就是这名女子?", money:0}, 
            GuHuiHe:{desc:"骨灰盒",isFindOut:false,isCanTouch:true,needFDJ:true, startNode:"",endNode:"",sound:"他手上抱着的照片不就是这个女孩吗", money:0}, 
            RenYing:{desc:"人影",isFindOut:false,isCanTouch:true,needFDJ:true, startNode:"",endNode:"guiYing",sound:"难道说,这个女孩已经...", money:0}, 
            GaoYaGuo:{desc:"高压锅",isFindOut:false,isCanTouch:true,needFDJ:true,startNode:"",endNode:"", money:0}, 
            GuiMenGuan:{desc:"鬼门关",isFindOut:false,isCanTouch:true,needFDJ:true,startNode:"luXianTu1",endNode:"luXianTu2", sound:"怎么回事,下一站怎么是鬼门关?", money:0}, 
            DiTie44:{desc:"44号线",isFindOut:false,isCanTouch:true,needFDJ:true,startNode:"xianLu1",endNode:"xianLu2",sound:"地铁怎么会有44号线这种不吉利的数字", money:0}, 
            XueJi:{desc:"血迹",isFindOut:false,isCanTouch:true,needFDJ:true,startNode:"",endNode:"xueJi",sound:"这个血迹是哪来的", money:0}, 
            Dao:{desc:"刀",isFindOut:false,isCanTouch:true,needFDJ:true,startNode:"",endNode:"dao", sound:"这个看起来像作案凶器", money:0}, 
            GuiLian:{desc:"鬼脸",isFindOut:false,isCanTouch:true,needFDJ:true, startNode:"",endNode:"",sound:"这,这究竟是怎么回事", money:0}
        },
        FeiChanDian:{
            nvShengShouJiTouchArea:{isCanTouch:true,isTouchOff:false},
            nanShengShouJiTouchArea:{isCanTouch:true,isTouchOff:false},
            guangGaoPaiTouchArea:{isCanTouch:true,isTouchOff:false}
        },
        soundurl: 'sound/',
        totalMoney:0,
        minMoney:0
    }
    onLoad () { 
        super.onLoad();
        this.chaDianConfig = this.gameConfig;
    }
    protected update(dt: number): void {
        if(this.bStartTimer){
            this._dt += dt
            if(this._dt >= 1){
                this._dt = 0
                this._timeNum += 1
                this.setTimeStr()
            }
        }
    }
    setTimeStr(){
        let timeStr = this.node.getChildByName("duiHuaDi").getChildByName("clock").getChildByName("timeStr")
        let miao = Math.floor(this._timeNum % 60) + ""
        let fen = Math.floor(this._timeNum/60) + ""
        timeStr.getComponent(cc.Label).string = fen + ":" + miao.padStart(2,'0')
    }
    start () {
        super.start();
        // Common5.playEffectCustom(this.subBundle,this.chaDianConfig.soundurl+"这是我们的至尊墓地,包您满意");
        // this.showDialogue("这是我们的至尊墓地,包您满意")
    }
    openTouchEvent(node,attrs){
        node.attr(attrs)
        this.openTouchEvent1(node)
    }
    openTouchEvent1(node){
        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)
    }
    touchStartSpecicalNode(event){
        this.touchStartTime = Date.now();
        if(event.target.name == "chatNanTouchArea" || event.target.name == "chatNvTouchArea" || event.target.name == "xinWenTouchArea"){
            event.target.callFunction()
        }
    }
    touchMoveSpecicalNode(event){
        if(!event.target.bCanMove){
            return
        }
        let nodeLoc = event.getLocation()
        let nodePos = event.target.parent.convertToNodeSpaceAR(nodeLoc)
        event.target.setPosition(nodePos)
    }
    touchEndSpecicalNode(event){
        if(event.target.touchArea && Common5.checkContainsNode(event.target.touchArea,event.target)){ 
            this.closeTouchEvent(event.target)
            event.target.active = false
            
            event.target.endNode && (event.target.endNode.active = true)
            event.target.bindChaNodeInfo && (event.target.bindChaNodeInfo.isCanTouch = true)
            event.target.callFunction && event.target.callFunction()
        }else{
            event.target.setPosition(event.target.startPos)
        }
    }
    specialNodeMoveEvent(){
        console.log("specialNodeMoveEvent子类==")

        //男手机
        let nanShouJiBoxNode = this.node.getChildByName("nanShouJiBoxNode")
        let chatNanTouchArea = nanShouJiBoxNode.getChildByName("chatNanTouchArea")
        let nanShouJiAttrs = { 
            startPos:chatNanTouchArea.getPosition(), 
            bCanMove:false,
            callFunction: ()=>{
                nanShouJiBoxNode.active = false
                this.chaDianConfig.FeiChanDian["nanShengShouJiTouchArea"].isTouchOff = true
                this.fangdajing.active = true
                let chaNode = this.maskBg.getChildByName('chaDian').getChildByName('NanRenShouJi')
                this.findOnChaDian(chaNode)
            }
        }
        this.openTouchEvent(chatNanTouchArea,nanShouJiAttrs)

        let nvShouJiBoxNode = this.node.getChildByName("nvShouJiBoxNode")
        let chatNvTouchArea = nvShouJiBoxNode.getChildByName("chatNvTouchArea")
        let nvShouJiAttrs = { 
            startPos:chatNvTouchArea.getPosition(), 
            bCanMove:false,
            callFunction: ()=>{
                nvShouJiBoxNode.active = false
                this.chaDianConfig.FeiChanDian["nvShengShouJiTouchArea"].isTouchOff = true
                this.fangdajing.active = true
                let chaNode = this.maskBg.getChildByName('chaDian').getChildByName('NvRenShouJi')
                this.findOnChaDian(chaNode)
            }
        }
        this.openTouchEvent(chatNvTouchArea,nvShouJiAttrs)

        let xinWenBoxNode = this.node.getChildByName("xinWenBoxNode")
        let xinWenTouchArea = xinWenBoxNode.getChildByName("xinWenTouchArea")
        let xinWenAttrs = { 
            startPos:xinWenTouchArea.getPosition(), 
            bCanMove:false,
            callFunction: ()=>{
                xinWenBoxNode.active = false
                this.chaDianConfig.FeiChanDian["guangGaoPaiTouchArea"].isTouchOff = true
                this.fangdajing.active = true
                let chaNode = this.maskBg.getChildByName('chaDian').getChildByName('GuangGaoPai')
                this.findOnChaDian(chaNode)
            }
        }
        this.openTouchEvent(xinWenTouchArea,xinWenAttrs)
    } 
    afterChaDianFindCallback(nameStr){
        console.log("afterChaDianFindCallback子类==")
        let startNodeStr = this.chaDianConfig.ChaNode[nameStr].startNode
        let endNodeStr = this.chaDianConfig.ChaNode[nameStr].endNode
        if(startNodeStr){
            let startNode = this.bg.getChildByName(startNodeStr)
            startNode.active = false
        }
        if(endNodeStr){
            let endNode = this.bg.getChildByName(endNodeStr)
            endNode.active = true
        }
        this._curFindChaDianNum++
        if(nameStr == "GuiLian"){
            Common5.playMusicCustom(this.bundle,this.chaDianConfig.soundurl + "背景音乐")
            let nvSpine = this.bg.getChildByName('nvSpine')
            let nvSpine2 = this.maskBg.getChildByName('nvSpine')
            nvSpine.getComponent(sp.Skeleton).setAnimation(0,"guiyidaiji",true)
            nvSpine2.getComponent(sp.Skeleton).setAnimation(0,"guiyidaiji",true)
        }else if(nameStr == "GuHuiHe"){
            let nanSpine = this.bg.getChildByName('nanSpine')
            let nanSpine2 = this.maskBg.getChildByName('nanSpine')
            nanSpine.getComponent(sp.Skeleton).setAnimation(0,"guhuihedaiji",true)
            nanSpine2.getComponent(sp.Skeleton).setAnimation(0,"guhuihedaiji",true)
        }
    }
    //非茬点处理
    handFeiChaDianEvent(pos){
        console.log("handFeiChaDianEvent子类==")
        //非茬点触碰逻辑
        if(!this.bg.getChildByName("feiChaDianNode")){
            return
        }

        let feiChaDianChildrens = this.bg.getChildByName("feiChaDianNode").children
        for(var i =0;i<feiChaDianChildrens.length;i++){ 
            //可以触摸且未触发过
            if(this.chaDianConfig.FeiChanDian[feiChaDianChildrens[i].name] && this.chaDianConfig.FeiChanDian[feiChaDianChildrens[i].name].isCanTouch && !this.chaDianConfig.FeiChanDian[feiChaDianChildrens[i].name].isTouchOff){
                let rect = feiChaDianChildrens[i].getBoundingBoxToWorld()
                if(rect.contains(pos)){
                    // this.chaDianConfig.FeiChanDian[feiChaDianChildrens[i].name].isTouchOff = true
                    //男生手机
                    if(feiChaDianChildrens[i].name == "nanShengShouJiTouchArea"){
                        let nanShouJiBoxNode = this.node.getChildByName("nanShouJiBoxNode")
                        nanShouJiBoxNode.active = true
                        this.fangdajing.active = false
                    //女生手机
                    }else if(feiChaDianChildrens[i].name == "nvShengShouJiTouchArea"){
                        let nvShouJiBoxNode = this.node.getChildByName("nvShouJiBoxNode")
                        nvShouJiBoxNode.active = true
                        this.fangdajing.active = false
                    //广告牌
                    }else if(feiChaDianChildrens[i].name == "guangGaoPaiTouchArea"){
                        Common5.playEffectCustom(this.bundle,this.chaDianConfig.soundurl + "新闻报道")
                        let xinWenBoxNode = this.node.getChildByName("xinWenBoxNode")
                        xinWenBoxNode.active = true
                        this.fangdajing.active = false
                    }
                }
            }
        }  
    }
    //背景图片的点击与移动
    protected override bgTouchEnd(event: cc.Event.EventTouch){
        if(Date.now() - this.touchStartTime > 0.2 * 1000){

        }else{
            let touchPos = event.getLocation(); 
            // let rect = this.maskBg.parent.getChildByName('rect').getBoundingBoxToWorld(); 
            // if(rect.contains(touchPos)){
                this.checkIsInAreaNewVersion(touchPos) 
            // } 
        }
    }
    protected override showDialogue(soundUrl){
        if(!soundUrl){
            return
        }
        Common5.playEffectCustom(this.bundle,this.chaDianConfig.soundurl + soundUrl)
        let dialog = this.node.getChildByName("duiHuaDi").getChildByName("dialog")
        dialog.getChildByName("dialogStr").getComponent(cc.Label).string = soundUrl
        dialog.stopAllActions()
        dialog.active = true
        dialog.scale = 0
        cc.tween(dialog)
            .to(0.2,{scale:1})
            .delay(2.8)
            .to(0.2,{scale:0})
            .call(()=>{
                dialog.active = false
            })
        .start()
    }
    // 判断是否找到茬点
    protected override checkIsInAreaNewVersion(touchPos){
        let rect = this.maskBg.parent.getChildByName('rect').getBoundingBoxToWorld(); 
        let containsFdj = rect.contains(touchPos)
        let childrens = this.chaDianNode.children 
        for(var i =0;i<childrens.length;i++){  
            let rect = childrens[i].getBoundingBoxToWorld()
            if(rect.contains(touchPos)){
                if(this.chaDianConfig.ChaNode[childrens[i].name].needFDJ && containsFdj){
                    if(this.chaDianConfig.ChaNode[childrens[i].name].isFindOut){ 
                        continue
                    }else{
                        //暂不可点击
                        if(!this.chaDianConfig.ChaNode[childrens[i].name].isCanTouch){ 
                            continue
                        }
                        
                        this.chaDianConfig.ChaNode[childrens[i].name].isFindOut = true 
                        //高压锅
                        if(childrens[i].name == "GaoYaGuo"){
                            this.gaoYaGuoEvent()
                            return
                        }
                        this.findOnChaDian(childrens[i])
                        
                    }
                    break
                }else if(!this.chaDianConfig.ChaNode[childrens[i].name].needFDJ && !containsFdj){
                    if(this.chaDianConfig.ChaNode[childrens[i].name].isFindOut){ 
                        continue
                    }else{
                        //暂不可点击
                        if(!this.chaDianConfig.ChaNode[childrens[i].name].isCanTouch){ 
                            continue
                        }
                        
                        this.chaDianConfig.ChaNode[childrens[i].name].isFindOut = true 
                        this.findOnChaDian(childrens[i])
                        this.feiFDJFindMardkEffect(childrens[i])
                    }
                    break
                }  
            }
        }  
        this.handFeiChaDianEvent(touchPos)
    }
    //非放大镜找到茬点mark效果
    feiFDJFindMardkEffect(childNode){
        let mark = cc.instantiate(this.maskSp)
        mark.active = true
        mark.opacity  = 0
        this.node.addChild(mark) 
        Common5.setNodeToTargetPos(mark,childNode)
        cc.tween(mark)
            .to(0.5,{opacity:255})
            .delay(1)
            .call(()=>{
                mark.removeFromParent()
            })
        .start()
        
    }
    onBtnChengJiaClick(){
        this.refreshDiJiaNum()
        if(this._curFindChaDianNum == 10){
            Common5.playEffectCustom(this.bundle,this.chaDianConfig.soundurl + "又成功破了一案")
            this.showDialogue("又成功破了一案")
            this.scheduleOnce(()=>{
                Game.ins.showSuccess();
            },2)
        }else{
            Common5.playEffectCustom(this.bundle,this.chaDianConfig.soundurl + "哪里的线索漏掉了吗")
            this.showDialogue("哪里的线索漏掉了吗")
            this.scheduleOnce(()=>{
                Game.ins.showFail();
            },2)
        }
    }
    gaoYaGuoEvent(){
        Common5.playEffectCustom(this.bundle,this.chaDianConfig.soundurl + "爆炸")
        let baoZhaNode = this.node.getChildByName("baoZhaNode")
        baoZhaNode.active = true
        let baozhaSpine = baoZhaNode.getChildByName("baozhaSpine")
        baozhaSpine.getComponent(sp.Skeleton).setAnimation(0,"baozha",false)
        let zhogbiaoSpine = baoZhaNode.getChildByName("zhogbiaoSpine")
        zhogbiaoSpine.getComponent(sp.Skeleton).setAnimation(0,"zhongbiao",false)
        this.scheduleOnce(()=>{
            // cc.director.loadScene("WordScene");
            Game.ins.showFail();
        },5.2)
    }
}