import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import AppPlatform from "../../../FrameWork/Util/AppPlatform";
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: 3,
            effectUrl: "GameRoomRes/sound/Room15/秘书是有问题,你真是浙菜传承人?"
        },
        {
            str: "确实是,我爷爷重病后才和我说的",
            qiPaoPos: 0,
            delay: 3,
            effectUrl: "GameRoomRes/sound/Room15/确实是,我爷爷重病后才和我说的"
        },
        {
            str: "你爷爷重病?",
            qiPaoPos: -1,
            delay: 0.8,
            effectUrl: "GameRoomRes/sound/Room15/你爷爷重病?"
        },
        {
            str: "对,需要一种特殊的膳食才能救命,你帮帮我",
            qiPaoPos: 0,
            delay: 3.8,
            effectUrl: "GameRoomRes/sound/Room15/对,需要一种特殊的膳食才能救命,你帮帮我"
        },
        {
            str: "这个肯定帮,我这就去准备材料",
            qiPaoPos: -1,
            delay: 2.5,
            effectUrl: "GameRoomRes/sound/Room15/这个肯定帮,我这就去准备材料"
        },
        {
            str: "义父在上,受孩儿一拜",
            qiPaoPos: 1,
            delay: 2.5,
            effectUrl: "GameRoomRes/sound/Room15/义父在上,受孩儿一拜"
        },
        {
            str: "矫情!",
            qiPaoPos: -1,
            delay: 0.8,
            effectUrl: "GameRoomRes/sound/Room15/矫情!"
        },
        {
            str: "浙菜菜谱和御厨匾被拿去当造林的奖励了,你需要这个吧",
            qiPaoPos: 1,
            delay: 4.8,
            effectUrl: "GameRoomRes/sound/Room15/浙菜菜谱和御厨匾被拿去当造林的奖励了,你需要这个吧"
        },
        {
            str: "真是帮大忙了!",
            qiPaoPos: -1,
            delay: 1,
            effectUrl: "GameRoomRes/sound/Room15/真是帮大忙了!"
        }
    ]
}

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

    goOutTimer: number = 999;
    installTimer: number = 999;

    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_1501) {
            this.node.getChildByName('发小坐').active = true;
            this.node.getChildByName('提交酒菜').active = true;
            this.node.getChildByName('nameplate').active = true;
        } else if (mainId == MainTaskIdEnum.MainTask_1502) {
            let faxiao = this.node.getChildByName('发小坐');
            faxiao.active = true;
            this.node.getChildByName('酒').active = true;
            const dialogEvents = [
                (func) => {
                    this.showQiPao(gameConfig.chadianLog1[0], func);
                },
                (func) => {
                    faxiao.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                    this.showQiPao(gameConfig.chadianLog1[1], func);
                },
                (func) => {
                    faxiao.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                    this.showQiPao(gameConfig.chadianLog1[2], func);
                },
                (func) => {
                    faxiao.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                    this.showQiPao(gameConfig.chadianLog1[3], func);
                },
                (func) => {
                    faxiao.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                    this.showQiPao(gameConfig.chadianLog1[4], func);
                }
            ]
            const dialogCallFunc = (logEvents: any[]) => {
                logEvents.shift()(() => {
                    if (logEvents.length > 0) {
                        dialogCallFunc(logEvents);
                    } else {
                        console.log("logEvents.length==0");
                    }
                });
            }
            dialogCallFunc(dialogEvents);
        } else if (mainId == MainTaskIdEnum.MainTask_1503) {
            this.node.getChildByName('发小坐').active = true;
            this.node.getChildByName('酒').active = true;
            this.node.getChildByName('熬药').active = true;
        } else {
            this.node.getChildByName('发小站').active = true;
            this.node.getChildByName('酒').active = true;
        }
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "提交酒菜":
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1501);
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1502);
                EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                break;
            case "提交膳食":
                BagManager.subBagList(3036, 1);
                this.node.getChildByName('发小坐').active = false;
                let faxiao = this.node.getChildByName('发小站');
                faxiao.active = true;
                const dialogEvents = [
                    (func) => {
                        faxiao.getComponent(sp.Skeleton).setAnimation(0, '拜谢', false);
                        this.scheduleOnce(() => {
                            faxiao.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                            this.showQiPao(gameConfig.chadianLog1[5], func);
                        }, 1);
                    },
                    (func) => {
                        faxiao.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        this.showQiPao(gameConfig.chadianLog1[6], func);
                    },
                    (func) => {
                        faxiao.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                        this.showQiPao(gameConfig.chadianLog1[7], func);
                    },
                    (func) => {
                        faxiao.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        this.showQiPao(gameConfig.chadianLog1[8], func);
                    }
                ]
                const dialogCallFunc = (logEvents: any[]) => {
                    logEvents.shift()(() => {
                        if (logEvents.length > 0) {
                            dialogCallFunc(logEvents);
                        } else {
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1503);
                            TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1504);
                        }
                    });
                }
                dialogCallFunc(dialogEvents);
                break;
            default:
                break;
        }
    }

    //展示气泡
    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)
    }

    onStartGameBtn1(event) {
        let target = event.target;
        target.active = false;
        if (BagManager.getBagGoodNums(3036) == 0) {
            let goodArray = [{ goodId: 3036, goodNum: 1 }];
            PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
                prefabNode.getComponent(GetAward).initView(goodArray, () => {
                    this.node.getChildByName('提交膳食').active = true;
                });
            });
        } else {
            this.node.getChildByName('提交膳食').active = true;
        }
    }
}