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

const {ccclass, property} = cc._decorator;

@ccclass
export default class ZhaoChaV3Base extends cc.Component {
 
    @property({type:cc.Node,tooltip:'时间节点位置'})
    protected timeNode:cc.Node = null; 

    @property({type:cc.Node,tooltip:'最外mask'})
    protected maskNode:cc.Node = null; 
    
    @property({type:cc.Node,tooltip:'茬点找到的masksp'})
    protected maskSp:cc.Node = null; 
    
    @property({type:cc.Node,tooltip:'可视的背景图片'})
    protected bg:cc.Node = null; 
    
    @property({type:cc.Node,tooltip:'使用镜子后可见的背景图片'})
    protected maskBg:cc.Node = null; 
    
    @property({type:cc.Node,tooltip:'镜子'})
    protected fangdajing:cc.Node = null; 
    
    @property({type:cc.Node,tooltip:'茬点节点集合'})
    protected chaDianNode:cc.Node = null; 

    bundle = null
    subUrl = null
    protected handFeiChaDianEvent(touchPos){
        console.log("handFeiChaDianEvent基类==")
    }
    
    protected chaDianConfig = {
        ChaNode:{},
        FeiChanDian:{},
        soundurl:'',
        totalMoney:0,
        minMoney:0
    }

    // 找到茬点后的回调函数
    protected afterChaDianFindCallback(nameStr){
        console.log("afterChaDianFindCallback基类==")
    }   

    //特殊节点的点击事件(主要处理节点可以移动的问题)
    protected specialNodeMoveEvent(){
        console.log("afterChaDianFindCallback基类==")
    } 
    // 找到茬点前的回调函数
    protected beforeChaDianFindCallback = null;
    protected leftBaoJiaMoney:number = 0
    protected diJiaMoney:number = 0

    onLoad () {
        console.log('base onload')
        Common.Type = 2;
        Common.subLevel = 0;
        Common.GameSubTipConfigs=[Common5.gameConfig.zmGameConfig[Common5.selectGameNum].toolTip]
        this.bundle = Common5.gameConfig.zmGameConfig[Common5.selectGameNum].bundle
        this.subUrl = Common5.gameConfig.zmGameConfig[Common5.selectGameNum].suburl
    }
    

