import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import Common5 from "../../../Platform/th/Common5";
import TaskManager, { MainTaskIdEnum } from "../../JuQingChat/TaskManager";
import JuQingQiPao from "../../JuQingGuanQia/JuQingQiPao";
import BagManager from "../../Manager/BagManager";
import GameBaseScript from "../GameBaseScript";

let gameConfig = {
    chadianLog1: [
        {
            str: "菜谱我拿来了,放人",
            qiPaoPos: -1,
            delay: 1.8,
            effectUrl: "GameRoomRes/sound/Room12/菜谱我拿来了,放人"
        },
        {
            str: "真是有情有义啊",
            qiPaoPos: 0,
            delay: 1.7,
            effectUrl: "GameRoomRes/sound/Room12/真是有情有义啊"
        },
        {
            str: "你是看上她了?",
            qiPaoPos: 1,
            delay: 1.3,
            effectUrl: "GameRoomRes/sound/Room12/你是看上她了?"
        },
        {
            str: "老板,救我~",
            qiPaoPos: 2,
            delay: 2,
            effectUrl: "GameRoomRes/sound/Room12/老板,救我~"
        },
        {
            str: "鲁菜,难道爷爷说的是真的....",
            qiPaoPos: 1,
            delay: 3.3,
            effectUrl: "GameRoomRes/sound/Room12/鲁菜,难道爷爷说的是真的...."
        },
        {
            str: "鲁菜菜谱我迟早会回来!",
            qiPaoPos: -1,
            delay: 2.5,
            effectUrl: "GameRoomRes/sound/Room12/鲁菜菜谱我迟早会回来!"
        },
        {
            str: "老板,徽菜菜谱和御厨匾,藏在冰川的企鹅族群里",
            qiPaoPos: 3,
            delay: 5.2,
            effectUrl: "GameRoomRes/sound/Room12/老板,徽菜菜谱和御厨匾,藏在冰川的企鹅族群里"
        }
    ]
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class Room12 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;
        let wife = this.node.getChildByName('老婆');
        let boss = this.node.getChildByName('老板');
        let Sec = this.node.getChildByName('秘书');

        if (mainId == MainTaskIdEnum.MainTask_1201) {
            this.node.getChildByName('nameplate').active = true;
            wife.active = true;
            boss.active = true;
            Sec.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) => {
                    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 {
                        this.node.getChildByName('提交菜谱').active = true;
                    }
                });
            }
            dialogCallFunc(dialogEvents);
        } else if (mainId == MainTaskIdEnum.MainTask_1202) {
            Sec.active = true;
            this.showQiPao(gameConfig.chadianLog1[5], () => {
                this.node.getChildByName('点击解绑').active = true;
            });
        }
    }

    normalTouchCallback(targetNode) {
        let wife = this.node.getChildByName('老婆');
        let boss = this.node.getChildByName('老板');
        switch (targetNode.name) {
            case "提交菜谱":
                BagManager.subBagList(3098, 1);
                cc.tween(boss).to(1, { opacity: 0 }).call(() => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '老婆说话', true);
                    this.showQiPao(gameConfig.chadianLog1[4], () => {
                        wife.getComponent(sp.Skeleton).setAnimation(0, '老婆待机', true);
                        cc.tween(wife).to(1, { opacity: 0 }).call(() => {
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1201);
                            TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1202);
                            EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                        }).hide().start();
                    });
                }).hide().start();
                break;
            case "点击解绑":
                let Sec = this.node.getChildByName('秘书');
                let Sec2 = this.node.getChildByName('秘书靠近');
                Sec.getComponent(sp.Skeleton).setAnimation(0, '秘书解绑', false);
                this.scheduleOnce(() => {
                    Sec.active = false;
                    Sec2.active = true;
                    Sec2.getComponent(sp.Skeleton).setAnimation(0, '靠近', false);
                    this.scheduleOnce(() => {
                        Sec2.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                        this.showQiPao(gameConfig.chadianLog1[6], () => {
                            Sec2.getComponent(sp.Skeleton).setAnimation(0, '消失', false);
                            this.scheduleOnce(() => {
                                Sec2.active = false;
                            }, 1);
                        });
                    }, 0.5);
                }, 1);
                break;
            default:
                break;
        }
    }

    onStartGameBtn1(event) {
        let target = event.target;
        target.active = false;
    }

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