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 PrefabManage from "../../PrefabManager/PrefabManage";
import CccGame from "../../WenZiRes/WenZiCommon/CccGame";
import GameBaseScript from "../GameBaseScript";

let gameConfig = {
    chadianLog1: [
        {
            str: "先把火灭了",
            qiPaoPos: -1,
            delay: 0.8,
            effectUrl: "GameRoomRes/sound/Room18/先把火灭了"
        },
        {
            str: "他出门了,可以动手了",
            qiPaoPos: 0,
            delay: 1.6,
            effectUrl: "GameRoomRes/sound/Room18/他出门了,可以动手了"
        },
        {
            str: "撒点汽油,多来点",
            qiPaoPos: 1,
            delay: 1.4,
            effectUrl: "GameRoomRes/sound/Room18/撒点汽油,多来点"
        },
        {
            str: "我都做到了,可以放过我了吧",
            qiPaoPos: 0,
            delay: 2,
            effectUrl: "GameRoomRes/sound/Room18/我都做到了,可以放过我了吧"
        },
        {
            str: "被烧了三个菜馆,上市计划得延后了",
            qiPaoPos: -1,
            delay: 3,
            effectUrl: "GameRoomRes/sound/Room18/被烧了三个菜馆,上市计划得延后了"
        },
        {
            str: "挑个礼物,让我满意,我把海外的20亿积蓄拿出来帮你",
            qiPaoPos: 2,
            delay: 5.7,
            effectUrl: "GameRoomRes/sound/Room18/挑个礼物,让我满意,我把海外的20亿积蓄拿出来帮你"
        },
        {
            str: "上道,我最后的积蓄,不能再搞砸了!",
            qiPaoPos: 2,
            delay: 3.8,
            effectUrl: "GameRoomRes/sound/Room18/上道,我最后的积蓄,不能再搞砸了!"
        }
    ]
}

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

    videoStep: number = 0;

    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_1801) {
            this.node.getChildByName('propNodeList').active = true;
            this.node.getChildByName('场景1').active = true;
            this.node.getChildByName('juQingTipNode').active = true;
            this.registerDragEvent();
        } else if (mainId == MainTaskIdEnum.MainTask_1802) {
            this.node.getChildByName('场景2').active = true;
            this.node.getChildByName('查看监控').active = true;
        } else if (mainId == MainTaskIdEnum.MainTask_1803) {
            this.node.getChildByName('场景2').active = true;
        } else if (mainId == MainTaskIdEnum.MainTask_1804) {
            this.node.getChildByName('场景2').active = true;
            this.node.getChildByName('nameplate').active = true;
            let investor = this.node.getChildByName('投资人');
            investor.active = true;
            const dialogEvents = [
                (func) => {
                    this.showQiPao(gameConfig.chadianLog1[4], func);
                },
                (func) => {
                    investor.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 {
                        investor.getComponent(sp.Skeleton).setAnimation(0, '待机', true);
                        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1804);
                        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1805);
                        EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                    }
                });
            }
            dialogCallFunc(dialogEvents);
        } else if (mainId == MainTaskIdEnum.MainTask_1805) {
            this.node.getChildByName('场景2').active = true;
            this.node.getChildByName('提交饰品').active = true;
            let investor = this.node.getChildByName('投资人');
            investor.active = true;
        } else {
            this.node.getChildByName('场景2').active = true;
        }
    }

    //注册道具拖动事件
    registerDragEvent() {
        const hideNodeList = ['火1', '火2', '火3', '火4'];
        const showNodeList = ['烟1', '烟2', '烟3', '烟4'];
        const spineNameList = ['水桶', '水枪', '水盆', '灭火器'];
        const propNodeList = this.node.getChildByName('propNodeList');
        const bg1 = this.node.getChildByName('场景1');
        const targetNodeList = propNodeList.getChildByName('content').children;
        const boxs = [
            bg1.getChildByName('火box1'),
            bg1.getChildByName('火box2'),
            bg1.getChildByName('火box3'),
            bg1.getChildByName('火box4')
        ]

        let curSchedule = 0;
        for (let index = 0; index < targetNodeList.length; index++) {
            let targetNode = targetNodeList[index].children[0];
            let callFuncs = CccGame.onNodeTouchCheckEvent(targetNode, boxs[index], { touchIndex: index }, propNodeList);
            callFuncs.setSuccessListener((data_) => {
                let touchIndex = data_.targetNode.touchIndex;
                let spineNode = bg1.getChildByName(spineNameList[touchIndex]);
                CccGame.playAnimation2(spineNode, spineNameList[touchIndex], false);
                if (data_.targetNode.name == '灭火器') {
                    Common5.playEffectCustom('Room18', 'sound/灭火器');
                } else {
                    Common5.playEffectCustom('Room18', 'sound/水灭火,0.5秒');
                }
                this.scheduleOnce(() => {
                    spineNode.active = false;
                    bg1.getChildByName(hideNodeList[touchIndex]).active = false;
                    bg1.getChildByName(showNodeList[touchIndex]).active = true;
                    curSchedule++;
                    if (curSchedule >= 4) {
                        this.node.getChildByName('juQingTipNode').active = false;
                        propNodeList.active = false;
                        bg1.active = false;
                        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1801);
                        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1802);
                        EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
                    }
                }, 1.5);
            });
            callFuncs.setFailListener((data_) => {
                let wrongTip = this.node.getChildByName('wrongTip')
                wrongTip.stopAllActions()
                cc.tween(wrongTip)
                    .set({ active: true, scale: 0 })
                    .to(0.2, { scale: 1 })
                    .delay(2)
                    .to(0.2, { scale: 0 })
                    .set({ active: false })
                    .start();
            });
        }
    }

    //展示视频
    showVideo() {
        const videoNode = this.node.getChildByName('摄像界面');
        if (videoNode.children[this.videoStep - 1]) {
            videoNode.children[this.videoStep - 1].active = false;
        }
        videoNode.children[this.videoStep].active = true;
        this.showQiPao(gameConfig.chadianLog1[this.videoStep + 1], () => {
            videoNode.getChildByName('摄像箭头').active = true;
            this.videoStep++;
            if (this.videoStep >= 3) {
                videoNode.active = false;
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1802);
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1803);
            };
        });
    }

    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "摄像箭头":
                this.showVideo();
                break;
            case "提交饰品":
                targetNode.active = true;
                let taskInfo: any = TaskManager.getCurUnLockMainTaskInfo();
                if (taskInfo.Id == MainTaskIdEnum.MainTask_1805) {
                    let taskCaiGouDan = TaskManager.getTaskConfigById(taskInfo.Id).taskCaiGouDan;
                    for (const goodId of taskCaiGouDan) {
                        if (BagManager.getBagGoodNums(goodId) == 0) {
                            PrefabManage.showTextTips('请先前往废瓶回收获得物品!');
                            return;
                        }
                    }
                    targetNode.active = false;
                    this.showQiPao(gameConfig.chadianLog1[6], () => {
                        UserManager.addMoney(2000000000);
                        PrefabManage.showTextTips('获得投资金20亿!');
                        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_1805);
                        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_1806);
                    });
                }
                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;
        this.node.getChildByName('摄像界面').active = true;
        this.showVideo();
    }
}