    start () {
        this.leftBaoJiaMoney = this.chaDianConfig.totalMoney
        this.diJiaMoney = this.chaDianConfig.minMoney
        console.log('base start this.leftBaoJiaMoney',this.leftBaoJiaMoney)
        this.node.getChildByName("duiHuaDi").getChildByName("baoJiaStr").getComponent(cc.Label).string = this.leftBaoJiaMoney + "元"

        EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
            Game.ins.showFail(); 
        }, this);
        this.setTimeNode(); 

        this.openNodeTouch(this.fangdajing, null, this.touchMove, null) 
        this.openNodeTouch(this.bg, this.bgTouchStart, this.bgTouchMove, this.bgTouchEnd) 
        this.specialNodeMoveEvent()
    }

    protected getEventTargetLoc(event){
        let nodeLoc = event.getLocation()
        let nodePos = event.target.parent.convertToNodeSpaceAR(nodeLoc)   
        return nodePos
    }

    //放大镜的位置移动  
    protected touchMove(event: cc.Event.EventTouch){    
        event.target.setPosition(this.getEventTargetLoc(event));    
        this.goToBoundaryFDJ()
        let pos = Common5.getRelativePosition(this.bg, this.maskBg.parent);
        this.maskBg.setPosition(pos);
    } 

    //背景图片的点击
    protected touchStartTime = Date.now();
    protected bgTouchStart(event: cc.Event.EventTouch){ 
        Common5.playEffect("click")
        this.touchStartTime = Date.now();
    }
    
    //背景图片的点击与移动
    protected 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 bgTouchMove(event: cc.Event.EventTouch){   
        let delta = event.getDelta()
        this.bg.x += delta.x 
        this.goToBoundaryBg()
        let pos = Common5.getRelativePosition(this.bg, this.maskBg.parent);
        this.maskBg.setPosition(pos);
    } 
    //检测边界(背景)
    protected goToBoundaryBg(){
        let widthMask = this.maskNode.width
        //let heightMask = this.maskNode.height

        let widthMap = this.bg.width * 1
        //let heightMap = this.bg.height * 1

        //右边界
        if(this.bg.x + widthMap/2 <= widthMask/2){
            this.bg.x = widthMask/2 - widthMap/2
        //左边界
        }else if(this.bg.x - widthMap/2>= -widthMask/2){
            this.bg.x = -widthMask/2 + widthMap/2
        }
        // //上边界
        // if(this.bg.y + heightMap/2 <= heightMask/2){
        //     this.bg.y = heightMask/2 - heightMap/2
        // //下边界
        // }else if(this.bg.y - heightMap/2 >= -heightMask/2){
        //     this.bg.y = -heightMask/2 + heightMap/2
        // }  
        
    }
    //检测边界(放大镜)
    protected goToBoundaryFDJ(){
        let widthMask = this.maskNode.width
        let heightMask = this.maskNode.height

        //右边界
        if(this.fangdajing.x >= widthMask/2 -5){
            this.fangdajing.x = widthMask/2 -5
        //左边界
        }else if(this.fangdajing.x <= -widthMask/2 + 100 ){
            this.fangdajing.x = -widthMask/2 + 100
        }
        //上边界
        if(this.fangdajing.y >= heightMask/2 - 100){
            this.fangdajing.y = heightMask/2 - 100
        //下边界
        }else if(this.fangdajing.y <= -heightMask/2 + 5){
            this.fangdajing.y = -heightMask/2 + 5
        }  
        
    }
    // 判断是否找到茬点
    protected checkIsInAreaNewVersion(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].isFindOut){ 
                    continue
                }else{
                    //暂不可点击
                    if(!this.chaDianConfig.ChaNode[childrens[i].name].isCanTouch){ 
                        continue
                    }
                    
                    this.chaDianConfig.ChaNode[childrens[i].name].isFindOut = true 
                    this.findOnChaDian(childrens[i])
                    
                }
                break
            } 
        }  
        this.handFeiChaDianEvent(touchPos)
    }
    //找到一个茬点
    protected findOnChaDian(curNode){
        Common5.playEffect("success2")
        let mark = cc.instantiate(this.maskSp)
        mark.active = true
        curNode.addChild(mark) 
        mark.setPosition(0,0);

        this.refreshBaoJiaNum(this.chaDianConfig.ChaNode[curNode.name].money)               
        this.showDialogue(this.chaDianConfig.ChaNode[curNode.name].sound)
        this.beforeChaDianFindCallback && this.beforeChaDianFindCallback();
        this.afterChaDianFindCallback && this.afterChaDianFindCallback(curNode.name);
    }
    //刷新报价数值
    protected refreshBaoJiaNum(curMoney){
        if(!curMoney){
            return
        }
        this.leftBaoJiaMoney -= curMoney
        this.node.getChildByName("duiHuaDi").getChildByName("baoJiaStr").getComponent(cc.Label).string = this.leftBaoJiaMoney + "元"
        let toolTipnode = cc.instantiate(this.node.getChildByName("duiHuaDi").getChildByName("effectStr")) ;
        toolTipnode.getComponent(cc.Label).string = "-"+curMoney
        toolTipnode.active = true;
        this.node.getChildByName("duiHuaDi").addChild(toolTipnode)
        cc.tween(toolTipnode).by(1.5,{y:80}).call(()=>{
            toolTipnode.removeFromParent()
        }).start();
    }
    //刷新底价数值
    protected refreshDiJiaNum(){
        this.node.getChildByName("duiHuaDi").getChildByName("diJiaStr").getComponent(cc.Label).string = this.diJiaMoney + "元"
    }
    //设置倒计时节点的位置
    protected setTimeNode(){
        let worldPos = this.timeNode.getPosition()
        Game.ins.setTimePos(worldPos.x,worldPos.y)
        Game.ins.setTimeScale(0.7)
    } 
    protected openNodeTouch(p:cc.Node, cbTouchStart, cbTouchMove, cbTouchEnd){ 
        cbTouchStart && p.on(cc.Node.EventType.TOUCH_START, cbTouchStart, this);
        cbTouchMove && p.on(cc.Node.EventType.TOUCH_MOVE, cbTouchMove, this);
        cbTouchEnd && p.on(cc.Node.EventType.TOUCH_END, cbTouchEnd, this);
        cbTouchEnd && p.on(cc.Node.EventType.TOUCH_CANCEL, cbTouchEnd, this);
    }
    protected closeNodeTouch(p:cc.Node, cbTouchStart, cbTouchMove, cbTouchEnd){ 
        cbTouchStart && p.off(cc.Node.EventType.TOUCH_START, cbTouchStart, this);
        cbTouchMove && p.off(cc.Node.EventType.TOUCH_MOVE, cbTouchMove, this);
        cbTouchEnd && p.off(cc.Node.EventType.TOUCH_END, cbTouchEnd, this);
        cbTouchEnd && p.off(cc.Node.EventType.TOUCH_CANCEL, cbTouchEnd, this);
    }
    
    protected showDialogue(soundUrl){
        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 closeStartTips(){
        this.maskNode.getChildByName("startTips").active = false
    }
}