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

let gameConfig = {
    chadianLog1: [
        {
            str: "怎么回事?",
            qiPaoPos: -1,
            delay: 0.5,
            effectUrl: "GameRoomRes/sound/Room5/怎么回事?"
        },
        {
            str: "林齐!你个废物,想毒害我的员工!",
            qiPaoPos: 0,
            delay: 3.1,
            effectUrl: "GameRoomRes/sound/Room5/林齐!你个废物,想毒害我的员工!"
        },
        {
            str: "没效果?这不是食物中毒!",
            qiPaoPos: -1,
            delay: 2.1,
            effectUrl: "GameRoomRes/sound/Room5/没效果?这不是食物中毒!"
        },
        {
            str: "别装了,起来吧!",
            qiPaoPos: -1,
            delay: 1.3,
            effectUrl: "GameRoomRes/sound/Room5/别装了,起来吧!"
        },
        {
            str: "也是个废物,一点小事都做不好",
            qiPaoPos: 0,
            delay: 3.4,
            effectUrl: "GameRoomRes/sound/Room5/也是个废物,一点小事都做不好"
        },
        {
            str: "这种老板,值得你这么做?",
            qiPaoPos: -1,
            delay: 2,
            effectUrl: "GameRoomRes/sound/Room5/这种老板,值得你这么做?"
        },
        {
            str: "我帮你把川菜菜谱偷过来,能不能收留我?",
            qiPaoPos: 1,
            delay: 3.6,
            effectUrl: "GameRoomRes/sound/Room5/我帮你把川菜菜谱偷过来,能不能收留我?"
        },
        {
            str: "你能带过来再说吧",
            qiPaoPos: -1,
            delay: 1.1,
            effectUrl: "GameRoomRes/sound/Room5/你能带过来再说吧"
        }
    ]
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class Room5 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_501) {
            this.node.getChildByName('老板').active = true;
            this.node.getChildByName('倒地吐').active = true;
            this.node.getChildByName('nameplate').active = true;
            let laoban = this.node.getChildByName('老板');
            laoban.getComponent(sp.Skeleton).setAnimation(0, '老板生气', true);
            this.showQiPao(gameConfig.chadianLog1[0], () => {
                laoban.getComponent(sp.Skeleton).setAnimation(0, '老板生气说话', true);
                this.showQiPao(gameConfig.chadianLog1[1], () => {
                    laoban.getComponent(sp.Skeleton).setAnimation(0, '老板生气', true);
                    this.node.getChildByName('解毒剂框').active = true;
                });
            });
        } else if (mainId == MainTaskIdEnum.MainTask_502) {
            this.node.getChildByName('倒地吐').active = true;
            let laoban = this.node.getChildByName('老板');
            laoban.active = true;
            laoban.getComponent(sp.Skeleton).setAnimation(0, '老板生气', true);
            this.showQiPao(gameConfig.chadianLog1[2], () => {
                this.node.getChildByName('startGameBtn1').active = true;
            });
        } else if (mainId == MainTaskIdEnum.MainTask_503) {
            this.node.getChildByName('倒地吐').active = false;
            this.node.getChildByName('服务员倒地笑').active = true;
            this.node.getChildByName('startGameBtn1').active = false;

            let laoban = this.node.getChildByName('老板');
            laoban.active = true;
            laoban.getComponent(sp.Skeleton).setAnimation(0, '老板笑嘻', true);
            let fuwuyuan = this.node.getChildByName('服务员站');
            this.showQiPao(gameConfig.chadianLog1[3], () => {
                this.node.getChildByName('服务员倒地笑').active = false;
                fuwuyuan.active = true;
                fuwuyuan.getComponent(sp.Skeleton).setAnimation(0, '服务员待机', true);
                laoban.getComponent(sp.Skeleton).setAnimation(0, '老板生气说话', true);
                this.showQiPao(gameConfig.chadianLog1[4], () => {
                    laoban.getComponent(sp.Skeleton).setAnimation(0, '老板生气', true);
                    cc.tween(laoban)
                        .to(1, { opacity: 0 })
                        .call(this.continueJuQing1.bind(this))
                        .start();
                });
            });
        } else {
            this.node.getChildByName('服务员站').active = true;
        }
    }

    continueJuQing1() {
        let logs = [gameConfig.chadianLog1[5], gameConfig.chadianLog1[6], gameConfig.chadianLog1[7]];
        const dialogCallback = (logs: any[], func) => {
            this.showQiPao(logs.shift(), () => {
                if (logs.length > 0) {
                    dialogCallback(logs, func);
                } else {
                    func && func();
                }
            });
        }

        let fuwuyuan = this.node.getChildByName('服务员站');
        dialogCallback(logs, () => {
            cc.tween(fuwuyuan)
                .to(1, { opacity: 0 })
                .call(() => {
                    fuwuyuan.getComponent(sp.Skeleton).setAnimation(0, '服务员递菜单', true);
                })
                .to(1, { opacity: 255 })
                .delay(1)
                .call(() => {
                    if (BagManager.getBagGoodNums(3092) == 0) {
                        let goodArray = [{ goodId: 3092, goodNum: 1 }];
                        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
                            prefabNode.getComponent(GetAward).initView(goodArray, () => {
                                this.node.getChildByName('聘用书').active = true;
                            });
                        });
                    } else {
                        this.node.getChildByName('聘用书').active = true;
                    }
                })
                .start();
        });
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "解毒剂框":
                let node = this.node.getChildByName('解毒剂注射');
                node.active = true;
                node.getComponent(sp.Skeleton).setAnimation(0, '解毒剂注射', false);
                this.scheduleOnce(() => {
                    node.active = false;
                    BagManager.subBagList(3007, 1);
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_501);
                    TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_502);
                    EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                }, 1);
                break;
            case "关闭聘用书":
                let fuwuyuan = this.node.getChildByName('服务员站');
                fuwuyuan.getComponent(sp.Skeleton).setAnimation(0, '服务员待机', true);
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_503);
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_504);
                JuQingManager.unLockNewJuQing('WX_501');
                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)
    }
}