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

let gameConfig = {
    chadianLog1: [
        {
            str: "那不是陈天霸么,他们吵什么",
            qiPaoPos: -1,
            delay: 3.1,
            effectUrl: "GameRoomRes/sound/Room9/那不是陈天霸么,他们吵什么"
        },
        {
            str: "陈氏西餐?不管你是谁,没有入场券不能进!",
            qiPaoPos: 0,
            delay: 3.8,
            effectUrl: "GameRoomRes/sound/Room9/陈氏西餐?不管你是谁,没有入场券不能进!"
        },
        {
            str: "他怎么可以进去?保安,快把他拦住!",
            qiPaoPos: 1,
            delay: 3,
            effectUrl: "GameRoomRes/sound/Room9/他怎么可以进去?保安,快把他拦住!"
        },
        {
            str: "入场券?可以带宠物入场?那我带陈天霸进去吧",
            qiPaoPos: -1,
            delay: 4.4,
            effectUrl: "GameRoomRes/sound/Room9/入场券?可以带宠物入场?那我带陈天霸进去吧"
        },
        {
            str: "下一个展品,苏菜菜谱和御厨匾,起拍价200万",
            qiPaoPos: 2,
            delay: 6.2,
            effectUrl: "GameRoomRes/sound/Room9/下一个展品,苏菜菜谱和御厨匾,起拍价200万"
        },
        {
            str: "把我当宠物是吧,你别想拍到你想要的",
            qiPaoPos: 3,
            delay: 4.5,
            effectUrl: "GameRoomRes/sound/Room9/把我当宠物是吧,你别想拍到你想要的"
        },
        {
            str: "可恶!",
            qiPaoPos: -1,
            delay: 1,
            effectUrl: "GameRoomRes/sound/Room9/可恶!"
        },
        {
            str: "跟我比钱?来啊,顺带告诉你,你老婆还真不赖",
            qiPaoPos: 3,
            delay: 5.3,
            effectUrl: "GameRoomRes/sound/Room9/跟我比钱?来啊,顺带告诉你,你老婆还真不赖"
        },
        {
            str: "疯子,只能先放弃了",
            qiPaoPos: -1,
            delay: 2,
            effectUrl: "GameRoomRes/sound/Room9/疯子,只能先放弃了"
        }
    ]
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class Room9 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 staff = this.node.getChildByName('保安');
        let boss = this.node.getChildByName('老板');
        let paimainv = this.node.getChildByName('拍卖女');
        let leadSeat = this.node.getChildByName('男主举牌');
        let bossSeat = this.node.getChildByName('老板举牌');
        let otherSeat = this.node.getChildByName('其他人');

        if (mainId == MainTaskIdEnum.MainTask_901) {
            this.node.getChildByName('提交入场券').active = true;
        } else if (mainId == MainTaskIdEnum.MainTask_902) {
            this.node.getChildByName('nameplate').active = true;
            Common5.playEffectCustom('Room9', 'sound/陈天霸和保安吵架');
            staff.active = true;
            boss.active = true;
            const dialogEvents = [
                (func) => {
                    staff.getComponent(sp.Skeleton).setAnimation(0, '保安说话', true);
                    boss.getComponent(sp.Skeleton).setAnimation(0, '老板说话', true);
                    this.showQiPao(gameConfig.chadianLog1[0], func);
                },
                (func) => {
                    staff.getComponent(sp.Skeleton).setAnimation(0, '保安说话', true);
                    this.showQiPao(gameConfig.chadianLog1[1], func);
                },
                (func) => {
                    staff.getComponent(sp.Skeleton).setAnimation(0, '保安', true);
                    boss.getComponent(sp.Skeleton).setAnimation(0, '老板指人说话', true);
                    this.showQiPao(gameConfig.chadianLog1[2], func);
                }
            ]

            const dialogCallFunc = (logEvents: any[]) => {
                logEvents.shift()(() => {
                    if (logEvents.length > 0) {
                        dialogCallFunc(logEvents);
                    } else {
                        boss.getComponent(sp.Skeleton).setAnimation(0, '老板指人', true);
                        this.node.getChildByName('入场券').active = true;
                        this.scheduleOnce(() => {
                            this.node.getChildByName('入场券').active = false;
                            this.showQiPao(gameConfig.chadianLog1[3], () => {
                                let boss = this.node.getChildByName('老板');
                                let staff = this.node.getChildByName('保安');
                                boss.getComponent(sp.Skeleton).setAnimation(0, '老板握拳', true);

                                cc.tween(staff).delay(1).to(1, { opacity: 0 }).hide().start();
                                cc.tween(boss).delay(1).to(1, { opacity: 0 }).call(() => {
                                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_902);
                                    TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_903);
                                    EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                                }).hide().start();
                            });
                        }, 2);
                    }
                });
            }
            dialogCallFunc(dialogEvents);
        } else if (mainId == MainTaskIdEnum.MainTask_903) {
            paimainv.active = true;
            otherSeat.active = true;
            leadSeat.active = true;
            bossSeat.active = true;

            const dialogEvents = [
                (func) => {
                    this.showQiPao(gameConfig.chadianLog1[4], func);
                },
                (func) => {
                    this.showQiPao(gameConfig.chadianLog1[5], func);
                },
                (func) => {
                    bossSeat.getComponent(sp.Skeleton).setAnimation(0, '举牌', false);
                    this.scheduleOnce(() => {
                        bossSeat.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        this.showQiPao(gameConfig.chadianLog1[6], func);
                    }, 1);
                },
                (func) => {
                    leadSeat.getComponent(sp.Skeleton).setAnimation(0, '举牌', false);
                    this.scheduleOnce(() => {
                        leadSeat.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        this.showQiPao(gameConfig.chadianLog1[7], func);
                    }, 1);
                },
                (func) => {
                    bossSeat.getComponent(sp.Skeleton).setAnimation(0, '举牌', false);
                    this.scheduleOnce(() => {
                        bossSeat.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        this.showQiPao(gameConfig.chadianLog1[8], func);
                    }, 1);
                }
            ]

            const dialogCallFunc = (logEvents: any[]) => {
                logEvents.shift()(() => {
                    if (logEvents.length > 0) {
                        dialogCallFunc(logEvents);
                    } else {
                        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_903);
                        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_904);
                        JuQingManager.unLockNewJuQing('WX_901');
                    }
                });
            }
            dialogCallFunc(dialogEvents);

        } else {

        }
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "入场券":
                break;
            default:
                break;
        }
    }

    onStartGameBtn1(event) {
        let target = event.target;
        target.active = false;
        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_901);
        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_902);
        EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
    }


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