import EventMgr from "../../../../FrameWork/Event/EventMgr";
import { ryw_Event } from "../../../../FrameWork/Event/EventEnum";
import Common5 from "../../../../Platform/th/Common5";
import Common from "../../../../FrameWork/Util/Common";
import DaDianScript from "../../../../FrameWork/Base/DaDianScript";
import GameReport from "../../../../FrameWork/Report/ZyZyReport";
import WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
const {ccclass, property} = cc._decorator;

enum NaiNaiStatus{
    xuehua,
    beixin,
    mianfu,
}


// "toolTip": "1.将钱拖动到三轮车\n2.将钱拖动到柴火堆",
// "answer": "n\n\n7.将爷爷拖动到垃圾桶上\n8.爷爷拖动到垃圾桶上之后,再将钱拖动到垃圾桶上\n\n\n\n\n13.将钱拖动到右墙\n14.将钱拖动到墙上的海报\n15.将钱拖动到老鼠洞\n16.将钱拖动到鸡身上",


let answer = [
    '将钱拖动到老太太上', //0
    '将钱拖动到木桶上',//1
    '将钱拖动到邮轮旁挂着的帅哥照片上',//2
    '将钱拖动到服务员托盘的雷碧上',//3
    '将钱拖到邮轮上的桌子上',//4
    '将钱拖到邮轮旁的栏杆上',//5
    '将钱拖动破烂救生圈上',//6
    '将钱拖动到鲨鱼身上',//7
    '再次将钱拖动到鲨鱼身上',//8
    '将钱拖动到海洋中',//9
    '将钱拖动到爷爷身上',//10
] 



let CurGameConfig = {
    CheckNode:[
       'ck_daYe',
       'ck_zhuoZi',
       '游泳圈',
       'ck_muTong',
       'ck_muTongLaoTai',
       'ck_chuanLaoTaiZuo',
       'ck_sheDeng',
       '海报',
       'ck_shaYu1',
       'ck_shaYu2',
       'ck_雷碧',
       'ck_海面',
    ],


}
@ccclass
export default class LuoNanLaoTai extends WordGameBaseComponent{
    @property(cc.Node)
    maskNode:cc.Node = null;  
    
    @property(cc.Node)
    mapNode: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
    isStepRight:boolean = true
    
