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

const {ccclass, property} = cc._decorator;

@ccclass
export default class chatLongGe extends cc.Component {

    @property(cc.Node)
    longgeNode: cc.Node = null;
    @property(cc.Node)
    nanzhuzuo: cc.Node = null;
    @property(cc.Node)
    duihuaNode: cc.Node = null;

    @property(cc.Label)
    duihuaLab: cc.Label = null;

    duihuaIndex= 0
    chatConfig = [
        {effectUrl:'sound/JuQing/姓洛的,你们给我等着,这个仇我一定报!',delayTime:5, str:'姓洛的,你们给我等着,这个仇我一定报!',  proArray:[
            GameType.PeopleLevelUp, 
            GameType.MishuGame,
            GameType.ChallengeChoose], posi:'qipao3'},
        {effectUrl:'sound/JuQing/哼!报仇你还是先把你爸欠的钱还了吧!',delayTime:4, str:'哼!报仇?你还是先把你爸欠的钱还了吧!', proArray:[
            GameType.SignDay, 
            GameType.OnLineGame, 
            GameType.MultipleIncome], posi:'qipao2'},
        {effectUrl:'sound/JuQing/那个混蛋欠的钱和我有什么关系!',delayTime:2.8, str:'那个混蛋欠的钱和我有什么关系!',proArray:[        
            GameType.TanWei, 
            GameType.Shipu],  posi:'qipao1'},
        {effectUrl:'sound/JuQing/我先去你家里等你,你妹妹那姿色,嘿嘿。',delayTime:4.8, str:'我先去你家里等你,你妹妹那姿色,嘿嘿。',proArray:[    
            GameType.FangChanGame, 
            GameType.GuPiao],  posi:'qipao2'},
        {effectUrl:'sound/JuQing/你不要碰我妹妹,钱我马上就还你!',delayTime:3, str:'你不要碰我妹妹,钱我马上就还你!', proArray:[
            GameType.ChatList, 
            GameType.CityPrefab], posi:'qipao1'},

    ]

               
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        for(let i= 0;i<this.chatConfig.length;i++){
            let effectUrl = this.chatConfig[i].effectUrl
            Common5.loadRemoteAudioEffect(effectUrl)
        }
        Common5.loadRemoteAudioEffect('sound/diandiandian/龙哥打男主耳光') 
        

        let roomGoodStatus = User.getRoomGoodStatus()
        console.log(roomGoodStatus, 'roomGoodStatus++++++++++==')
        this.longgeNode.scaleX = -1
        Common5.playRemoteSpine(this.longgeNode,'Spine/roomSpine','男主龙哥', '男主待机')

        let config = this.chatConfig[this.duihuaIndex]

        this.duihuaIndex++
        Common5.ReportDY("inLevel", '关卡00006-开始剧情'); // 姓洛的,你们给我等着,这个仇我一定报
        this.showQiPao(config,()=>{
            this.longgeNode.scaleX = 1
            Common5.playRemoteSpine(this.longgeNode,'Spine/roomSpine','男主龙哥', '龙哥进场', false)
            this.scheduleOnce(()=>{
               
                Common5.playRemoteSpine(this.longgeNode,'Spine/roomSpine','男主龙哥', '龙哥男主待机', false)
    
                this.scheduleOnce(()=>{
                    Common5.playRemoteSpine(this.longgeNode,'Spine/roomSpine','男主龙哥', '龙哥打男主', false)
                    Common5.playRemoteAudioEffect('sound/diandiandian/龙哥打男主耳光') 
            
                    this.scheduleOnce(()=>{
                        Common5.playRemoteSpine(this.longgeNode,'Spine/roomSpine','男主龙哥', '龙哥举刀')
                        this.nanzhuzuo.active = true
                        Common5.playRemoteSpine(this.nanzhuzuo,'Spine/roomSpine','男主龙哥', '男主待机')
                        let config2 = this.chatConfig[this.duihuaIndex]
                        this.duihuaIndex++

                        Common5.ReportDY("inLevel", '关卡00007-开始剧情'); //哼!报仇?你还是先把你爸欠的钱还了吧!

                        this.showQiPao(config2,()=>{
                            this.duihuaNode.active = true
                            // let config3 = this.chatConfig[this.duihuaIndex]
                            // this.duihuaLab.string = config3.str
                        })
                        //this.node.getChildByName('出门').active = true
                    },0.5)
                },0.5)
            },0.5)
        })


    }


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

        cc.audioEngine.stopAllEffects();

        if (curLog.effectUrl && curLog.effectUrl.length > 0) {
            Common5.playRemoteAudioEffect(curLog.effectUrl);
        }

        if(curLog.proArray){
            PrefabManage.preloadPrefabArray(curLog.proArray)

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


    duihuaBtnClick(event){
        event.target.active = false
        if(this.duihuaIndex == 4){
            Common5.ReportDY("inLevel", '关卡000010-开始剧情'); //你不要碰我妹妹,钱我马上就还你
            let config = this.chatConfig[this.duihuaIndex]
            this.showQiPao(config,()=>{
                this.exitGame()
            })
        }else{
            Common5.ReportDY("inLevel", '关卡00008-开始剧情'); //那个混蛋欠的钱和我有什么关系!
            let config = this.chatConfig[this.duihuaIndex]
            this.duihuaIndex++
            this.showQiPao(config,()=>{
                let config2 = this.chatConfig[this.duihuaIndex]
                this.duihuaIndex++
                Common5.ReportDY("inLevel", '关卡00009-开始剧情'); //我先去你家里等你,你妹妹那姿色,嘿嘿
                this.showQiPao(config2,()=>{
                    // this.duihuaNode.active = true
                    // let config3 = this.chatConfig[this.duihuaIndex]
                    // this.duihuaLab.string = config3.str
                    this.exitGame()
                })
            })
        }
     
    }



    exitGame(){
        TaskManager.setCurTask(MainTaskIdEnum.MainTask_502)
        User.setRoomGoodStatus(5)

        let name = this.node.name
        this.node.removeFromParent()
        this.node.destroy()
        
        EventMgr.emitEvent_custom(ryw_Event.ExitBtnEvent, name);

        EventMgr.emitEvent_custom(ryw_Event.JuQingGuide);
    }
    // update (dt) {}
}