// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

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 TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class YanJiuSuo extends cc.Component {

  

    chatConfig = [
        {chadian:'',  shouzhi:'', effectUrl:'sound/JuQing2/小子,这研究所归我光明会了。',delayTime:5, str:'小子,这研究所归我光明会了', posi:'qipao1'},
        {chadian:'',  shouzhi:'nanzhuCheck',effectUrl:'sound/JuQing2/我管你光明会还是什么会,你绑我爸爸干什么!',delayTime:4, str:'我管你光明会还是什么会,你绑我爸爸干什么!', posi:'qipao3'},
        {chadian:'',  shouzhi:'huairenCheck',effectUrl:'sound/JuQing2/我不光要绑你爸爸,还需要你的神源,不然...',delayTime:7, str:'我不光要绑你爸爸,还需要你的神源,不然...', posi:'qipao1'},
        {chadian:'',  shouzhi:'nanbaCheck', effectUrl:'sound/JuQing2/光明会就是神明的狗腿子,千万不要把神源给他!',delayTime:6, str:'光明会就是神明的狗腿子,千万不要把神源给他!', posi:'qipao2'},
        {chadian:'',  shouzhi:'',effectUrl:'sound/JuQing2/我把神源给你,你不要伤害我爸!',delayTime:3, str:'我把神源给你,你不要伤害我爸!', posi:'qipao3'},
        {chadian:'',  shouzhi:'huairenCheck',effectUrl:'sound/JuQing2/哈哈,等你爸协助我融合了神源,我就放他离开,而你,滚吧!',delayTime:11, str:'等你爸协助我融合了神源,我就放他离开,而你,滚吧!', posi:'qipao1'},
    
    ]

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    nanzhuZhan:cc.Node = null
    nanzhuZhi:cc.Node = null
    clickJuqing:cc.Node = null
    guangminghuiSpine:cc.Node = null
    guangminghuiDaijiSpine:cc.Node = null
    laodieSpine:cc.Node = null


    duihuaIndex = 0

    onDestroy(){
        cc.audioEngine.stopMusic()
    }
    
    start () {
        Common5.playRemoteAudioMusic('sound/JuQingBG/研究所背景音')

        this.nanzhuZhan = this.node.getChildByName('男主站')
        this.nanzhuZhi = this.node.getChildByName('男主指')
        this.clickJuqing = this.node.getChildByName('clickJuqing')
        
        this.guangminghuiSpine = this.node.getChildByName('申梓')
        
        this.guangminghuiDaijiSpine = this.node.getChildByName('申梓待机')


        this.laodieSpine = this.node.getChildByName('男主爸')

        this.nanzhuZhan.active = true
        this.nanzhuZhi.active = false
        Common5.playRemoteSpine(this.nanzhuZhan,'Spine/roomSpine','男主','男主')
        Common5.playRemoteSpine(this.guangminghuiSpine,'Spine/YanJiuSuo','申梓','申梓')
        this.scheduleOnce(()=>{
            Common5.playRemoteSpine(this.nanzhuZhi,'Spine/YanJiuSuo','男主','男主')
        },0.5)

        //
        this.scheduleOnce(()=>{
            let date = this.chatConfig[this.duihuaIndex]
            this.duihuaIndex++
            this.showQiPao(date,()=>{
               
                let configs = this.chatConfig[this.duihuaIndex]
                let shouzhi = this.node.getChildByName(configs.shouzhi)
                Common5.setNodeToTargetPos(this.clickJuqing, shouzhi)
                this.clickJuqing.active = true
            })
        },0.5)
    }

    endGame(){
    
        
        cc.tween(this.node)
        .to(1.0,{opacity:0})
        .call(()=>{
            let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
            let mainId = mainTaskInfo.Id
            if(mainId == MainTaskIdEnum.MainTask_534){
                User.setRoomGoodStatus(22)
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_534)
                
            }
            let name = this.node.name
            this.node.removeFromParent()
            this.node.destroy()
            EventMgr.emitEvent_custom(ryw_Event.ChuMenEvent)
            EventMgr.emitEvent_custom(ryw_Event.ExitBtnEvent, name);
        })
        .start()
    }
    
    setAnim(node, anim, loop = false){
        node.getComponent(sp.Skeleton).setAnimation(0,anim,loop)
    }
     //展示气泡
     showQiPao(curLog, func?) {
        if (curLog.effectUrl && curLog.effectUrl.length > 0) {
            Common5.playRemoteAudioEffect(curLog.effectUrl);
        }

        let string_ = curLog.str
        let qiPaoPos_ = curLog.posi
        let delayTime_ = curLog.delayTime


        if (qiPaoPos_ != -1) {
            let node = this.node.getChildByName('qipao')
            let qiPao = node.getChildByName(qiPaoPos_);
            qiPao.stopAllActions()
            this.showDialogStr(string_, qiPao.getChildByName("str"))
            // qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
            .to(0.2, { scale: 1 })
            .delay(delayTime_)
            .call(() => {
                qiPao.active = false;
                if (func) {
                    func();
                }
            })
            .start();
        }
     
    }

    showDialogStr(str, dialogStr, finishFunc?) {
        let curStr = ""
        let curIndex = 0
        let callFunc = () => {
            curStr += str[curIndex++]
            dialogStr.getComponent(cc.Label).string = curStr
            if (curIndex >= str.length) {
                finishFunc && finishFunc()
            }
        }
        this.schedule(callFunc, 0.05, str.length - 1)
    }
    

    nextJuQingClickEvent(event){
        event.target.active = false
        if(this.duihuaIndex == 1){
            this.nanzhuZhan.active = false
            this.nanzhuZhi.active = true
            let date = this.chatConfig[this.duihuaIndex]
            this.duihuaIndex++
            this.showQiPao(date,()=>{
                let configs = this.chatConfig[this.duihuaIndex]
                let shouzhi = this.node.getChildByName(configs.shouzhi)
                Common5.setNodeToTargetPos(this.clickJuqing, shouzhi)
                this.clickJuqing.active = true
            })

        }else if(this.duihuaIndex == 2){
            let date = this.chatConfig[this.duihuaIndex]
            this.duihuaIndex++
            this.showQiPao(date,()=>{
                //播放动画
                this.guangminghuiSpine.active = true
                this.guangminghuiDaijiSpine.active = false


                this.setAnim(this.guangminghuiSpine, '申梓架刀', false)
                this.scheduleOnce(()=>{
                    this.setAnim(this.guangminghuiSpine, '申梓架刀待机', true)
                    let configs = this.chatConfig[this.duihuaIndex]
                    let shouzhi = this.node.getChildByName(configs.shouzhi)
                    Common5.setNodeToTargetPos(this.clickJuqing, shouzhi)
                    this.clickJuqing.active = true
                },0.3)
                

            })
        }else if(this.duihuaIndex == 3){
            //父子对话
            let date = this.chatConfig[this.duihuaIndex]
            this.duihuaIndex++
            this.showQiPao(date,()=>{
                //播放动画
     
                let date = this.chatConfig[this.duihuaIndex]
                this.duihuaIndex++
                this.showQiPao(date,()=>{
                    //男主伸手,浮现出神源飘向申梓,申梓伸手接住 点击继续 男主抓胸口待机 男主拿神源
                    Common5.playRemoteSpine(this.nanzhuZhi,'Spine/YanJiuSuo','男主','男主拿神源', false)
                    this.nanzhuZhi.getComponent(sp.Skeleton).timeScale = 2
                    this.scheduleOnce(()=>{
                        Common5.playRemoteSpine(this.nanzhuZhi,'Spine/YanJiuSuo','男主','男主抓胸口待机')
                        this.nanzhuZhi.getComponent(sp.Skeleton).timeScale = 1

                        let configs = this.chatConfig[this.duihuaIndex]
                        let shouzhi = this.node.getChildByName(configs.shouzhi)
                        Common5.setNodeToTargetPos(this.clickJuqing, shouzhi)
                        this.clickJuqing.active = true
                    },3.2)
                })
            })
        }else if(this.duihuaIndex == 5){
            let date = this.chatConfig[this.duihuaIndex]
            this.setAnim(this.guangminghuiSpine, '申梓大笑', true)
            this.showQiPao(date,()=>{
                this.endGame()
            })
        }
    }

    // update (dt) {}
}