import Common5 from "../../../../Platform/th/Common5";
import JuQingTuiLiConfig from "./JuQingTuiLi_Config";

import Common from "../../../../FrameWork/Util/Common";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
const {ccclass, property} = cc._decorator;

enum NodeType{
    None,
    Person,
    Goods
}
enum BtnType{
    SiYin,
    DongJi,
    XiongQi,
    XiongShou
}
@ccclass
export default class JuQingTuiLi extends cc.Component{
    gameId:number = 0;
    gameInfo = null

    mapNode: cc.Node = null;  //map图片
    mapNodeFixedScale:number = 1//固定缩放
    originalTouchDistance:number = 0 //起始双指触摸间距
    lastScale:number = 1//上次缩放值
    curScale:number = 1//当前缩放值
    touchId1:number = -1
    touchId2:number = -2
    maskNode:cc.Node = null;
    caseIntroductionNode:cc.Node = null
    goodsOrPersonNode:cc.Node = null
    successPassNode:cc.Node = null
    bottomInfoNode:cc.Node = null
    curSelectNode:cc.Node = null//当前选中的节点
    iconSiYinSpFrame:cc.Node = null
    iconDongJiFrame:cc.Node = null
    iconXiongQiSpFrame:cc.Node = null
    iconXiongShouSpFrame:cc.Node = null
    btnCommit:cc.Node = null

    siYinStr:string = ""//死因
    dongJiStr:string = ""//动机
    xiongQiStr:string = ""//凶器
    xiongShouStr:string = ""//凶手