    isNaiNaiTiaoWu:boolean = false
    isBianChuan:boolean = false
    onLoad(){
        DaDianScript.userEnterDaDian()
        Common.Type = 3;
        Common.subLevel = 0;
        Common.GameSubTipConfigs=answer//[Common5.gameConfig.zmGameConfig[Common5.selectGameNum].toolTip]
        // Common.GameSubAnswerConfigs=[Common5.gameConfig.zmGameConfig[Common5.selectGameNum].answer]


      
        this.bundle = Common5.gameConfig.zmGameConfig[Common5.selectGameNum].bundle
        EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
            Game.ins.showFail();; 
        }, this);
        // EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
        //     if(data_.targetNode.name == 'ck_大爷空中'){
        //         if(this.curFinishNum == 11){
        //             this.showDuiHua('哎呀,谁又把我拉回来了')
        //             cc.tween(this.mapNode.getChildByName('大爷空中spine'))
        //                 .to(0.6,{y:300})
        //                 .call(()=>{
        //                     this.mapNode.getChildByName('ck_大爷落下').active = true
        //                 })
        //             .start()
        //         }else{
        //             data_.targetNode.active = true
        //         }
        //     }
        // }, this);
    }
    start(){
        super.start();
        cc.macro.ENABLE_MULTI_TOUCH = true;
        this.initMapNodeTouchEvent()
        // Common5.playMusicCustom('qiaoxialaoren','sound/背景音')
        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

    }

    normalTouchCallback(targetNode){
        
    }

    //初始化触摸监听
    initMapNodeTouchEvent(){
        this.initMouseEvent();
        this.mapNode.on(cc.Node.EventType.TOUCH_START, this.touchStart_mapNode,this)
        this.mapNode.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove_mapNode,this)
        this.mapNode.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd_mapNode,this)
        this.mapNode.on(cc.Node.EventType.TOUCH_END, this.touchEnd_mapNode,this)

        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.specialNodeTouchEvent()
    }
    touchStart_mapNode(event){
        Common5.playEffect("click")
        if(this.touchId1 < 0){
            this.touchId1 = event.touch.getID()
        }else if(this.touchId2 < 0){
            this.touchId2 = event.touch.getID()
        }
        if(this.touchId1 >= 0 && this.touchId2 >= 0){
            // console.log("邵阳1—双点触摸开始")
        }

        this.touchStartTime = Date.now()
    }
    touchMove_mapNode(event){
        let touches = event.getTouches()
        if(touches.length == 2){ //缩放
            if(this.originalTouchDistance == 0){
                this.originalTouchDistance = this.getDistance(touches[0].getLocation(),touches[1].getLocation())
            }

            //获取双指移动数据
            let curDistance = this.getDistance(touches[0].getLocation(),touches[1].getLocation())
            let tempScale = curDistance/this.originalTouchDistance
            let y = 0.5*tempScale + 0.5
            let scale = y * this.lastScale
            
            if(scale < this.mapNodeFixedScale){
                scale = this.mapNodeFixedScale
            }else if(scale > 3){
                scale = 3
            }else{
            }
            this.mapNode.scale = scale
            this.curScale = scale
            this.goToBoundary();
        }else if(touches.length == 1){ //拖动
            let delta = event.getDelta()
            this.mapNode.x += delta.x
            this.mapNode.y += delta.y
            // console.log(this.mapNode.scale,this.mapNode.x,this.mapNode.y);

            this.goToBoundary()
        }
    }
    touchEnd_mapNode(event){
        console.log('touchEnd===')
        let dateNow = Date.now()
        if(dateNow - this.touchStartTime > 0.2 * 1000){
            // console.log('0.2===')
            // //补丁,长按两秒
            // if(dateNow - this.touchStartTime > 1.5 * 1000){
            //     console.log('2===')
            //     let wl_fangzi = this.mapNode.getChildByName('wl_房子')
            //     let chuanghuguan = wl_fangzi.getChildByName('窗户关')
            //     let chuanghukai = wl_fangzi.getChildByName('窗户开')
            //     let rect = chuanghuguan.getBoundingBoxToWorld()
            //     if(wl_fangzi.active && chuanghuguan.active && rect.contains(event.getLocation())){
            //         chuanghuguan.active = false
            //         chuanghukai.active = true
            //         this.isKaiChuang = true
            //     }
            // }
        }else{
            // this.isFind = false;
            let touchPos = event.getLocation();
            this.checkIsInAreaNewVersion(touchPos)
        }

        if(event.touch.getID() == this.touchId1){
            this.touchId1 = -1
        }else if(event.touch.getID() == this.touchId2){
            this.touchId2 = -2
        }

        if(this.touchId1 < 0 && this.touchId2 < 0){
            this.originalTouchDistance = 0
            this.lastScale = this.curScale
            // this.goToBoundary()
        }
    }
    //距离
    getDistance(startPos,endPos){
        var pos = cc.v2(startPos.x - endPos.x,startPos.y - endPos.y)
        var dis = Math.sqrt(pos.x * pos.x + pos.y * pos.y)
        return dis
    }
    //检测边界
    goToBoundary(){
        let widthMask = this.maskNode.width
        let heightMask = this.maskNode.height

        let widthMap = this.mapNode.width*this.curScale
        let heightMap = this.mapNode.height*this.curScale

        //右边界
        if(this.mapNode.x + widthMap/2 <= widthMask/2){
            this.mapNode.x = widthMask/2 - widthMap/2
        //左边界
        }else if(this.mapNode.x - widthMap/2>= -widthMask/2){
            this.mapNode.x = -widthMask/2 + widthMap/2
        }
        //上边界
        if(this.mapNode.y + heightMap/2 <= heightMask/2){
            this.mapNode.y = heightMask/2 - heightMap/2
        //下边界
        }else if(this.mapNode.y - heightMap/2 >= -heightMask/2){
            this.mapNode.y = -heightMask/2 + heightMap/2
        }  
        
    }
    initMouseEvent(){
        this.mapNode.on(cc.Node.EventType.MOUSE_WHEEL,function(event){//监听名称+事件参数
            console.log(event);  
            let tmpscale = this.mapNode.scale;              
            if (event._scrollY > 0){
                if (this.mapNode.scale < 3){
                    tmpscale+=0.2;
                    this.mapNode.scale = tmpscale           
                    this.curScale = tmpscale
                    this.lastScale = tmpscale
                }
            }
            else {
                if (this.mapNode.scale > this.mapNodeFixedScale){
                    tmpscale-=0.2;
                    this.mapNode.scale = tmpscale        
                    this.curScale = tmpscale
                    this.lastScale = tmpscale
                }
            }
            // console.log(this.mapNode.scale,this.mapNode.x,this.mapNode.y);
            this.goToBoundary();
        },this);
        
    }
    //检测node是否进入target区域(新版)
    checkIsInAreaNewVersion(pos){
        // //茬点触碰逻辑
        // let childrens = this.mapNode.getChildByName("chaDianNode").children 
        // for(var i =0;i<childrens.length;i++){ 
        //     let rect = childrens[i].getBoundingBoxToWorld()
        //     if(rect.contains(pos)){
        //         if(this.gameInfo.ChaNode[childrens[i].name].isFindOut){ 
        //             continue
        //         }else{
        //             //暂不可点击
        //             if(!this.gameInfo.ChaNode[childrens[i].name].isCanTouch){ 
        //                 continue
        //             }
        //             this.gameInfo.ChaNode[childrens[i].name].isFindOut = true 
        //             if (this.gameInfo.PrefabInfo.zhaoChaType == ZhaoChaType.Type6){
        //                 this.findOnChaDian(childrens[i],this.gameId)
        //             }
        //             else {
        //                 this.findOnChaDian(childrens[i])
        //             }
        //             this.isFind = true
        //         }
        //         break
        //     }else{
        //         //点错这里需要减5s 
        //     }
        // }  
        // this.handSpecialGameLogic(pos)
    }

    
    //特殊节点的点击事件(主要处理节点可以移动的问题)
    specialNodeTouchEvent(){
        let qianbiNodes = this.mapNode.getChildByName("qianNode")
        for(let i=0;i<qianbiNodes.childrenCount;i++){
            this.openTouchEvent(qianbiNodes.children[i],CurGameConfig.CheckNode)
        }

    }
    openTouchEvent(node, checkNodeTab){
        var attrs = {
            isCanMove:true,
            checkNodeTab: checkNodeTab,
            startPos: node.getPosition(),//初始位置
        };
        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)
    }

    // let answer = [





 

    // ] 

    touchEndSpecicalNode(event){
        // event.target.zIndex = 0 
        
        let target = event.target;
        let checkTarget = this.checkArea(target,target.checkNodeTab)
        if (checkTarget) {

            GameReport.BtnsReport(checkTarget.name, Common5.selectGameInfo.titleUrl);

            
            this.curFinishNum ++

            console.log(checkTarget.name, 'checkTarget.name+++++====')
            target.active = false
            checkTarget.active = false


            if(checkTarget.name == 'ck_daYe'){
                this.unlockLevel(10)
                Common5.playEffectCustom(this.bundle, 'sound/老大爷起飞');
                cc.tween(this.mapNode.getChildByName('大爷空中spine'))
                    .set({active:true})
                    .delay(0.8)
                    .call(()=>{
                        
                    })
                    .to(1.5,{y:1500,scale:0.6})
                    .call(()=>{
                        this.mapNode.getChildByName('ck_大爷空中').active = true
                    })
                .start()
                this.mapNode.getChildByName('大爷水spine').active = false
                
            }else if(checkTarget.name == 'ck_海面'){
                this.unlockLevel(9)
                this.showDuiHua('这水质才干净')
                this.mapNode.getChildByName('污染背景').active = false
                this.mapNode.getChildByName('干净背景').active = true
                
            }else if(checkTarget.name == 'ck_shaYu1'){
                this.unlockLevel(7)
                this.showDuiHua('鱼儿真可怜,给它个游泳圈')

                this.mapNode.getChildByName('鲨鱼spine').getComponent(sp.Skeleton).setAnimation(0,'正常',true)
                this.mapNode.getChildByName('ck_shaYu2').active = true
                
            }else if(checkTarget.name == 'ck_shaYu2'){
                this.unlockLevel(8)
                this.showDuiHua('小鱼儿你可要多吃点')

                this.mapNode.getChildByName('鲨鱼spine').getComponent(sp.Skeleton).setAnimation(0,'金牙',true)
                
            }else if(checkTarget.name == 'ck_muTongLaoTai'){
                this.unlockLevel(0)
                this.showDuiHua('每天准时跳科目三')
                Common5.playMusicCustom(this.bundle,'sound/科目三老太太跳舞')
                this.mapNode.getChildByName('老太木桶spine').getComponent(sp.Skeleton).setAnimation(0,'木桶跳舞',true)
                this.isNaiNaiTiaoWu = true

            }else if(checkTarget.name == 'ck_muTong'){
                this.unlockLevel(1)
                this.showDuiHua('豪华大邮轮真舒服啊')
                Common5.playEffectCustom(this.bundle, 'sound/游轮出场');
                this.mapNode.getChildByName('老太木桶spine').active = false
                this.mapNode.getChildByName('ck_muTongLaoTai').active = false
                this.isBianChuan = true


                this.mapNode.getChildByName('船').active = true
                this.mapNode.getChildByName('餐桌').active = true
                this.mapNode.getChildByName('ck_zhuoZi').active = true
                this.mapNode.getChildByName('海报').active = true
                this.mapNode.getChildByName('游泳圈').active = true
                this.mapNode.getChildByName('老太船spine').active = true
                if(this.isNaiNaiTiaoWu){
                    this.mapNode.getChildByName('老太船spine').y = 80
                    this.mapNode.getChildByName('老太船spine').getComponent(sp.Skeleton).setAnimation(0,'跳舞',true)
                }else{
                    this.mapNode.getChildByName('老太船spine').y = 140
                    this.mapNode.getChildByName('老太船spine').getComponent(sp.Skeleton).setAnimation(0,'坐',false)
                    this.mapNode.getChildByName('ck_chuanLaoTaiZuo').active = true
                }
                this.mapNode.getChildByName('ck_sheDeng').active = true
                this.mapNode.getChildByName('围栏').active = true
            }else if(checkTarget.name == 'ck_chuanLaoTaiZuo'){
                this.unlockLevel(0)
                this.showDuiHua('每天准时跳科目三')
                Common5.playMusicCustom(this.bundle,'sound/科目三老太太跳舞')

                this.mapNode.getChildByName('老太船spine').y = 80
                this.mapNode.getChildByName('老太船spine').getComponent(sp.Skeleton).setAnimation(0,'跳舞',true)

            }else if(checkTarget.name == 'ck_zhuoZi'){
                this.unlockLevel(4)
                this.showDuiHua('吃好喝好')

                this.mapNode.getChildByName('海鲜').active = true
            }else if(checkTarget.name == 'ck_sheDeng'){
                this.unlockLevel(5)
                this.showDuiHua('氛围感搞起来')

                this.mapNode.getChildByName('射灯').active = true
            }else if(checkTarget.name == '游泳圈'){
                this.unlockLevel(6)
                this.showDuiHua('上厕所用这个才舒服')

                this.mapNode.getChildByName('黄金马桶').active = true
            }else if(checkTarget.name == '海报'){
                this.unlockLevel(2)
                this.showDuiHua('为美丽的小姐服务')

                this.mapNode.getChildByName('服务员').active = true
                this.mapNode.getChildByName('雷碧').active = true
                this.mapNode.getChildByName('ck_雷碧').active = true
            }else if(checkTarget.name == 'ck_雷碧'){
                this.unlockLevel(3)
                this.showDuiHua('82年的拉飞才解渴嘛')

                this.mapNode.getChildByName('红酒').active = true
                this.mapNode.getChildByName('雷碧').active = false
            }else if(checkTarget.name == 'ck_大爷落下'){
                // this.unlockLevel(11)
                // this.showDuiHua('看我家老太太跳舞真舒服啊')

                // this.mapNode.getChildByName('大爷空中spine').active = false
                // this.mapNode.getChildByName('沙滩椅').active = true
                // this.mapNode.getChildByName('大爷躺下').active = true
            }


            this.checkIsFinish()
        }else{
            event.target.setPosition(event.target.startPos)
        }
    }
    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.curFinishNum >= 11){
            console.log('结算+++===')
            if(this.isStepRight){
                cc.tween(this.mapNode.getChildByName('大爷空中spine'))
                    .delay(1)
                    .to(1,{y:300})
                    .call(()=>{
                        this.showDuiHua('嘿嘿,我又回来了')
                    })
                    .delay(1.5)
                    .call(()=>{
                        this.mapNode.getChildByName('大爷空中spine').active = false
                        this.mapNode.getChildByName('沙滩椅').active = true
                        this.mapNode.getChildByName('大爷躺下').active = true
                        this.scheduleOnce(()=>{
                            Game.ins.showSuccess()
                        },1.5)
                    })
                .start()
            }else{
                Game.ins.showFail()
            }
        }
        
    }


    unlockLevel(sublevel){
        Game.ins.tipUnlock(sublevel)
    }
    showDuiHua(str, func?) {

        console.log("str==", str)
        if (!str || str == '') {
            return;
        }

        Common5.playEffectCustom(this.bundle, 'sound/'+str);
        let qiPao = this.node.getChildByName("duihua")
        qiPao.stopAllActions()
        qiPao.getChildByName("tabLab2").getComponent(cc.Label).string = str
        qiPao.active = true
        qiPao.scale = 0
        cc.tween(qiPao)
            .to(0.2, { scale: 1 })
            .delay(3)
            .call(() => {
                qiPao.active = false;
                if (func) {
                    func();
                }
            })
            .start();
    }
}