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 GameBaseScript from "../GameBaseScript";

let gameConfig = {
    chadianLog1: [
        {
            str: "陈天霸,正好要找你!",
            qiPaoPos: -1,
            delay: 2,
            effectUrl: "GameRoomRes/sound/Room19/陈天霸,正好要找你!"
        },
        {
            str: "陈总现在有事,你先和我说,我转达吧",
            qiPaoPos: 1,
            delay: 3,
            effectUrl: "GameRoomRes/sound/Room19/陈总现在有事,你先和我说,我转达吧"
        },
        {
            str: "陈天霸!出来,烧楼的事,必须给我个交代",
            qiPaoPos: -1,
            delay: 3.3,
            effectUrl: "GameRoomRes/sound/Room19/陈天霸!出来,烧楼的事,必须给我个交代"
        },
        {
            str: "陈总,他希望您赏脸出去聊一下",
            qiPaoPos: 2,
            delay: 3,
            effectUrl: "GameRoomRes/sound/Room19/陈总,他希望您赏脸出去聊一下"
        },
        {
            str: "先晾晾他,挫挫他的锐气,好吞并他的中餐城",
            qiPaoPos: 0,
            delay: 4.6,
            effectUrl: "GameRoomRes/sound/Room19/先晾晾他,挫挫他的锐气,好吞并他的中餐城"
        },
        {
            str: "你稍等,陈总想和你聊一下中西餐城合作事宜",
            qiPaoPos: 1,
            delay: 3.8,
            effectUrl: "GameRoomRes/sound/Room19/你稍等,陈总想和你聊一下中西餐城合作事宜"
        },
        {
            str: "不可能合作!我一定会把西餐赶回海外",
            qiPaoPos: -1,
            delay: 3.5,
            effectUrl: "GameRoomRes/sound/Room19/不可能合作!我一定会把西餐赶回海外"
        },
        {
            str: "他希望得到您的支持,继续经营中餐厅",
            qiPaoPos: 2,
            delay: 3.1,
            effectUrl: "GameRoomRes/sound/Room19/他希望得到您的支持,继续经营中餐厅"
        },
        {
            str: "100万,我收购他的中餐城,挂陈氏中餐上市",
            qiPaoPos: 0,
            delay: 5.2,
            effectUrl: "GameRoomRes/sound/Room19/100万,我收购他的中餐城,挂陈氏中餐上市"
        },
        {
            str: "做梦,把鲁菜菜谱还回来,不然要你好看!",
            qiPaoPos: -1,
            delay: 3.7,
            effectUrl: "GameRoomRes/sound/Room19/做梦,把鲁菜菜谱还回来,不然要你好看!"
        }
    ]
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class Room19 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_1901) {
            this.node.getChildByName('nameplate').active = true;
            let bg = this.node.getChildByName("背景");
            let wife = this.node.getChildByName("老婆");
            let boss = bg.getChildByName("老板");
            const dialogEvents = [
                (func) => {
                    this.showQiPao(gameConfig.chadianLog1[0], func);
                },
                (func) => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '待机说话', true);
                    this.showQiPao(gameConfig.chadianLog1[1], func);
                },
                (func) => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                    this.showQiPao(gameConfig.chadianLog1[2], func);
                },
                (func) => {
                    cc.tween(bg).to(1, { position: cc.v3(-520, 0) }).call(() => {
                        this.node.getChildByName('nameplate2').active = true;
                        wife.getComponent(sp.Skeleton).setAnimation(0, '待机说话', true);
                        this.showQiPao(gameConfig.chadianLog1[3], func);
                    }).start();
                },
                (func) => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                    boss.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                    this.showQiPao(gameConfig.chadianLog1[4], func);
                },
                (func) => {
                    boss.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                    cc.tween(bg).to(1, { position: cc.v3(575, 0) }).call(() => {
                        wife.getComponent(sp.Skeleton).setAnimation(0, '待机说话', true);
                        this.showQiPao(gameConfig.chadianLog1[5], func);
                    }).start();

                },
                (func) => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                    this.showQiPao(gameConfig.chadianLog1[6], func);
                },
                (func) => {
                    cc.tween(bg).to(1, { position: cc.v3(-520, 0) }).call(() => {
                        wife.getComponent(sp.Skeleton).setAnimation(0, '待机说话', true);
                        this.showQiPao(gameConfig.chadianLog1[7], func);
                    }).start();
                },
                (func) => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                    boss.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                    this.showQiPao(gameConfig.chadianLog1[8], func);
                },
                (func) => {
                    boss.getComponent(sp.Skeleton).setAnimation(0, '扇巴掌', false);
                    Common5.playEffectCustom('Room19', 'sound/巴掌声');
                    this.scheduleOnce(() => {
                        boss.getComponent(sp.Skeleton).setAnimation(0, '老板巴掌待机', true);
                        this.showQiPao(gameConfig.chadianLog1[9], func);
                    }, 1);
                }
            ]
            const dialogCallFunc = (logEvents: any[]) => {
                logEvents.shift()(() => {
                    if (logEvents.length > 0) {
                        dialogCallFunc(logEvents);
                    } else {
                        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1901);
                        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1902);
                    }
                });
            }
            dialogCallFunc(dialogEvents);
        }
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "提交酒菜":
                // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1501);
                // TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1502);
                // EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                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;
    }
}