import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import Common5 from "../../../Platform/th/Common5";
import JuQingManager from "../../JuQingChat/JuQingManager";
import TaskManager, { MainTaskIdEnum } from "../../JuQingChat/TaskManager";
import JuQingQiPao from "../../JuQingGuanQia/JuQingQiPao";
import BagManager from "../../Manager/BagManager";
import UserManager from "../../Manager/UserManager";
import PrefabManage, { GameType } from "../../PrefabManager/PrefabManage";
import GetAward from "../../SCommon/GetAward";
import GameBaseScript from "../GameBaseScript";

let gameConfig = {
    chadianLog1: [
        {
            str: "师妹和陈天霸,怎么吵起来了?",
            qiPaoPos: -1,
            delay: 2.6,
            effectUrl: "GameRoomRes/sound/Room6/师妹和陈天霸,怎么吵起来了?"
        },
        {
            str: "西餐的甜点加辣椒!这个月工资扣光!",
            qiPaoPos: 0,
            delay: 3.7,
            effectUrl: "GameRoomRes/sound/Room6/西餐的甜点加辣椒!这个月工资扣光!"
        },
        {
            str: "老娘不干了,谁爱做谁做!",
            qiPaoPos: 1,
            delay: 2.3,
            effectUrl: "GameRoomRes/sound/Room6/老娘不干了,谁爱做谁做!"
        },
        {
            str: "我们签了合同的,要走,赔违约金",
            qiPaoPos: 0,
            delay: 4.2,
            effectUrl: "GameRoomRes/sound/Room6/我们签了合同的,要走,赔违约金"
        },
        {
            str: "她的违约金我付了!",
            qiPaoPos: -1,
            delay: 1.6,
            effectUrl: "GameRoomRes/sound/Room6/她的违约金我付了!"
        },
        {
            str: "师哥,我的厨具和川菜御厨匾,都封在深海了",
            qiPaoPos: 2,
            delay: 4.8,
            effectUrl: "GameRoomRes/sound/Room6/师哥,我的厨具和川菜御厨匾,都封在深海了"
        }
    ]
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class Room6 extends cc.Component {

    onLoad() {
        GameBaseScript.preLoadRemoteAudio(gameConfig);
        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);
        EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);
    }

    start() {
        this.showChatBtnStatus();
        EventMgr.onEvent_custom(ryw_Event.RefreshJuQingDuiHua, () => {
            this.showChatBtnStatus();
        }, this);
    }

    showChatBtnStatus() {
        let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if (mainId == MainTaskIdEnum.MainTask_601) {
            this.node.getChildByName('nameplate').active = true;
            let shimei = this.node.getChildByName('师妹');
            shimei.active = true;
            shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹生气', true);
            let boss = this.node.getChildByName('老板');
            boss.active = true;
            const dialogEvents = [
                (func) => {
                    this.showQiPao(gameConfig.chadianLog1[0], func);
                },
                (func) => {
                    boss.getComponent(sp.Skeleton).setAnimation(0, '老板说话', true);
                    this.showQiPao(gameConfig.chadianLog1[1], func);
                },
                (func) => {
                    boss.getComponent(sp.Skeleton).setAnimation(0, '老板', true);
                    this.showQiPao(gameConfig.chadianLog1[2], func);
                },
                (func) => {
                    boss.getComponent(sp.Skeleton).setAnimation(0, '老板说话', true);
                    this.showQiPao(gameConfig.chadianLog1[3], func);
                }
            ]

            const dialogCallFunc = (logEvents: any[]) => {
                logEvents.shift()(() => {
                    if (logEvents.length > 0) {
                        dialogCallFunc(logEvents);
                    } else {
                        boss.getComponent(sp.Skeleton).setAnimation(0, '老板', true);
                        this.node.getChildByName('劳动合同').active = true;
                    }
                });
            }
            dialogCallFunc(dialogEvents);
        } else if (mainId == MainTaskIdEnum.MainTask_602) {
            let shimei = this.node.getChildByName('师妹');
            shimei.active = true;
            let boss = this.node.getChildByName('老板');
            boss.active = true;
            shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹生气', true);
            this.node.getChildByName('钞票').active = true;
        } else {
            let shimei = this.node.getChildByName('师妹');
            shimei.active = true;
            shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹开心', true);
        }
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "关闭劳动合同":
                this.showQiPao(gameConfig.chadianLog1[4], () => {
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_601);
                    TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_602);
                    this.node.getChildByName('钞票').active = true;
                });
                break;
            case "钞票":
                if (UserManager.getCurMoney() >= 150000) {
                    let shimei = this.node.getChildByName('师妹');
                    let boss = this.node.getChildByName('老板');
                    boss.active = false;
                    let boss2 = this.node.getChildByName('老板握拳');
                    boss2.active = true;
                    boss2.getComponent(sp.Skeleton).setAnimation(0, '老板握拳', true);
                    cc.tween(boss2).delay(1).to(1, { opacity: 0 }).call(() => {
                        shimei.active = false;
                        this.node.getChildByName('师妹拥抱').active = true;
                        this.node.getChildByName('师妹拥抱').getComponent(sp.Skeleton).setAnimation(0, 'animation', false);
                    }).delay(1).call(() => {
                        this.node.getChildByName('师妹拥抱').getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                        //shimei.active = true;
                        //this.node.getChildByName('师妹拥抱').active = false;
                        //shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹开心', true);
                        this.showQiPao(gameConfig.chadianLog1[5], () => {
                            this.node.getChildByName('师妹拥抱').getComponent(sp.Skeleton).setAnimation(0, '呼吸', true);
                            UserManager.subMoney(150000);
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_602);
                            TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_603);
                        });
                    }).hide().start();
                } else {
                    PrefabManage.showTextTips("余额不足!");
                    targetNode.active = true;
                }
                break;
            default:
                break;
        }
    }

    onStartGameBtn1() {
        // console.log('游戏文字剧情5-查询真相');
        // PrefabManage.loadPrefabByType(GameType.GameStory5);
    }

    //展示气泡
    showQiPao(curLog, func?) {
        console.log("curLog==", curLog)
        if (!curLog) {
            console.log("xxxxxx")
            return;
        }
        let string_ = curLog.str
        let qiPaoPos_ = curLog.qiPaoPos

        if (qiPaoPos_ != -1) {
            if (curLog.effectUrl && curLog.effectUrl.length > 0) {
                Common5.playRemoteAudioEffect(curLog.effectUrl);
            }

            let node = this.node
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + 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(curLog.delay)
                .call(() => {
                    qiPao.active = false;
                    if (func) {
                        func();
                    }
                })
                .start();
        }
        else {
            let qiPao = this.node.getChildByName("dh_qiPao")
            qiPao.getComponent(JuQingQiPao).initView(curLog, func)
        }
    }

    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.07, str.length - 1)
    }
}