// 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 FeiQiJiDi2 extends cc.Component {
    @property(cc.Node)
    chadianIconArray:cc.Node = null

    chadianIndex= 0

    chatConfig = [
        {chadian:'金银珠宝', effectUrl:'sound/JuQing2/可恶!你们居然收敛了这么多钱财!',delayTime:3, str:'可恶!你们居然收敛了这么多钱财!', posi:''},
        {chadian:'甄刑', effectUrl:'sound/JuQing2/我是镇魔司甄刑,你来这里做什么!',delayTime:4, str:'我是镇魔司甄刑,你来这里做什么!', posi:'qipao2'},
        {chadian:'魔使令牌', effectUrl:'sound/JuQing2/甄刑,既然你的身份暴露了,那也没存在的必要了。',delayTime:7, str:'甄刑,既然你的身份暴露了,那也没存在的必要了。', posi:'qipao1'},
        {chadian:'洛北辰', effectUrl:'sound/JuQing2/是洛北辰!怎么这么强!逃!',delayTime:3, str:'是洛北辰!怎么这么强!逃!', posi:''},
    
    ]


    chadianArray = ['金银珠宝','甄刑','魔使令牌','洛北辰']

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    zhenxingSpine:cc.Node = null
    luobeichenSpine:cc.Node = null
    chadianClick:cc.Node = null
    start () {
        
        let maskBg2 = this.node.getChildByName('maskBg2')
        maskBg2.active = true

        cc.tween(maskBg2)
        .to(1,{opacity:0})
      
        .start()

        this.scheduleOnce(()=>{
            maskBg2.active = false
        },1.0)
        this.zhenxingSpine = this.node.getChildByName('甄刑')
        this.luobeichenSpine = this.node.getChildByName('洛北辰')
        this.chadianClick = this.node.getChildByName('chadianClick')
        Common5.playRemoteSpine(this.zhenxingSpine, 'Spine/FeiQiJiDi', '甄刑', '待机')
        Common5.playRemoteSpine(this.luobeichenSpine,'Spine/FeiQiJiDi', '洛北辰' ,'待机')
        Common5.loadRemoteAudioEffect('sound/diandiandian/龙哥打男主耳光')
    }

    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/FeiQiJiDi/茬点/'+textureUrl, node.getComponent(cc.Sprite))
        }
        this.chadianIndex++
        // fix me
        if(this.chadianIndex==2){
            this.chadianClick.children[this.chadianIndex].active = true
        }


        if(data == 0){
            this.showQiPao(chatDate)
        }else if(data == 1){
            this.showQiPao(chatDate)
        }else if(data == 2){
            this.showQiPao(chatDate, ()=>{
                //扇死甄刑。然后瞬移到画面中间
                Common5.playRemoteSpine(this.luobeichenSpine,'Spine/FeiQiJiDi', '洛北辰' ,'挥手',false)
                Common5.playRemoteAudioEffect('sound/diandiandian/龙哥打男主耳光') 
                this.scheduleOnce(()=>{
                    Common5.playRemoteSpine(this.luobeichenSpine,'Spine/FeiQiJiDi', '洛北辰' ,'举手待机')
                    Common5.playRemoteSpine(this.zhenxingSpine,'Spine/FeiQiJiDi', '甄刑' ,'死',false)
                    this.scheduleOnce(()=>{
                        Common5.playRemoteSpine(this.zhenxingSpine,'Spine/FeiQiJiDi', '甄刑' ,'死待机')
                        Common5.playRemoteSpine(this.luobeichenSpine,'Spine/FeiQiJiDi', '洛北辰' ,'瞬移中间',false)
                        this.scheduleOnce(()=>{
                            Common5.playRemoteSpine(this.luobeichenSpine,'Spine/FeiQiJiDi', '洛北辰' ,'中间待机')
                            if(this.chadianIndex==3){
                                this.chadianClick.children[this.chadianIndex].active = true
                            }
                        },0.3)
                    },0.3)
                },0.5)

            })
        }else if(data == 3){
            //点击后面具脱落,露出洛北辰的脸
            Common5.playRemoteSpine(this.luobeichenSpine,'Spine/FeiQiJiDi', '洛北辰' ,'中间面具脱落',false)
            this.scheduleOnce(()=>{
                Common5.playRemoteSpine(this.luobeichenSpine,'Spine/FeiQiJiDi', '洛北辰' ,'中间无面具待机')
                this.showQiPao(chatDate, ()=>{
                    this.endGame()
               })
            },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_520){
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_520)
                TaskManager.setCurTask(MainTaskIdEnum.MainTask_521)
                User.setTreeUnlockIndex(7)
            }
            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()

      
    
    }


    showLinPaiClick(event){
        event.target.active = false
        this.node.getChildByName('令牌放大').active = true

    }


     //展示气泡
     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)
    }
    // update (dt) {}
}