import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";

import JuQingManager from "./JuQingManager";

const {ccclass, property} = cc._decorator;
@ccclass
export default class JuQingDialog extends cc.Component {
    @property(cc.Node)
    dialogStrL: cc.Node = null;
    @property(cc.Node)
    dialogStrR: cc.Node = null;

    @property(cc.Node)
    leftAvatar: cc.Node = null;

    @property(cc.Node)
    rightAvatar: cc.Node = null;

    @property(cc.Node)
    duihuakuangNodeLeft: cc.Node = null;

    @property(cc.Node)
    duihuakuangNodeRight: cc.Node = null;


    @property(cc.Node)
    leftPersonNameStr: cc.Node = null;
    @property(cc.Node)
    rightPersonNameStr: cc.Node = null;

    @property(cc.String)
    wenjianjiaName:string = ''

    @property(cc.Boolean)
    isJuqingPeopleChat:boolean = false


    dialogStr:cc.Node = null

    curDialogIndex:number = 0

    isCanClickNext:boolean = false
    isFinishDialog:boolean = false
    curDialogConfig = null
    curPersonConfig = null
    unLockTaskId:number = -1
    unLockJuQingIndex:string = ""
    nextJuQingID:number = 0
    curJuQingUnLockIndex:string = ''
    callFunc = null
    isShowFirst = true
    isGuide = false

    newGuideEvent2 = true
    onLoad () {

    }
  
