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

let gameConfig = {
    chadianLog1: [
        {
            str: "女儿怎么样了?",
            qiPaoPos: -1,
            delay: 0.8,
            effectUrl: "GameRoomRes/sound/Room22/女儿怎么样了?"
        },
        {
            str: "爸爸妈妈,太好了",
            qiPaoPos: 1,
            delay: 2.3,
            effectUrl: "GameRoomRes/sound/Room22/爸爸妈妈,太好了"
        },
        {
            str: "夏芸,当初跟陈天霸走,就因为我穷我失业?",
            qiPaoPos: -1,
            delay: 3.8,
            effectUrl: "GameRoomRes/sound/Room22/夏芸,当初跟陈天霸走,就因为我穷我失业?"
        },
        {
            str: "女儿的病拖不起了,要很多很多的钱",
            qiPaoPos: 0,
            delay: 3,
            effectUrl: "GameRoomRes/sound/Room22/女儿的病拖不起了,要很多很多的钱"
        },
        {
            str: "那你现在回来干什么!",
            qiPaoPos: -1,
            delay: 1.4,
            effectUrl: "GameRoomRes/sound/Room22/那你现在回来干什么!"
        },
        {
            str: "陈天霸在海外的儿子重病,女儿的条件刚好匹配",
            qiPaoPos: 0,
            delay: 4,
            effectUrl: "GameRoomRes/sound/Room22/陈天霸在海外的儿子重病,女儿的条件刚好匹配"
        },
        {
            str: "可惜鲁菜谱没拿回来",
            qiPaoPos: 0,
            delay: 2.1,
            effectUrl: "GameRoomRes/sound/Room22/可惜鲁菜谱没拿回来"
        },
        {
            str: "你照顾好女儿,我去把家里装修一下",
            qiPaoPos: -1,
            delay: 2.2,
            effectUrl: "GameRoomRes/sound/Room22/你照顾好女儿,我去把家里装修一下"
        }
    ]
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class Room22 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_2201) {
            this.node.getChildByName("底图1").active = true;
            this.node.getChildByName("底图1").getChildByName("小女孩睡").active = true;
            let wife = this.node.getChildByName("老婆");
            wife.active = true;
            wife.getComponent(sp.Skeleton).setAnimation(0, '担心', true);
            this.showQiPao(gameConfig.chadianLog1[0], () => {
                this.node.getChildByName('熬制膳食').active = true;
            });
        } else if (mainId == MainTaskIdEnum.MainTask_2202) {
            this.node.getChildByName("底图1").active = true;
            this.node.getChildByName("底图1").getChildByName("小女孩睡").active = true;
            let wife = this.node.getChildByName("老婆");
            wife.active = true;
            wife.getComponent(sp.Skeleton).setAnimation(0, '担心', true);
            this.node.getChildByName('膳食提交').active = true;
        } else if (mainId == MainTaskIdEnum.MainTask_2203) {
            this.node.getChildByName("底图1").active = true;
            this.node.getChildByName("底图1").getChildByName("小女孩坐").active = true;
            let wife = this.node.getChildByName("老婆");
            wife.active = true;
            wife.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
            const dialogEvents = [
                (func) => {
                    this.showQiPao(gameConfig.chadianLog1[2], func);
                },
                (func) => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '待机说话', true);
                    this.showQiPao(gameConfig.chadianLog1[3], func);
                },
                (func) => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                    this.showQiPao(gameConfig.chadianLog1[4], func);
                },
                (func) => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '待机说话', true);
                    this.showQiPao(gameConfig.chadianLog1[5], func);
                },
                (func) => {
                    this.showQiPao(gameConfig.chadianLog1[6], func);
                },
                (func) => {
                    wife.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                    this.showQiPao(gameConfig.chadianLog1[7], func);
                },
            ]
            const dialogCallFunc = (logEvents: any[]) => {
                logEvents.shift()(() => {
                    if (logEvents.length > 0) {
                        dialogCallFunc(logEvents);
                    } else {
                        console.log("MainTask_2203对话结束!");
                    }
                });
            }
            dialogCallFunc(dialogEvents);
        } else if (mainId == MainTaskIdEnum.MainTask_2204) {
            this.node.getChildByName("底图1").active = true;
            this.node.getChildByName("底图1").getChildByName("小女孩坐").active = true;
            let wife = this.node.getChildByName("老婆");
            wife.active = true;
            wife.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
            this.node.getChildByName('装修家里').active = true;
        } else {
            this.node.getChildByName('装修家里').active = false;
            this.node.getChildByName("底图2").active = true;
            let wife = this.node.getChildByName("老婆");
            wife.active = true;
            wife.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
        }
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "膳食提交":
                let node1 = this.node.getChildByName("底图1").getChildByName("小女孩睡");
                let node2 = this.node.getChildByName("底图1").getChildByName("小女孩坐");
                cc.tween(node1).to(1, { opacity: 0 }).hide().start();
                cc.tween(node2).delay(0.5).set({ active: true, opacity: 0 })
                    .to(1, { opacity: 255 }).call(() => {
                        this.showQiPao(gameConfig.chadianLog1[1], () => {
                            BagManager.subBagList(3052, 1);
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_2202);
                            TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_2203);
                            EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                        });
                    }).start();
                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;
        let goodArray = [{ goodId: 3052, goodNum: 1 }];
        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
            prefabNode.getComponent(GetAward).initView(goodArray, () => {
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_2201);
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_2202);
                EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
            });
        });
    }

    onStartGameBtn2(event) {
        // let target = event.target;
        // target.active = false;
        console.log('游戏文字剧情22-查询真相');
        PrefabManage.loadPrefabByType(GameType.GameStory22);
    }
}