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

const {ccclass, property} = cc._decorator;

@ccclass
export default class GaiZhaoRen extends cc.Component {

    @property(cc.Node)
    chadianIconArray:cc.Node = null

    chadianIndex= 0

    chatConfig = [
        {chadian:'妹妹', effectUrl:'sound/JuQing2/妹妹居然被拿来做实验?',delayTime:2, str:'妹妹居然被拿来做实验?', posi:''},
        {chadian:'魔物', effectUrl:'sound/JuQing2/真是可恶啊,居然拿魔物做人体实验!',delayTime:3, str:'真是可恶啊,居然拿魔物做人体实验!', posi:''},
        {chadian:'研究记录', effectUrl:'sound/JuQing2/什...什么!怎么会这样!我...我到底还是不是人?',delayTime:6, str:'什...什么!怎么会这样!我...我到底还是不是人?', posi:''},
        {chadian:'', effectUrl:'sound/JuQing2/快,快走,我感受到很多人过来,你打不过!',delayTime:5, str:'快,快走,我感受到很多人过来,你打不过!', posi:'qipao1'},
    
    ]


    chadianArray = ['妹妹','魔物','研究记录']

    // LIFE-CYCLE CALLBACKS:

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

    }
    endGame(){

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


    }

    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('GaiZhaoRen/chadian/'+textureUrl, node.getComponent(cc.Sprite))
        }
        this.chadianIndex++
        this.showQiPao(chatDate)
        let delayTime_ = chatDate.delayTime
        
        if(this.chadianIndex == 3){
 
            this.scheduleOnce(()=>{
                let baojin = this.node.getChildByName('报警灯')
                this.setAnim(baojin, '闪灯', true)
    
                this.moveAnim()
            },delayTime_)
        }
    }
    showLinPaiClick(event){
        event.target.active = false
        this.node.getChildByName('文件').active = true
        this.node.getChildByName('桌子').getChildByName('文件').active = false
        
    }

    moveAnim(){

        let guoNode = this.node.getChildByName('器灵')
        Common5.playRemoteSpine(guoNode, 'Spine/roomSpine','器灵','器灵')

        guoNode.active = true
        cc.tween(guoNode)
        .to(0.5,{x:0})
        .call(()=>{
            let chatDate = this.chatConfig[3]
            this.showQiPao(chatDate, ()=>{
                this.endGame()
            })
        })
        .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_ != '') {
            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_
            
            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)
    }
    // update (dt) {}
}