    start () {
        EventMgr.onEvent_custom(ryw_Event.changeScene,()=>{
            console.log('changeScene++=JuQingDialog')
            this.node.removeFromParent()
            this.node.destroy()
        },this)

        this.curJuQingUnLockIndex = JuQingManager.getCurJuQingUnLockIndex()
        this.curDialogConfig = JuQingManager.getJuQingJsonByIndexStr(this.curJuQingUnLockIndex)
        this.curPersonConfig = JuQingManager.getPersonJsonByIndexStr(this.curJuQingUnLockIndex)
        for(let i=0;i<this.curDialogConfig.length;i++){
            if(this.curDialogConfig[i].indexStr == this.curJuQingUnLockIndex){
                this.curDialogIndex = this.curDialogConfig[i].id
                break
            }
        }
    
        
        this.showDialogStr()
        // 
        if(this.isJuqingPeopleChat){
            Common5.addUrlSprite_custom("head/"+this.wenjianjiaName+this.curPersonConfig.chatPersonName, this.leftAvatar.getComponent(cc.Sprite));
            this.leftAvatar.setPosition(cc.v2(-1100, -200))
            if(this.curPersonConfig.chatPersonName == '补天锅(器灵)'){
                this.leftAvatar.setPosition(cc.v2(-1100, -100))
            }

           
              //监听事件
            this.node.getChildByName('对话框').on(cc.Node.EventType.TOUCH_START, (event) => {
                this.onBtnNextClick()
            })
            

        }


        EventMgr.onEvent_custom(ryw_Event.swallowToucheEvent,()=>{
            if(this.isJuqingPeopleChat){
                

                //this.node.getChildByName('maskNode').active = true
                let index = Common5.CommonGuideIndex
                if(index != 2){
                    this.scheduleOnce(()=>{
                        this.node.getChildByName('对话框')['_touchListener'].setSwallowTouches(true);
                    },0.1)
                    //this.node.getChildByName('对话框')['_touchListener'].setSwallowTouches(true);
                }

                console.log(Common5.CommonGuideIndex, 'Common5.CommonGuideIndex++++++++++===')
                if(index == 1){
                    this.guideNextChat()
                    Common5.CommonGuideIndex = 2
             
                }else  if(index == 2){
                  
                    Common5.CommonGuideIndex = 3
                 
                }else  if(index == 3){
                    this.guideNextChat()
                    Common5.CommonGuideIndex = 4
                   
                }else  if(index == 4){
                    this.guideNextChat()
                    Common5.CommonGuideIndex = 5
                 
                    //进行位移
                    let posi1 = this.leftAvatar.getPosition()
                    let posi2 = this.duihuakuangNodeLeft.getPosition()
                    let posi3 = this.dialogStrL.getPosition()
                    let posi4 = this.leftPersonNameStr.getPosition()
                    this.leftAvatar.setPosition(cc.v2(posi1.x, posi1.y-300))
                    this.duihuakuangNodeLeft.setPosition(cc.v2(posi2.x, posi2.y-300))
                    this.dialogStrL.setPosition(cc.v2(posi3.x, posi3.y-300))
                    this.leftPersonNameStr.setPosition(cc.v2(posi4.x, posi4.y-300))


                }else  if(index == 5){
                    this.guideNextChat()
                    Common5.CommonGuideIndex = 6
                }
                
            }
           

        },this) 




        EventMgr.onEvent_custom(ryw_Event.juqingFirstEvent,()=>{
            if(this.isJuqingPeopleChat){
                
                let index = Common5.CommonGuideIndex
                if(index == 100){
                    //补天锅出现
                    this.showDialogStr(true)
                    this.isGuide = false
                    //this.guideNextChat()
                  
                }
            }
           
        },this) 

    }
    showDialogStr(directShow = false){
       
        if(!this.curDialogConfig[this.curDialogIndex]){
            this.node.removeFromParent()
            this.node.destroy()
            
            return
        }
        this.showSpineNode(directShow)
        this.isCanClickNext = false
        let curStr = ""
        let curIndex = 0
        let str = this.curDialogConfig[this.curDialogIndex].contentDesc
        let callFunc = ()=>{
            curStr += str[curIndex++]
            this.dialogStr.getComponent(cc.Label).string = curStr
            if(curIndex >= str.length){
                if(this.curDialogConfig[this.curDialogIndex].isFinish){
                    this.isFinishDialog = true
                    // this.unLockTaskId = this.curDialogConfig[this.curDialogIndex].unLockTaskId
                    this.unLockJuQingIndex = this.curDialogConfig[this.curDialogIndex].unLockJuQingIndex
                    this.nextJuQingID = this.curDialogConfig[this.curDialogIndex].nextJuQingID
                }
                this.curDialogIndex = this.curDialogConfig[this.curDialogIndex].next
                this.isCanClickNext = true
                this.guideStep(str)
            }
        }
        this.callFunc = callFunc
        this.schedule( callFunc , 0.04,str.length-1)
    }
    showSpineNode(directShow = false){
     
        let speaker = this.curDialogConfig[this.curDialogIndex].speaker
        if(speaker == 'other'){
            this.leftPersonNameStr.active = true
            this.leftPersonNameStr.getComponent(cc.Label).string = this.curPersonConfig.chatPersonName
            this.rightPersonNameStr.active = false
            if(this.isJuqingPeopleChat){
                this.duihuakuangNodeLeft.active = true
                this.duihuakuangNodeRight.active = false
            }else{
                this.leftAvatar.active = true
                this.rightAvatar.active = false
            }
     
            this.dialogStrL.active = true
            this.dialogStrR.active = false
            this.dialogStr = this.dialogStrL
            this.dialogStr.getComponent(cc.Label).string = ''
     
            Common5.addUrlSprite_custom("head/"+this.wenjianjiaName+this.curPersonConfig.chatPersonName, this.leftAvatar.getComponent(cc.Sprite));
         
            if(directShow){
                let posi =  this.leftAvatar.getPosition()
                this.leftAvatar.setPosition(-186, posi.y)
                this.leftAvatar.scale = 0.1
                cc.tween(this.leftAvatar)
                .to(0.2, {scale:1})
                .start()
            }else{
                if(this.isShowFirst && this.isJuqingPeopleChat){
                    this.isShowFirst = false
                   
                    cc.tween(this.leftAvatar)
                    .to(0.5, {x:-186})
                    .start()
    
                }
            }

       }else{
            if(speaker == 'me'){
                this.rightPersonNameStr.active = true
                this.rightPersonNameStr.getComponent(cc.Label).string = '我'
                this.leftPersonNameStr.active = false
               
                if(this.isJuqingPeopleChat){
                    this.duihuakuangNodeLeft.active = false
                    this.duihuakuangNodeRight.active = true
                }else{
                    this.leftAvatar.active = false
                    this.rightAvatar.active = true
                }

                Common5.addUrlSprite_custom("head/"+this.wenjianjiaName+'男主',this.rightAvatar.getComponent(cc.Sprite));
            
                
            }else{
                this.rightPersonNameStr.active = true
                this.rightPersonNameStr.getComponent(cc.Label).string = speaker
                this.leftPersonNameStr.active = false
                
                if(this.isJuqingPeopleChat){
                    this.duihuakuangNodeLeft.active = false
                    this.duihuakuangNodeRight.active = true
                }else{
                    this.leftAvatar.active = false
                    this.rightAvatar.active = true
                }

                Common5.addUrlSprite_custom("head/"+this.wenjianjiaName+speaker, this.rightAvatar.getComponent(cc.Sprite));
            }
               

            this.dialogStrL.active = false
            this.dialogStrR.active = true
            this.dialogStr = this.dialogStrR
            this.dialogStr.getComponent(cc.Label).string = ''
        }
    }
    onBtnNextClick(){
        if(this.isGuide){
            return
        }
        //Common5.playEffect("sound/按键点击")

        
        if(this.curDialogConfig && this.curDialogConfig[this.curDialogIndex] && this.curDialogConfig[this.curDialogIndex].contentDesc &&this.curDialogConfig[this.curDialogIndex].contentDesc == '她这是寒气入体,不能拖太久,赶紧到迷雾森林去找几味草药。'){
            EventMgr.emitEvent_custom(ryw_Event.NewGuideEvent, 3)
        }else  if(this.curDialogConfig && this.curDialogConfig[this.curDialogIndex] && this.curDialogConfig[this.curDialogIndex].contentDesc &&this.curDialogConfig[this.curDialogIndex].contentDesc == '夜….夜哥,我头…好痛,啊…是谁在说话,出来啊!'){
            EventMgr.emitEvent_custom(ryw_Event.NewGuideEvent, 4)
        }else  if(this.curDialogConfig && this.curDialogConfig[this.curDialogIndex] && 
            this.curDialogConfig[this.curDialogIndex].contentDesc 
            &&this.curDialogConfig[this.curDialogIndex].contentDesc == '敢威胁我家人,我必须干掉你!' && this.newGuideEvent2){
            this.newGuideEvent2 = false
            EventMgr.emitEvent_custom(ryw_Event.NewGuideEvent, 2)
        }


        if(!this.isCanClickNext){
            this.unschedule(this.callFunc)

            let str = this.curDialogConfig[this.curDialogIndex].contentDesc
            this.dialogStr.getComponent(cc.Label).string = str
            if(this.curDialogConfig[this.curDialogIndex].isFinish){
                this.isFinishDialog = true
                // this.unLockTaskId = this.curDialogConfig[this.curDialogIndex].unLockTaskId
                this.unLockJuQingIndex = this.curDialogConfig[this.curDialogIndex].unLockJuQingIndex
                this.nextJuQingID = this.curDialogConfig[this.curDialogIndex].nextJuQingID
            }
            this.curDialogIndex = this.curDialogConfig[this.curDialogIndex].next
            this.isCanClickNext = true
            this.guideStep(str)
            return
        }
        if(this.isFinishDialog){
            JuQingManager.finishCurJuQing(this.curJuQingUnLockIndex,this.unLockJuQingIndex)
            this.node.removeFromParent()
            return
        }
        this.showDialogStr()
    }



