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 GameReport from "../../../../FrameWork/Report/ZyZyReport";
import WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
const {ccclass, property} = cc._decorator;

enum YeYeStatus{
    xuehua,
    beixin,
    mianfu,
}

enum DoorStatus{
    open,
    close
}
// "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
    '上划美女露出手机,点击手机后将钱拖动到手机界面处',//11
    '将钱拖动到墙角的衣服处',//12
] 




let CurGameConfig = {
    CheckNode:[
       '破旧窗户',
       'nvren_check',
       'nvren_check2',
       '老婆婆check',
       '床板1',
       '灶台',
       '烤火炉',
       'mencheck',
       '黑社会大哥check',
       '衣服',
       '馒头',
       '手机前',
       '地面', 
       '破旧墙壁',
 
    ]
}
@ccclass
export default class PingKuRenJia 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
    isKaiChuang:boolean = false

    YeYeCurStatus = YeYeStatus.xuehua
    yeyeMove = false
    yeyeAnmo: boolean = false;

    nainaiIsTang = false
    isAnmoNan: boolean = false;

    curDoorStatus = DoorStatus.close
    doorGetMoney = false

    chadianMax = 13
    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);




    }
    start(){
        super.start();
        cc.macro.ENABLE_MULTI_TOUCH = true;
        this.initMapNodeTouchEvent()
        Common5.playMusicCustom('pingkurenjia','sound/背景音')

        
        EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
            if(data_.targetNode.name == 'menTouchcheck'){
                this.curDoorStatus = DoorStatus.open
                if(this.doorGetMoney){
                    this.mapNode.getChildByName('破旧门开').active = false
                    this.mapNode.getChildByName('豪华门开').active = true
                }

                Common5.playEffectCustom('pingkurenjia','sound/快还钱')
                this.scheduleOnce(()=>{
                    this.mapNode.getChildByName('气泡框1').active = false   
                },1.0)
            }
        }, this);


        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            
        }, this);

        // 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){
    
        }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){
     
    }

    
    //特殊节点的点击事件(主要处理节点可以移动的问题)
    specialNodeTouchEvent(){
        let qianbiNodes = this.mapNode.getChildByName("qianbiNodes")
        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)
    }


    // CheckNode:[
    //     '黑社会大哥',
    //     '破旧窗户',
    //     'nvren_check',
    //     'nvren_check2',
    //     '老婆婆',
    //     '床板1',
    //     '灶台',
    //     '烤火炉',
    //     '破旧门关',
    //     '衣服',
    //     '馒头',
    //     '手机前',
    //     '地面', 
    //     '破旧墙壁',
  
    //  ]

    // let answer = [
    //     '将钱拖动奶奶身上', //0
    //     '将钱拖动奶奶睡的棺材',//1
    //     '将钱拖动到墙边的火炉',//2
    //     '将钱拖动到地上',//3
    //     '将钱拖到墙上',//4
    //     '将钱拖动窗户',//5
    //     '将钱拖动到门上',//6
    //     '上划门后,将钱拖给门外的人',//7
    //     '将钱拖动到灶台',//8
    //     '上划灶台上的蒸锅露出馒头,将钱拖动到馒头上',//9
    //     '将钱拖动到美女上',//10
    //     '上划美女露出手机,点击手机后将钱拖动到手机界面处',//11
    //     '将钱拖动到墙角的衣服处',//12
    // ] 


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

       
        
        if (checkTarget) {
            this.curFinishNum ++

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



            console.log(checkTarget.name, 'checkTarget.name+++++====')
            target.active = false
            checkTarget.active = false
            if(checkTarget.name == '黑社会大哥check'){
                // this.mapNode.getChildByName('黑社会大哥').active = true
                this.unlockLevel(7)
                this.mapNode.getChildByName('黑社会大哥').getComponent(sp.Skeleton).setAnimation(0, '后', true)
                this.mapNode.getChildByName('气泡框11').active = true   
                Common5.playEffectCustom('pingkurenjia','sound/社区送温暖哦')

                this.scheduleOnce(()=>{
                    this.curDoorStatus = DoorStatus.close

                    this.mapNode.getChildByName('气泡框11').active = false   

                    if(this.doorGetMoney == true){
                        this.mapNode.getChildByName('破旧门开').active = false
                        this.mapNode.getChildByName('豪华门开').active = false
                        this.mapNode.getChildByName('豪华门关').active = true
                    }else{
                        this.mapNode.getChildByName('破旧门开').active = false
                        this.mapNode.getChildByName('豪华门开').active = false
                        this.mapNode.getChildByName('破旧门关').active = true   
                    }
                },1.0)
            }else if(checkTarget.name == '破旧窗户'){
                this.unlockLevel(5)
                this.mapNode.getChildByName('新窗户').active = true
                this.showQiPao('不会吹风进来了')

            }else if(checkTarget.name == 'nvren_check' || checkTarget.name == 'nvren_check2' ){
                this.showQiPao('新衣服好看吗')
                this.mapNode.getChildByName('nvren_check2').active = false
                this.mapNode.getChildByName('nvren_check').active = false
                this.mapNode.getChildByName('女').getComponent(sp.Skeleton).setAnimation(0, '后', true)
                this.mapNode.getChildByName('nvren_click').active = true
                this.unlockLevel(10)
            }else if(checkTarget.name == '老婆婆check'){
                this.showQiPao('给奶奶买件棉衣')
                // this.mapNode.getChildByName('老婆婆').active = true
                this.unlockLevel(0)
                this.mapNode.getChildByName('老婆婆').getComponent(sp.Skeleton).setAnimation(0, '后', true)
            }else if(checkTarget.name == '床板1'){
                this.unlockLevel(1)
                this.mapNode.getChildByName('床板1').active = false
                this.mapNode.getChildByName('床板2').active = false
                this.mapNode.getChildByName('现代床').active = true
                this.showQiPao('床暖和一些')
            }else if(checkTarget.name == '灶台'){
                this.unlockLevel(8)
                this.mapNode.getChildByName('馒头2').active = true
                this.mapNode.getChildByName('锅盖').active = true
                this.mapNode.getChildByName('现代厨柜').active = true
                this.showQiPao('这灶台才适合我的气质')
            }else if(checkTarget.name == '烤火炉'){
                this.unlockLevel(2)
                this.mapNode.getChildByName('空调').active = true
                this.showQiPao('空调才是最暖和的')
            }else if(checkTarget.name == 'mencheck'){
                this.unlockLevel(6)
                this.showQiPao('防盗门安全')
                this.doorGetMoney = true
                if(this.curDoorStatus == DoorStatus.open){
                    this.mapNode.getChildByName('豪华门开').active = true
                    this.mapNode.getChildByName('破旧门开').active = false
                }else{
                    this.mapNode.getChildByName('豪华门关').active = true
                    this.mapNode.getChildByName('破旧门关').active = false
                }
            }else if(checkTarget.name == '衣服'){
                this.unlockLevel(12)
                this.mapNode.getChildByName('洗衣机').active = true
           
                this.showQiPao('家电都得上全自动的')
            }else if(checkTarget.name == '馒头'){
                this.unlockLevel(9)
                this.mapNode.getChildByName('龙虾').active = true
                this.showQiPao('我这身份怎么能吃馒头')
            }else if(checkTarget.name == '手机前'){
                this.unlockLevel(11)
                this.mapNode.getChildByName('手机后').active = true
             
                this.scheduleOnce(()=>{
                    this.mapNode.getChildByName('手机后').active = false
                    //Common5.playEffectCustom('pingkurenjia','sound/别误会,他们只是朋友')
                    this.showQiPao('别误会,他们只是朋友')
                    // this.mapNode.getChildByName('气泡框2').active = true
                    // this.scheduleOnce(()=>{
                    //     this.mapNode.getChildByName('气泡框2').active = false
                    // },1.5)
                },1.5)
            }else if(checkTarget.name == '地面'){
                this.unlockLevel(3)
                this.mapNode.getChildByName('木地板').active = true
                this.showQiPao('木地板舒服')
            }else if(checkTarget.name == '破旧墙壁'){
                this.showQiPao('粉粉的墙很适合我')
                this.unlockLevel(4)
                this.mapNode.getChildByName('新墙壁').active = true
               
            }

            

            this.checkIsFinish()
        }else{
            event.target.setPosition(event.target.startPos)
        }
    }

    showQiPao(str){
        let qipaoNode = this.mapNode.getChildByName('气泡框2')
        qipaoNode.scale = 0
        qipaoNode.active = true
        qipaoNode.stopAllActions()
        qipaoNode.getChildByName("lab").getComponent(cc.Label).string = str
        if (str) {
            Common5.playEffectCustom("pingkurenjia", "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.curFinishNum >= this.chadianMax){
            console.log('结算+++===')
            this.scheduleOnce(()=>{
                if(this.curFinishNum >= this.chadianMax){
                    if(this.isStepRight){
                        Game.ins.showSuccess()
                    }else{
                        Game.ins.showFail()
                    }
                }
            },2)
        }
        
    }


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