// 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 PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class JuQingStart extends cc.Component {

    // @property(cc.ProgressBar)
    // juexingPro: cc.ProgressBar = null;
    
    @property(cc.Node)
    shouzhiSpine: cc.Node = null;
    
    @property(cc.Node)
    lueduoNode: cc.Node = null;
    
    @property(cc.Node)
    wenhouNode: cc.Node = null;
    
    @property(cc.Node)
    quanzuNode: cc.Node = null;
    
    chatConfig = [
        {effectUrl:'sound/JuQing2/江夜,你就在我家进行器魂觉醒吧,我爸可以保护你!',delayTime:5, str:'江夜,你就在我家进行器魂觉醒吧,我爸可以保护你!',posi:'qipao1',shouzhi:'女主Node'},
        {effectUrl:'sound/JuQing2/好,那就麻烦伯父了!',delayTime:3, str:'好,那就麻烦伯父了!', posi:'qipao2',shouzhi:'男主Node'},
        {effectUrl:'sound/JuQing2/哈哈,果然是这至尊剑,从此我儿北辰就有了至尊之姿!',delayTime:8, str:'哈哈,果然是这至尊剑,从此我儿北辰就有了至尊之姿!', posi:'qipao3',shouzhi:'洛行天Node'},
        {effectUrl:'sound/JuQing2/爸!你在干什么!快还给江夜!',delayTime:4, str:'爸!你在干什么!快还给江夜!', posi:'qipao4',shouzhi:'女主Node'},
        {effectUrl:'sound/JuQing2/你以后就是个废物了,记住!离夕颜远点!',delayTime:6, str:'你以后就是个废物了,记住!离夕颜远点!', posi:'qipao5',shouzhi:'洛北辰Node'},
        {effectUrl:'sound/JuQing2/你们父子真是卑鄙,这个仇我一定会报!',delayTime:5, str:'你们父子真是卑鄙,这个仇我一定会报!', posi:'qipao6'},
        {effectUrl:'sound/JuQing2/至尊剑,我居然觉醒了至尊剑',delayTime:3, str:'至尊剑,我居然觉醒了至尊剑', posi:'qipao2'},
    ]
    
    luobeichenZuo:cc.Node = null
    luoxingtian:cc.Node = null
    // nanzhu:cc.Node = null
    nvzhu:cc.Node = null
    dazuo:cc.Node = null
    luobeichenXZ:cc.Node = null
    // clickJueXing:cc.Node = null
    clickJuqing:cc.Node = null
    clickJueXingIndex = 0
    clickJueXingMax = 20
    juqingIndex = 0

    start () {
        Common5.ReportDY("inLevel", "关卡00001-开始剧情");
        //预加载动画 打坐 洛北辰男主
        this.luobeichenZuo = this.node.getChildByName('洛北辰坐')
        this.luoxingtian = this.node.getChildByName('洛行天')
        // this.nanzhu = this.node.getChildByName('男主站')
        this.nvzhu = this.node.getChildByName('女主')
        this.dazuo = this.node.getChildByName('打坐')
        this.luobeichenXZ = this.node.getChildByName('洛北辰男主')
        //this.clickJueXing = this.node.getChildByName('clickJueXing')
        this.clickJuqing = this.node.getChildByName('clickJuqing')
        Common5.playRemoteAudioMusic('sound/JuQingBG/引导剧情背景音')
        Common5.playRemoteSpine(this.luoxingtian,'Spine/JuQingStart','洛行天女主', '洛天行待机')
        Common5.playRemoteSpine(this.nvzhu,'Spine/JuQingStart','洛行天女主', '女待机')
        for(let i= 0;i<this.chatConfig.length;i++){
            let effectUrl = this.chatConfig[i].effectUrl
            Common5.loadRemoteAudioEffect(effectUrl)
        }
       // Common5.loadRemoteAudioEffect('sound/JuQing2/至尊剑,我居然觉醒了至尊剑')
        Common5.loadRemoteAudioEffect('sound/diandiandian/洛北辰踢人')
        Common5.playRemoteAudioEffect('sound/觉醒')
        this.setAnim(this.dazuo, '1', false)
        this.scheduleOnce(()=>{
            this.setAnim(this.dazuo, '三阶段', true)
            let config = this.chatConfig[6]
            this.showQiPao(config,()=>{
                this.juqingIndex = 2
                this.clickJuqing.active = true
                let config2 = this.chatConfig[this.juqingIndex]
                this.lueduoNode.active = true
                if(config2.shouzhi){
                    let node = this.node.getChildByName(config2.shouzhi)
                    Common5.setNodeToTargetPos(this.shouzhiSpine, node)
                }
            })
       
        },1.3)
    }


    juexingClick(event){
        let target = event.target
       
        this.clickJueXingIndex++
        if(this.clickJueXingIndex == 5){
            target.getChildByName('手指箭头').active = false
        }
        if(this.clickJueXingIndex == 10){
            //this.setAnim(this.dazuo, '二阶段', true)
            Common5.playRemoteSpine(this.dazuo,'Spine/JuQingStart','打坐', '二阶段')
        }else if(this.clickJueXingIndex == 20){
            //this.setAnim(this.dazuo, '三阶段', true)
            Common5.playRemoteSpine(this.dazuo,'Spine/JuQingStart','打坐', '三阶段')
            this.clickJuqing.active = true
            let config2 = this.chatConfig[this.juqingIndex]
            if(config2.shouzhi){
                let node = this.node.getChildByName(config2.shouzhi)
                Common5.setNodeToTargetPos(this.shouzhiSpine, node)
            }
            target.active = false
        }

        //this.juexingPro.progress = this.clickJueXingIndex/this.clickJueXingMax
    }

    setAnim(node, anim, loop = false){
        node.getComponent(sp.Skeleton).setAnimation(0,anim,loop)
    }

    clickJuqingEvent(){
        this.clickJuqing.active = false
        let config = this.chatConfig[this.juqingIndex]

      
        if(this.juqingIndex == 0){
            Common5.loadRemoteSpine('Spine/JuQingStart','打坐')
            Common5.loadRemoteSpine('Spine/JuQingStart','洛北辰男主')
    
            //对话1
            this.juqingIndex++
            this.clickJuqing.active = true
            let config2 = this.chatConfig[this.juqingIndex]
            if(config2.shouzhi){
                let node = this.node.getChildByName(config2.shouzhi)
                Common5.setNodeToTargetPos(this.shouzhiSpine, node)
            }

            this.showQiPao(config,()=>{
        
            })
        }else if(this.juqingIndex == 1){
            //盘腿坐
        
            this.showQiPao(config,()=>{
                this.juqingIndex++
                // this.nanzhu.active = false
                this.dazuo.active = true
                //this.setAnim(this.dazuo, '一阶段', true)
                Common5.playRemoteSpine(this.dazuo,'Spine/JuQingStart','打坐', '一阶段')
                // this.clickJueXing.active = true
            })

          
        }else if(this.juqingIndex == 2){
            
            Common5.ReportDY("inLevel", '关卡00002-开始剧情');

            this.juqingIndex++
            this.lueduoNode.active = false
            this.showQiPao(config,()=>{
       
            })

            //洛天行伸手将剑吸到自己手里。男主跪在地上 点击继续
            //this.setAnim(this.dazuo, '吸走', false)
            Common5.playRemoteSpine(this.dazuo,'Spine/JuQingStart','打坐', '吸走', false)

            this.setAnim(this.luoxingtian, '洛天行收剑', false)
            this.scheduleOnce(()=>{
                this.dazuo.active = false
                this.luobeichenXZ.active = true
                
                Common5.playRemoteSpine(this.luobeichenXZ,'Spine/JuQingStart','洛北辰男主', '男主跌倒待机')

                this.setAnim(this.luoxingtian, '洛天行收剑待机', true)
                this.clickJuqing.active = true
                let config2 = this.chatConfig[this.juqingIndex]
                if(config2.shouzhi){
                    this.quanzuNode.active = true
                    let node = this.node.getChildByName(config2.shouzhi)
                    Common5.setNodeToTargetPos(this.shouzhiSpine, node)
                }
            },1.0)


        }else if(this.juqingIndex == 3){
            Common5.ReportDY("inLevel", '关卡00003-开始剧情');

            this.quanzuNode.active = false
            this.nvzhu.active = false
            this.setAnim(this.luoxingtian, '女上前', false)
            this.scheduleOnce(()=>{
                this.setAnim(this.luoxingtian, '靠近待机', true)
                this.juqingIndex++
                this.clickJuqing.active = true
                let config2 = this.chatConfig[this.juqingIndex]
                this.wenhouNode.active = true
                if(config2.shouzhi){
                    let node = this.node.getChildByName(config2.shouzhi)
                    Common5.setNodeToTargetPos(this.shouzhiSpine, node)
                }

                this.showQiPao(config,()=>{
              
                })

            },0.5)
        

        }else if(this.juqingIndex == 4){
            Common5.ReportDY("inLevel", '关卡00004-开始剧情');

            this.luobeichenZuo.active = false
            this.wenhouNode.active = false

            //this.setAnim(this.luobeichenXZ, '洛北辰踢男主', false)
            Common5.playRemoteSpine(this.luobeichenXZ,'Spine/JuQingStart','洛北辰男主', '洛北辰踢男主', false)
            Common5.playRemoteAudioEffect('sound/diandiandian/洛北辰踢人');

            this.scheduleOnce(()=>{
                //this.setAnim(this.luobeichenXZ, '洛北辰踢男主待机', true)
                Common5.playRemoteSpine(this.luobeichenXZ,'Spine/JuQingStart','洛北辰男主', '洛北辰踢男主待机')

                this.setAnim(this.luoxingtian, '女被抓', true)
                this.scheduleOnce(()=>{
                    this.setAnim(this.luoxingtian, '女被抓待机', true)
                    this.showQiPao(config,()=>{
                        this.juqingIndex++
                        let config2 = this.chatConfig[this.juqingIndex]
                        this.showQiPao(config2,()=>{
                            //end
                            this.node.getChildByName('layer3').opacity = 0
                            this.node.getChildByName('layer3').active = true
                            cc.tween(this.node.getChildByName('layer3'))
                            .delay(1.0)
                            .to(0.5, { opacity: 255 })
                            .call(() => {
                                Common5.ReportDY("inLevel", '关卡00005-开始剧情');

                                Common5.loadRemoteAudioEffect('sound/锅飞出')
      
                                PrefabManage.preloadPrefabArray([GameType.MyRoom,GameType.JiShiBen])
                                let str = '在这地球灵气复苏的时代,失去觉醒物的江夜又该如何生存下去'
                                let func = () => {
                                    Common5.loadRemoteSpine('Spine/JuQingRoom2','妹妹')
                                    Common5.loadRemoteSpine('Spine/roomSpine','男主龙哥')
                                   
                                    this.scheduleOnce(() => {
                                        this.starGame()
                                    },0.8)
                                    // this.node.getChildByName('exitRoom').active = true
                                }
                                this.showDialogStr(str, this.node.getChildByName('layer3').getChildByName("str"), func)
                            })
                            .start()

                        })
                    })
                },0.5)

           
            },0.5)
        }

    }

     //展示气泡
     showQiPao(curLog, func?) {

        cc.audioEngine.stopAllEffects();

        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)
    }

    
    starGame() {
        //1已完成开始剧情
        if (User.getFirstStepIndex() == 0) {
            User.setFirstStepIndex(1)
        }
        let name = this.node.name
        this.node.removeFromParent()
        this.node.destroy()
        EventMgr.emitEvent_custom(ryw_Event.ExitBtnEvent, name);
    
        PrefabManage.loadPrefabByType(GameType.MyRoom, null, () => {
            PrefabManage.loadPrefabByType(GameType.JiShiBen)
        })
    }
    // update (dt) {}
}