    guideNextChat(){
        
        let str = this.curDialogConfig[this.curDialogIndex].contentDesc
        this.dialogStr.getComponent(cc.Label).string = str
        if(this.curDialogConfig[this.curDialogIndex].isFinish){
            this.isFinishDialog = true
            // this.unLockTaskId = this.curDialogConfig[this.curDialogIndex].unLockTaskId
            this.unLockJuQingIndex = this.curDialogConfig[this.curDialogIndex].unLockJuQingIndex
            this.nextJuQingID = this.curDialogConfig[this.curDialogIndex].nextJuQingID
        }
        this.curDialogIndex = this.curDialogConfig[this.curDialogIndex].next
        this.isCanClickNext = true
        this.guideStep(str)

        if(this.isFinishDialog){
            JuQingManager.finishCurJuQing(this.curJuQingUnLockIndex,this.unLockJuQingIndex)
            this.node.removeFromParent()
            return
        }
        // this.showDialogStr()
    }

    onBtnClose(){
        while(!this.isFinishDialog){
            if(this.curDialogConfig[this.curDialogIndex].isFinish){
                this.isFinishDialog = true
                this.unLockTaskId = this.curDialogConfig[this.curDialogIndex].unLockTaskId
                this.unLockJuQingIndex = this.curDialogConfig[this.curDialogIndex].unLockJuQingIndex
                this.nextJuQingID = this.curDialogConfig[this.curDialogIndex].nextJuQingID
            }
            this.curDialogIndex = this.curDialogConfig[this.curDialogIndex].next
        }


        JuQingManager.finishCurJuQing(this.curJuQingUnLockIndex,this.unLockJuQingIndex)
        this.node.removeFromParent()
        return
    }


