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 UserManager from "../../Manager/UserManager";
import NewGuideScript from "../../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../../PrefabManager/PrefabManage";
import GetAward from "../../SCommon/GetAward";
import GameBaseScript from "../GameBaseScript";

let gameConfig = {
    chadianLog1: [
        {
            str: "你脸上是?",
            qiPaoPos: 1,
            delay: 1,
            effectUrl: "GameRoomRes/sound/Room10/你脸上是?"
        },
        {
            str: "和你没关系!赶快去准备女儿的膳食",
            qiPaoPos: 0,
            delay: 3,
            effectUrl: "GameRoomRes/sound/Room10/和你没关系!赶快去准备女儿的膳食"
        },
        {
            str: "你要苏菜菜谱和苏菜御厨匾?",
            qiPaoPos: 0,
            delay: 3.1,
            effectUrl: "GameRoomRes/sound/Room10/你要苏菜菜谱和苏菜御厨匾?"
        },
        {
            str: "嗯,可惜被陈天霸拍走了",
            qiPaoPos: -1,
            delay: 1.9,
            effectUrl: "GameRoomRes/sound/Room10/嗯,可惜被陈天霸拍走了"
        },
        {
            str: "给我钱,我去拿出来给你,陈天霸压根不在意这个",
            qiPaoPos: 0,
            delay: 4.1,
            effectUrl: "GameRoomRes/sound/Room10/给我钱,我去拿出来给你,陈天霸压根不在意这个"
        },
        {
            str: "我们之间...算了,钱我会准备好",
            qiPaoPos: -1,
            delay: 3.6,
            effectUrl: "GameRoomRes/sound/Room10/我们之间...算了,钱我会准备好"
        },
        {
            str: "菜谱给你,御厨匾我快递发给你,走了",
            qiPaoPos: 0,
            delay: 3.8,
            effectUrl: "GameRoomRes/sound/Room10/菜谱给你,御厨匾我快递发给你,走了"
        }
    ]
}

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

    maskGuideNode: cc.Node = null

    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_1001) {
            this.node.getChildByName('老婆').active = true;
            this.node.getChildByName("nameplate").active = true;
            let node = this.node.getChildByName("巴掌印");
            node.active = true;
            this.guideView([node]);
        } else if (mainId == MainTaskIdEnum.MainTask_1002) {
            this.node.getChildByName('老婆').active = true;
            this.node.getChildByName("膳食提交").active = true;
        } else if (mainId == MainTaskIdEnum.MainTask_1003) {
            this.node.getChildByName('老婆').active = true;
            this.node.getChildByName('钞票').active = true;
        } else {
            this.node.getChildByName('老婆').active = true;
        }
    }

    normalTouchCallback(targetNode) {
        let laopo = this.node.getChildByName('老婆');
        switch (targetNode.name) {
            case "巴掌印":
                this.showQiPao(gameConfig.chadianLog1[0], () => {
                    laopo.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                    this.showQiPao(gameConfig.chadianLog1[1], () => {
                        laopo.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        this.node.getChildByName('熬制膳食').active = true;
                    });
                });
                break;
            case "膳食提交":
                const dialogEvents = [
                    (func) => {
                        laopo.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                        this.showQiPao(gameConfig.chadianLog1[2], func);
                    },
                    (func) => {
                        laopo.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        this.showQiPao(gameConfig.chadianLog1[3], func);
                    },
                    (func) => {
                        laopo.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                        this.showQiPao(gameConfig.chadianLog1[4], func);
                    },
                    (func) => {
                        laopo.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        this.showQiPao(gameConfig.chadianLog1[5], func);
                    }
                ]
                const dialogCallFunc = (logEvents: any[]) => {
                    logEvents.shift()(() => {
                        if (logEvents.length > 0) {
                            dialogCallFunc(logEvents);
                        } else {
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1002);
                            TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1003);
                            BagManager.subBagList(3023, 1);
                            this.node.getChildByName('钞票').active = true;
                        }
                    });
                }
                dialogCallFunc(dialogEvents);
                break;
            case "钞票":
                if (UserManager.getCurMoney() >= 300000) {
                    laopo.getComponent(sp.Skeleton).setAnimation(0, '说话', true);
                    this.showQiPao(gameConfig.chadianLog1[6], () => {
                        laopo.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        cc.tween(laopo).to(1, { opacity: 0 }).call(() => {
                            let goodArray = [{ goodId: 3093, goodNum: 1 }];
                            PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
                                prefabNode.getComponent(GetAward).initView(goodArray, () => {
                                    UserManager.subMoney(300000);
                                    TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1003);
                                    TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1004);
                                });
                            });
                        }).hide().start();
                    });
                } else {
                    PrefabManage.showTextTips("余额不足!");
                    targetNode.active = true;
                }
                break;
            default:
                break;
        }
    }

    onStartGameBtn1(event) {
        let target = event.target;
        target.active = false;
        let goodArray = [{ goodId: 3023, goodNum: 1 }];
        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
            prefabNode.getComponent(GetAward).initView(goodArray, () => {
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1001);
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1002);
                EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
            });
        });
    }

    guideView(nodeArray) {
        if (this.maskGuideNode == null) {
            PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab) => {
                let guideNodeArray = nodeArray
                this.maskGuideNode = prefab
                let firstNode = guideNodeArray.shift()
                prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            })
        } else {
            this.maskGuideNode.active = true
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            this.maskGuideNode.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
        }
    }

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