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

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

    changlaoSpine:cc.Node = null
    changlaoNode:cc.Node = null
    luoxiyan:cc.Node = null
    nanzhu:cc.Node = null
    chadianClick:cc.Node = null
    chadianIndex= 0
    // onLoad () {}
    chatConfig = [
        {chadian:'伤心女友', effectUrl:'sound/JuQing2/夜哥,你终于来了,快救我!',delayTime:3, str:'夜哥,你终于来了,快救我!', posi:'qipao3'},
        {chadian:'愤怒洛天行', effectUrl:'sound/JuQing2/你别太嚣张,这里容不得你撒野!',delayTime:4, str:'你别太嚣张,这里容不得你撒野!', posi:'qipao2'},
        {chadian:'男主', effectUrl:'sound/JuQing2/手下败将嚣张什么!夕颜我今天一定要带走!',delayTime:4, str:'手下败将嚣张什么!夕颜我今天一定要带走!', posi:'qipao1'},
        {chadian:'暗月长老', effectUrl:'sound/JuQing2/哈哈,这女娃子真是上好的鼎炉,真不错!',delayTime:6, str:'哈哈,这女娃子真是上好的鼎炉,真不错!', posi:'qipao4'},
        {chadian:'男主', effectUrl:'sound/JuQing2/你放开她,不然我一定会毁了你暗月组织!',delayTime:5, str:'你放开她,不然我一定会毁了你暗月组织!', posi:'qipao1'},
        {chadian:'暗月长老', effectUrl:'sound/JuQing2/小鬼,就凭你!',delayTime:4, str:'小鬼,就凭你!', posi:'qipao4'},
    ]
    

    chadianArray = ['伤心女友','愤怒洛天行',  '男主', '暗月长老']
    start () {
        //预加载
        this.nanzhu =this.node.getChildByName('男主洛天行')
        this.luoxiyan =this.node.getChildByName('洛夕颜')
        this.changlaoSpine =this.node.getChildByName('暗月长老')
        this.changlaoNode =this.node.getChildByName('长老瞬移')
        this.chadianClick = this.node.getChildByName('chadianClick')
        Common5.addUrlSprite_custom('Spine/JiuZhuLuoxiyan/长老', this.changlaoNode.getComponent(cc.Sprite))
        Common5.playRemoteSpine(this.changlaoSpine, 'Spine/JiuZhuLuoxiyan' ,'暗月长老','暗月长老')
        Common5.loadRemoteAudioEffect('sound/diandiandian/锅拍岳父') 
        
    }

    // 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('Spine/JiuZhuLuoxiyan/茬点/'+textureUrl, node.getComponent(cc.Sprite))
        }
        this.chadianIndex++

        let self = this
        if(data == 0){
            this.showQiPao(chatDate)
            if(this.chadianIndex == 2){
                self.chadianClick.children[2].active = true
            }
        }else if(data == 1){
            this.setAnim(this.nanzhu, '举锤子', false)
            this.scheduleOnce(()=>{
                this.setAnim(this.nanzhu, '举锤子待机', true)
                this.showQiPao(chatDate, ()=>{
                    if(this.chadianIndex == 2){
                        self.chadianClick.children[2].active = true
                    }
              
                })
            },0.4)
 
        }else if(data == 2){
            this.setAnim(this.nanzhu, '男主打洛天行', false)
            Common5.playRemoteAudioEffect('sound/diandiandian/锅拍岳父') 
        

            this.scheduleOnce(()=>{
                this.setAnim(this.nanzhu, '洛天行跪待机', true)
                this.showQiPao(chatDate, ()=>{
                    this.changlaoSpine.active = true
                    cc.tween(this.changlaoSpine)
                    .to(0.3,{x:256})
                    .call(()=>{
                        self.chadianClick.children[3].active = true
                    })
                    .start()

                })
            },0.5)
        }else if(data == 3){
            this.showQiPao(chatDate,()=>{
                let chatDate2 = this.chatConfig[4]
                this.showQiPao(chatDate2,()=>{
                    let chatDate3 = this.chatConfig[5]
                    this.showQiPao(chatDate3,()=>{
                        self.changlaoNode.active = true
                        self.luoxiyan.active = false
                        self.changlaoSpine.active = false
                        
                        cc.tween(self.changlaoNode)
                        .to(1,{y:1100, scale:0.2})
                        .call(()=>{
                            self.endGame()
                        })
                        .start()

                    })
                })
            })

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