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

    @property(cc.Node)
    shouNode: cc.Node = null;
    @property(cc.Node)
    shouSpineNode: cc.Node = null;

    @property(cc.Node)
    nvSpineNode: cc.Node = null;
    @property(cc.Node)
    nvzuoSpineNode: cc.Node = null;
    @property(cc.Node)
    lbcSpineNode: cc.Node = null;
    @property(cc.Node)
    lbccqSpineNode: cc.Node = null;

    chooseLeft = false
    chushouIndex = 1
    chatConfig = [
        {effectUrl:'sound/JuQing2/夜哥,今天订婚,你觉得我要穿哪件衣服呢',delayTime:4.5, str:'夜哥,今天订婚,你觉得我要穿哪件衣服呢',   posi:'qipao1'},
        {effectUrl:'sound/JuQing2/那你打算用哪件家传宝做聘礼呢',delayTime:4, str:'那你打算用哪件家传宝做聘礼呢!', proArray:[GameType.MainHall, GameType.ChaoFanWang],posi:'qipao1'},
        {effectUrl:'sound/JuQing2/夜哥,你快走,我被我爸利用了!',delayTime:4, str:'夜哥,你快走,我被我爸利用了!', proArray:[GameType.ChaoFanDesk,GameType.JuQingDialog2, ],posi:'qipao2'},
        {effectUrl:'sound/JuQing2/你们父子要干什么!',delayTime:2, str:'你们父子要干什么!', posi:'-1'},
        {effectUrl:'sound/JuQing2/哼!要不是为了你这把剑,你连这个门都进不来,滚吧!',delayTime:5, str:'哼!要不是为了你这把剑,你连这个门都进不来,滚吧!', proArray:[GameType.chatLongGe],posi:'qipao3'},

    ]
    


    juqingIndex = 0

    start () {
    
        Common5.ReportDY("inLevel", "关卡00001-开始剧情");//开始
        
        Common5.playRemoteAudioMusic('sound/JuQing2/背景音乐4')

        for(let i= 0;i<this.chatConfig.length;i++){
            let effectUrl = this.chatConfig[i].effectUrl
            Common5.loadRemoteAudioEffect(effectUrl)
        }
   
        Common5.loadRemoteAudioEffect('sound/diandiandian/锅拍岳父')
        Common5.playRemoteSpine(this.shouSpineNode,'Spine/JuQingStart2', '手', '手静止', false)

        this.scheduleOnce(()=>{
           
            this.nextGameQipao(0)
       
        },0.1)
    }




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


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

        cc.audioEngine.stopAllEffects();

        if (curLog.effectUrl && curLog.effectUrl.length > 0) {
            Common5.playRemoteAudioEffect(curLog.effectUrl);
        }
        if(curLog.proArray){
            PrefabManage.preloadPrefabArray(curLog.proArray)
        }else{
            Common5.loadRemoteAudioEffect('sound/锅飞出')
            Common5.loadRemoteSpine('Spine/JuQingRoom2','妹妹')
            Common5.loadRemoteSpine('Spine/roomSpine','男主龙哥')
        }


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

    
    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.MainHall, null, () => {
            
        })
    }

    leftClickEvent(){
        if(this.chushouIndex == 1){
            Common5.ReportDY("inLevel", "关卡00002-开始剧情");//点击一次
        
            Common5.playRemoteSpine(this.lbcSpineNode,'Spine/JuQingStart2', '洛北辰', '待机')
            Common5.playRemoteSpine(this.nvzuoSpineNode,'Spine/JuQingStart2', '洛天行洛夕颜坐', '') //挣扎粉,挣扎绿
            this.chushouIndex++
            this.chooseLeft = true
            this.setAnim(this.nvSpineNode, '绿翠花', true)
            this.shouNode.active = false
            this.shouNode.getChildByName('左裙子').active = false
            this.shouNode.getChildByName('右裙子').active = false
            this.nextGameQipao(1)
        }else{
            Common5.ReportDY("inLevel", "关卡00003-开始剧情");//点击二次
            this.nextGame()
        }
    }


    rightClickEvent(){
        if(this.chushouIndex == 1){
            Common5.ReportDY("inLevel", "关卡00002-开始剧情");//点击一次
            Common5.playRemoteSpine(this.lbcSpineNode,'Spine/JuQingStart2', '洛北辰', '待机')
            Common5.playRemoteSpine(this.nvzuoSpineNode,'Spine/JuQingStart2', '洛天行洛夕颜坐', '')
            this.chushouIndex++
            this.chooseLeft = false
            this.setAnim(this.nvSpineNode, '粉裙', true)
            this.shouNode.active = false
            this.shouNode.getChildByName('左裙子').active = false
            this.shouNode.getChildByName('右裙子').active = false
            this.nextGameQipao(1)
        }else{
            Common5.ReportDY("inLevel", "关卡00003-开始剧情");//点击二次
        
            this.nextGame()
        }
    }


    nextGameQipao(index){
        let config = this.chatConfig[index]
        this.showQiPao(config,()=>{
            this.shouNode.active = true
            Common5.playRemoteSpine(this.shouSpineNode,'Spine/JuQingStart2', '手', '手盖上', false)
            this.scheduleOnce(()=>{
                if(index == 0){
                    this.shouNode.getChildByName('左裙子').active = true
                    this.shouNode.getChildByName('右裙子').active = true
                }else{
           
                    this.shouNode.getChildByName('左剑').active = true
                    this.shouNode.getChildByName('右锅').active = true
                }
                
            },1.0)
        })
    }
    nextGame(){
        //
        this.shouNode.active = false
        this.nvSpineNode.active = false
        this.nvzuoSpineNode.active = true
        this.lbcSpineNode.active = true
        if(this.chooseLeft){
            Common5.playRemoteSpine(this.nvzuoSpineNode,'Spine/JuQingStart2', '洛天行洛夕颜坐', '挣扎绿')
        }else{
            Common5.playRemoteSpine(this.nvzuoSpineNode,'Spine/JuQingStart2', '洛天行洛夕颜坐', '挣扎粉')
        }
        let config = this.chatConfig[2]
        this.showQiPao(config,()=>{
            this.node.getChildByName('对话框').active = true
        })
    }


    duihuaClickEvent(event){
        event.target.active = false
        Common5.playRemoteSpine(this.lbccqSpineNode,'Spine/JuQingStart2', '洛北辰出拳', '出拳')
        let config = this.chatConfig[3]
        //PrefabManage.preloadPrefabArray([GameType.MainHall, GameType.ChaoFanWang])
        
        Common5.ReportDY("inLevel", "关卡00004-开始剧情");//对话
        this.showQiPao(config,()=>{
            let config2 = this.chatConfig[4]
            this.showQiPao(config2,()=>{
                this.lbccqSpineNode.active = true
                this.lbcSpineNode.active = false
                Common5.playRemoteSpine(this.lbccqSpineNode,'Spine/JuQingStart2', '洛北辰出拳', '出拳', false)
                this.scheduleOnce(()=>{
           
                    Common5.playRemoteAudioEffect('sound/diandiandian/锅拍岳父')
                },0.5)

                this.scheduleOnce(()=>{
                    this.lbcSpineNode.active = true
                    this.lbccqSpineNode.active = false
                    this.showEnd()
                },1.5)
            })
        })
    }


    showEnd(){
        cc.tween(this.node)
        .to(1.0, {opacity:0})
        .call(()=>{
            this.node.stopAllActions()
            this.scheduleOnce(() => {
                this.starGame()
            },0.1)
    
        })
        .start()
        // 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.ChaoFanDesk,GameType.JuQingDialog2, GameType.chatLongGe])
        //     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()

    }
    // update (dt) {}
}