    guideStep(str){
        if(str == '首先,通过升级来提升你的级别。'){
            this.isGuide = true     
            Common5.CommonGuideIndex = 1  
            this.node.getChildByName('maskNode').active = false
            EventMgr.emitEvent_custom(ryw_Event.NewGuideEvent, 1)
            this.rightAvatar.active = false
            this.scheduleOnce(()=>{
                this.node.getChildByName('对话框')['_touchListener'].setSwallowTouches(false);
            },0.5)
            Common5.ReportDY("inLevel", '关卡000012-开始剧情');
        }else if(str == '消耗金钱升级,级别越高收益越高!'){
            this.isGuide = true       
            this.node.getChildByName('maskNode').active = false
            this.rightAvatar.active = false
            this.scheduleOnce(()=>{
                this.node.getChildByName('对话框')['_touchListener'].setSwallowTouches(false);
            },0.5)
            Common5.ReportDY("inLevel", '关卡000013-开始剧情');

        }else if(str == '对了,我喜欢小姐姐,你可以雇佣几个小姐姐做助理。'){
            this.isGuide = true       
            this.node.getChildByName('maskNode').active = false
            this.rightAvatar.active = false
            this.scheduleOnce(()=>{
                this.node.getChildByName('对话框')['_touchListener'].setSwallowTouches(false);
            },0.5)
            Common5.ReportDY("inLevel", '关卡000014-开始剧情');

        }else if(str == '雇佣后,不仅增加每秒的收益,后期还可以提升点击收益。'){
            this.isGuide = true       
            this.node.getChildByName('maskNode').active = false
            this.rightAvatar.active = false
            this.scheduleOnce(()=>{
                this.node.getChildByName('对话框')['_touchListener'].setSwallowTouches(false);
            },0.5)
            Common5.ReportDY("inLevel", '关卡000015-开始剧情');

        }else if(str == '好啦,接下来你先赚够20万吧!'){
            User.setFirstStepIndex(4)
            JuQingManager.finishCurJuQing(this.curJuQingUnLockIndex,this.unLockJuQingIndex)
            this.node.removeFromParent()
        }else if(str == '欺负你又怎样!凑不齐钱,就拿你这个小美人来抵债,嘿嘿!'){
            //EventMgr.emitEvent_custom(ryw_Event.NewGuideEvent, 2)
        }else if(str == '这啥时候才能赚够20万啊。'){
            this.isGuide = true     
            // this.scheduleOnce(()=>{
            //     this.isGuide = false
            // },0.8)
            Common5.CommonGuideIndex = 100
            EventMgr.emitEvent_custom(ryw_Event.NewGuideEvent, 100)
        }
    }



    setMengbanOpacity(opacity){
        this.node.getChildByName('maskNode').opacity = opacity
    }
}