// 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 XieZhuShouYeren extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    @property(cc.Node)
    chadianIconArray:cc.Node = null
    xunyeren:cc.Node = null


    chadianIndex= 0
    duihuaIndex = 3
    isEnd:boolean = true

    chatConfig = [
        {chadian:'受伤的赵城', effectUrl:'sound/JuQing2/你是巡夜人赵城?怎么伤的这么严重。',delayTime:3, str:'你是巡夜人赵城?怎么伤的这么严重。', posi:''},
        {chadian:'禁药', effectUrl:'sound/JuQing2/我们小队已全部牺牲,我也撑不了几分钟了!',delayTime:7, str:'我们小队已全部牺牲,我也撑不了几分钟了!', posi:'qipao1'},
        {chadian:'怪兽', effectUrl:'sound/JuQing2/城市里面怎么也会有怪兽?不是都在荒野吗?',delayTime:3, str:'城市里面怎么也会有怪兽?不是都在荒野吗?', posi:''},
        {chadian:'', effectUrl:'sound/JuQing2/你现在愿意加入巡夜人,重整天选小队吗?',delayTime:3, str:'你现在愿意加入巡夜人,重整天选小队吗?', posi:'qipao1'},
        {chadian:'', effectUrl:'sound/JuQing2/好,我答应你!那我需要怎么做?',delayTime:3, str:'好,我答应你!那我需要怎么做?', posi:''},
        {chadian:'', effectUrl:'sound/JuQing2/天选小队不能有名无实,你要招募队友,重整小队!',delayTime:3, str:'天选小队不能有名无实,你要招募队友,重整小队!', posi:'qipao1'},
        {chadian:'', effectUrl:'sound/JuQing2/好!我先送你...回家!',delayTime:4, str:'好!我先送你...回家!', posi:''},
    ]


    chadianArray = ['受伤的赵城','禁药','怪兽']

    onDestroy(){
        cc.audioEngine.stopMusic()
    }
    
    start () {
        
        Common5.playRemoteAudioMusic('sound/JuQingBG/救巡夜人背景音')
        //预加载
        this.xunyeren =this.node.getChildByName('协助巡夜人')


    }

    // update (dt) {}

    clickChaDian(event, data){
        event.target.active = false
        let chatDate = this.chatConfig[data]
        let textureUrl = this.chadianArray[data]
        let node = this.chadianIconArray.children[this.chadianIndex]
        if(node){
            node['isChange'] = true
            node.getChildByName('lab').getComponent(cc.Label).string = textureUrl
            Common5.addUrlSprite_custom('XieZhuShouYeren/茬点/'+textureUrl, node.getComponent(cc.Sprite))
        }
        this.chadianIndex++

        if(data == 0){
            this.showQiPao(chatDate)
            //
            this.setAnim(this.xunyeren,'咳血',true)
            this.scheduleOnce(()=>{
                this.setAnim(this.xunyeren,'待机',true)
            },2.0)
        }else if(data == 1){
            this.showQiPao(chatDate)
        }else if(data == 2){
            this.showQiPao(chatDate)
        }
      
        this.endDuiHua(chatDate.delayTime)

        
    }


    endDuiHua(delayTime){

        if(this.chadianIndex == 3 && this.isEnd){
            this.isEnd = false
            //结算
            let self = this
            this.scheduleOnce(()=>{
                let chatDate = this.chatConfig[this.duihuaIndex]
                this.showQiPao(chatDate, ()=>{
                    this.duihuaIndex++
                    this.node.getChildByName('clickJuqing').active = true
                })
            },delayTime)

        }
    }
    endGame(){
       
        cc.tween(this.node)
        .to(1.0,{opacity:0})
        .call(()=>{
            let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
            let mainId = mainTaskInfo.Id
            if(mainId == MainTaskIdEnum.MainTask_513){
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_513)
                TaskManager.setCurTask(MainTaskIdEnum.MainTask_514)
                User.setRoomGoodStatus(16)
            }
      
            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)
    }


    duihuaClickEvent(event){
        event.target.active = false
        if(this.duihuaIndex == 4){
            let chatDate = this.chatConfig[this.duihuaIndex]
            this.showQiPao(chatDate, ()=>{
                this.duihuaIndex++
                let chatDate = this.chatConfig[this.duihuaIndex]
                this.showQiPao(chatDate, ()=>{
                    this.duihuaIndex++
                    this.node.getChildByName('clickJuqing').active = true
                })
            })
        }else if(this.duihuaIndex == 6){
          
            let chatDate = this.chatConfig[this.duihuaIndex]
            this.showQiPao(chatDate, ()=>{
                this.endGame()
            })
        }
 
        // let chatDate = self.chatConfig[4]
        // self.showQiPao(chatDate, ()=>{
        //     let chatDate = self.chatConfig[5]
        //     self.showQiPao(chatDate, ()=>{
        //         let chatDate = self.chatConfig[6]
        //         self.showQiPao(chatDate, ()=>{
        //             this.endGame()
        //         })
        //     })
        // })
    }

     //展示气泡
     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_ != '') {
            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();
        }else{
            //简单对话
            let chatLog = this.node.getChildByName('chatLog')
            chatLog.active = true
            chatLog.getChildByName("str").getComponent(cc.Label).string = string_
            //this.showDialogStr(string_, chatLog.getChildByName("str"))
            chatLog.stopAllActions()
            cc.tween(chatLog)
            .delay(delayTime_)
            .call(() => {
                chatLog.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)
    }
}