import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import Common5 from "../../../Platform/th/Common5";
import ZhongCanTingManager from "../../GameMethodRes/ZhongCanTing/ZhongCanTingManager";
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: 1.1,
            effectUrl: "GameRoomRes/sound/Room8/现在情况怎么样了?"
        },
        {
            str: "客人还在闹,得赶紧配辣椒酱了",
            qiPaoPos: 0,
            delay: 3.8,
            effectUrl: "GameRoomRes/sound/Room8/客人还在闹,得赶紧配辣椒酱了"
        },
        {
            str: "客人越来越多,餐馆需要扩张一下了",
            qiPaoPos: 0,
            delay: 3,
            effectUrl: "GameRoomRes/sound/Room8/客人越来越多,餐馆需要扩张一下了"
        }
    ]
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class Room8 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_801) {
            this.node.getChildByName('nameplate').active = true;
            let xiaoren = this.node.getChildByName('小人');
            xiaoren.active = true;
            xiaoren.getComponent(sp.Skeleton).setAnimation(0, '小黑人', true);
            let shimei = this.node.getChildByName('师妹');
            shimei.active = true;
            shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹急', true);
            this.showQiPao(gameConfig.chadianLog1[0], () => {
                shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹正常说话', true);
                this.showQiPao(gameConfig.chadianLog1[1], () => {
                    shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹正常', true);
                    this.node.getChildByName('融合辣酱').active = true;
                });
            });
        } else if (mainId == MainTaskIdEnum.MainTask_802) {
            let xiaoren = this.node.getChildByName('小人');
            xiaoren.active = true;
            xiaoren.getComponent(sp.Skeleton).setAnimation(0, '小黑人', true);
            let shimei = this.node.getChildByName('师妹');
            shimei.active = true;
            shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹正常', true);
            this.node.getChildByName('魔鬼辣酱').active = true;
        } else if (mainId == MainTaskIdEnum.MainTask_803) {
            let xiaoren = this.node.getChildByName('小人');
            xiaoren.active = true;
            xiaoren.getComponent(sp.Skeleton).setAnimation(0, '小黑人', true);
            let shimei = this.node.getChildByName('师妹');
            shimei.active = true;
            shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹正常', true);
        } else {
            this.node.getChildByName('小黑人').active = true;
            let shimei = this.node.getChildByName('师妹');
            shimei.active = true;
            shimei.getComponent(sp.Skeleton).setAnimation(0, '师妹正常', true);
        }
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "魔鬼辣酱":
                BagManager.subBagList(3021, 1);
                let xiaoren = this.node.getChildByName('小人');
                xiaoren.getComponent(sp.Skeleton).setAnimation(0, '红温人', true);
                Common5.playEffectCustom('Room8', 'sound/喷火');
                this.scheduleOnce(() => {
                    xiaoren.getComponent(sp.Skeleton).setAnimation(0, '小黑人', true);
                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_802);
                    TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_803);
                    ZhongCanTingManager.checkTask();
                }, 3);
                break;
            default:
                break;
        }
    }

    onStartGameBtn1(event) {
        let target = event.target;
        target.active = false;
        let goodArray = [{ goodId: 3021, goodNum: 1 }];
        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
            prefabNode.getComponent(GetAward).initView(goodArray, () => {
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_801);
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_802);
                this.node.getChildByName('魔鬼辣酱').active = true;
            });
        });
    }


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