    onLoad(){
        Common.Type = 0;
        Common.subLevel = 0;
        Common.GameSubTipConfigs=[Common5.gameConfig.zmGameConfig[Common5.selectGameNum].toolTip]
        Common.GameSubAnswerConfigs=[Common5.gameConfig.zmGameConfig[Common5.selectGameNum].answer]

        
        this.gameId = Common5.gameConfig.zmGameConfig[Common5.selectGameNum].gameId
        this.gameInfo = JuQingTuiLiConfig.getInstance().getGoodsInfoConfig(this.gameId)
    }
    start(){
        this.initNode()
        this.openCaseIntroduction()
        this.initBottomDescStr()
        this.initBtnString()
        this.initSuccessView()
        this.initMapNodeTouchEvent()
    }  
    initNode(){
        this.maskNode = this.node.getChildByName("maskNode")
        this.caseIntroductionNode = this.node.getChildByName("caseIntroductionNode")
        this.goodsOrPersonNode = this.node.getChildByName("goodsOrPersonNode")
        this.bottomInfoNode = this.node.getChildByName("bottomInfoNode")
        this.iconSiYinSpFrame = this.bottomInfoNode.getChildByName("icon_siYin").getChildByName("spFrame")
        this.iconDongJiFrame = this.bottomInfoNode.getChildByName("icon_dongJi").getChildByName("spFrame")
        this.iconXiongQiSpFrame = this.bottomInfoNode.getChildByName("icon_xiongQi").getChildByName("spFrame")
        this.iconXiongShouSpFrame = this.bottomInfoNode.getChildByName("icon_xiongShou").getChildByName("spFrame")
        this.btnCommit = this.bottomInfoNode.getChildByName("btn_commit")
        this.successPassNode = this.node.getChildByName("successPassNode")
    }
    //初始化触摸监听
    initMapNodeTouchEvent(){
        let prefabUrl = this.gameInfo.PrefabInfo.prefabUrl
        Common5.getPrefabFromBundle("JuQingTuiLi",prefabUrl,this.maskNode,(prefabNode)=>{
            this.mapNode = prefabNode
            
            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
        });
    }
    //底部信息名称
    initBottomDescStr(){
        for(var i=0;i<this.bottomInfoNode.children.length;i++){
            let spStr = this.bottomInfoNode.children[i].getChildByName("spStr")
            if(spStr){
                spStr.getComponent(cc.Label).string = this.gameInfo.BottomDescStr[i]
            }    
        }
    }
    touchStart_mapNode(event){
        if(this.touchId1 < 0){
            this.touchId1 = event.touch.getID()
        }else if(this.touchId2 < 0){
            this.touchId2 = event.touch.getID()
        }
    }
    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 > 2){
                scale = 2
            }else{
            }
            this.mapNode.scale = scale*this.mapNodeFixedScale
            this.curScale = scale

        }else if(touches.length == 1){ //拖动
            let delta = event.getDelta()
            this.mapNode.x += delta.x
            this.mapNode.y += delta.y

            this.goToBoundary()
        }
    }
    touchEnd_mapNode(event){
        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
        }
        let touchPos = event.getLocation();
        this.checkTouchArea(touchPos)
    }

    //距离
    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
        }  
    }
    checkTouchArea(pos){
        //人物触碰点逻辑
        let personChildrens = this.mapNode.getChildByName("personNode").children
        for(var i =0;i<personChildrens.length;i++){ 
            let rect = personChildrens[i].getBoundingBoxToWorld()
            if(rect.contains(pos)){
                this.openGoodsPersonIntroduction(NodeType.Person,personChildrens[i])
                return
            }
        }  
        //物品触碰点逻辑
        let goodsChildrens = this.mapNode.getChildByName("goodsNode").children
        for(var i =0;i<goodsChildrens.length;i++){ 
            let rect = goodsChildrens[i].getBoundingBoxToWorld()
            if(rect.contains(pos)){
                this.openGoodsPersonIntroduction(NodeType.Goods,goodsChildrens[i])
                return
            }
        }  
    }
    //打开案情介绍
    openCaseIntroduction(){
        this.setCaseIntroView()

        let bg = this.caseIntroductionNode.getChildByName("bg")
        bg.scale = 0
        this.caseIntroductionNode.active = true
        cc.tween(bg)
            .to(0.3,{scale:1.1})
            .to(0.1,{scale:1})
        .start()
    }
    //设置案情介绍界面
    setCaseIntroView(){
        let caseIntroduction = this.gameInfo.CaseIntroduction
        //标题
        let tittleStr = this.caseIntroductionNode.getChildByName("bg").getChildByName("tittleStr")
        tittleStr.getComponent(cc.Label).string = caseIntroduction.tittleStr
        //内容
        let contentStr = this.caseIntroductionNode.getChildByName("bg").getChildByName("contentStr")
        contentStr.getComponent(cc.Label).string = caseIntroduction.contentStr
    }
    //关闭案情介绍
    onBtnCloseCaseIntroduction(){
        Common5.playEffect("click")
        let bg = this.caseIntroductionNode.getChildByName("bg")
        cc.tween(bg)
            .to(0.1,{scale:1.1})
            .to(0.3,{scale:0})
            .call(()=>{
                this.caseIntroductionNode.active = false
            })
        .start()   
    }
    //打开物品/人物介绍
    openGoodsPersonIntroduction(nodeType,curNode){
        Common5.playEffect("click")
        this.curSelectNode = curNode
        this.setGoodsPersonIntroView(nodeType,curNode)

        let bg = this.goodsOrPersonNode.getChildByName("bg")
        bg.scale = 0
        this.goodsOrPersonNode.active = true
        cc.tween(bg)
            .to(0.3,{scale:1.1})
            .to(0.1,{scale:1})
        .start()
    }
    //四个按钮文字
    initBtnString(){
        let siYinStr = this.goodsOrPersonNode.getChildByName("bg").getChildByName("btn_siYin").getChildByName("str")
        let dongJiStr = this.goodsOrPersonNode.getChildByName("bg").getChildByName("btn_dongJi").getChildByName("str")
        let xiongQiStr = this.goodsOrPersonNode.getChildByName("bg").getChildByName("btn_xiongQi").getChildByName("str")
        let xiongShouStr = this.goodsOrPersonNode.getChildByName("bg").getChildByName("btn_xiongShou").getChildByName("str")

        siYinStr.getComponent(cc.Label).string = this.gameInfo.BottomDescStr[0]
        dongJiStr.getComponent(cc.Label).string = this.gameInfo.BottomDescStr[1]
        xiongQiStr.getComponent(cc.Label).string = this.gameInfo.BottomDescStr[2]
        xiongShouStr.getComponent(cc.Label).string = this.gameInfo.BottomDescStr[3]
    }
    //设置物品/人物介绍界面
    setGoodsPersonIntroView(nodeType,curNode){
        let personInfo = this.gameInfo.PersonInfo
        let goodsInfo = this.gameInfo.GoodsInfo

        let childIcon = cc.instantiate(curNode.getChildByName("childIcon"));
        let spFrame = childIcon.getComponent(cc.Sprite).spriteFrame

        let goodsNode = this.goodsOrPersonNode.getChildByName("bg").getChildByName("goodsNode")
        let personNode = this.goodsOrPersonNode.getChildByName("bg").getChildByName("personNode")
        let tittleStr = this.goodsOrPersonNode.getChildByName("bg").getChildByName("tittleStr")
        
        
        if(nodeType == NodeType.Person){
            personNode.active = true
            goodsNode.active = false
            //标题
            tittleStr.getComponent(cc.Label).string = "人物信息"
            //图片
            let personSpFrame = personNode.getChildByName("personSpFrame")
            personSpFrame.getComponent(cc.Sprite).spriteFrame = spFrame
            //名字
            let nameStr = personNode.getChildByName("nameStr")
            nameStr.getComponent(cc.Label).string = personInfo[curNode.name].nameStr
            //关系
            let relationStr = personNode.getChildByName("relationStr")
            relationStr.getComponent(cc.Label).string = personInfo[curNode.name].relationStr
            //简历
            let resumeStr = personNode.getChildByName("resumeStr")
            resumeStr.getComponent(cc.Label).string = personInfo[curNode.name].resumeStr

        }else if(nodeType == NodeType.Goods){
            personNode.active = false
            goodsNode.active = true
            //标题
            tittleStr.getComponent(cc.Label).string = "物品信息"
            //图片
            let goodsSpFrame = goodsNode.getChildByName("goodsSpFrame")
            goodsSpFrame.getComponent(cc.Sprite).spriteFrame = spFrame
            //名字
            let nameStr = goodsNode.getChildByName("nameStr")
            nameStr.getComponent(cc.Label).string = goodsInfo[curNode.name].nameStr
            //描述
            let desc = goodsNode.getChildByName("desc")
            desc.getComponent(cc.Label).string = goodsInfo[curNode.name].desc
        }
    }
    //关闭物品/人物介绍
    onCloseGoodsPersonIntroduction(){
        let bg = this.goodsOrPersonNode.getChildByName("bg")
        cc.tween(bg)
            .to(0.1,{scale:1.1})
            .to(0.3,{scale:0})
            .call(()=>{
                this.goodsOrPersonNode.active = false
            })
        .start()  
    }
    //死因/动机/凶器/凶手按钮
    onBtnTypeClick(event,customData){
        Common5.playEffectCustom("WordGame","JuQingTuiLi/sound/选择线索时")
        this.checkIsRepeatShow()
        let spFrame = null
        if(customData == BtnType.SiYin){
            spFrame = this.iconSiYinSpFrame
            this.siYinStr = this.curSelectNode.name
        }else if(customData == BtnType.DongJi){
            spFrame = this.iconDongJiFrame
            this.dongJiStr = this.curSelectNode.name
        }else if(customData == BtnType.XiongQi){
            spFrame = this.iconXiongQiSpFrame
            this.xiongQiStr = this.curSelectNode.name
        }else if(customData == BtnType.XiongShou){
            spFrame = this.iconXiongShouSpFrame
            this.xiongShouStr = this.curSelectNode.name
        }
        spFrame.active = true
        //底部栏图片
        let childIcon = cc.instantiate(this.curSelectNode.getChildByName("childIcon"))
        spFrame.getComponent(cc.Sprite).spriteFrame = childIcon.getComponent(cc.Sprite).spriteFrame
        this.onCloseGoodsPersonIntroduction()
        this.setBtnCommitStatus()
    }

    //检测是否重复选择同个物品/人物
    checkIsRepeatShow(){
        if(this.gameInfo.PrefabInfo.isCanCheckSame){
            return
        }
        if(this.curSelectNode.name == this.siYinStr){
            this.iconSiYinSpFrame.active = false
            this.siYinStr = ""
        }else if(this.curSelectNode.name == this.dongJiStr){
            this.iconDongJiFrame.active = false
            this.dongJiStr = ""
        }if(this.curSelectNode.name == this.xiongQiStr){
            this.iconXiongQiSpFrame.active = false
            this.xiongQiStr = ""
        }if(this.curSelectNode.name == this.xiongShouStr){
            this.iconXiongShouSpFrame.active = false
            this.xiongShouStr = ""
        }
    }
    //关闭按钮
    onBtnClose(){
        Common5.playEffect("click")
        this.onCloseGoodsPersonIntroduction()
    }
    //提交按钮
    onBtnCommit(){
        Common5.playEffect("click")
        if(this.siYinStr == this.gameInfo.AnswerInfo.SiYin && this.dongJiStr == this.gameInfo.AnswerInfo.DongJi 
            && this.xiongQiStr == this.gameInfo.AnswerInfo.XiongQi && this.xiongShouStr == this.gameInfo.AnswerInfo.XiongShou){
                console.log("游戏成功!")
                this.successPassNode.active = true
        }else{
            console.log("游戏失败!")
            console.log("四个字符串==",this.siYinStr,this.dongJiStr,this.xiongQiStr,this.xiongShouStr)
            Game.ins.showFail()
        }
    }
    //成功确认按钮
    onBtnSure(){
        Common5.playEffect("click")
        Game.ins.showSuccess();
    }
    //设置提交按钮状态
    setBtnCommitStatus(){
        if(this.siYinStr != "" && this.dongJiStr != "" && this.xiongQiStr != "" && this.xiongShouStr != ""){
            this.btnCommit.getComponent(cc.Button).interactable = true
        }else{
            this.btnCommit.getComponent(cc.Button).interactable = false
        }
    }
    //成功界面
    initSuccessView(){
        let contentStr = this.successPassNode.getChildByName("bg").getChildByName("contentStr")
        contentStr.getComponent(cc.Label).string = this.gameInfo.ResultDesc.contentStr
    }
}