import DaDianScript from "../../../../FrameWork/Base/DaDianScript";
import WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
import { ryw_Event } from "../../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../../FrameWork/Event/EventMgr";
import Common from "../../../../FrameWork/Util/Common";
import Common5 from "../../../../Platform/th/Common5";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
import CccGame from "../../../WordGame/gameComScript/CccGame/CccGame";
import DuiChoosePrefab from "../../../WordGame/gameComScript/DuiChoosePrefab";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MaiYouTiaoScript extends WordGameBaseComponent {
    @property(cc.Node)
    layerNode: cc.Node[] = [];

    @property(cc.Node)
    duiChoosePrefab: cc.Node = null;

    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule: number = 0;
    healthPoint: number = 0;

    //文本配置
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    duiListConfig: any[] = [];
    wrongAnswerConfig: string[] = [];
    answersIndexConfig: number[] = [];

    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        //this.openTouchEvent(this.node.getChildByName('bg'));
        Common5.stopMusic();

        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle, 'script/MaiYouTiaoConfig', (assest) => {
            this.jsonData = assest.json;
            this.initParameters();
            this.initComponent();
        })

    }

    //初始化参数
    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;
        this.healthPoint = 3;

        this.titleArrayConfig = this.jsonData.titleArray;
        this.duihuaArrayConfig = this.jsonData.duihuaArray;
        this.duiListConfig = this.jsonData.duiList;
        this.wrongAnswerConfig = this.jsonData.wrongAnswer;
        this.answersIndexConfig = this.jsonData.answersIndex;

        Common.Type = 3;
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.answersArray;

        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.DuiChooseResult, (data_) => {
            this.duiChooseResultCallback(data_);
        }, this);
    }

    //初始化组件
    initComponent() {
        for (const node of this.layerNode) {
            node.active = false;
        }
        let node = this.layerNode[this.curLayerIndex];
        node.active = true;

        Game.ins.setGameTitle(this.titleArrayConfig[0]);
        this.duiChoosePrefab.active = false;
        //Common5.playMusicCustom(Common5.selectGameInfo.bundle, 'sound/背景音');

        this.node.getChildByName('inputBlock').active = true;
        this.showDuiHua(this.duihuaArrayConfig[0], () => {
            this.node.getChildByName('inputBlock').active = false;
            this.refreshSchedule();
        });
    }

    //刷新进度
    refreshSchedule() {
        const dui_ = this.duiListConfig[this.curSchedule];

        if (dui_) {
            this.showDuiHua(dui_.npcLog[0]);
            this.scheduleOnce(() => {
                CccGame.playAnimation2(this.getLayerChild("员工"), "懵逼", true);
                let scr: DuiChoosePrefab = this.duiChoosePrefab.getComponent("DuiChoosePrefab");
                scr.setDuiChooseList(dui_.answerArray);
                this.duiChoosePrefab.active = true;
            }, 3);
        }
    }

    duiChooseResultCallback(data_: any) {
        const dui_ = this.duiListConfig[this.curSchedule];
        let duiChooseInfo = dui_.answerArray[data_.touchIndex];

        console.log("++++++++++++++++++touchIndex + duiChooseResult: ", data_.touchIndex, duiChooseInfo.isCorrect);
        this.duiChoosePrefab.active = false;
        this.closeDuiHua(0);

        const callback = () => {
            this.curSchedule++;
            if (this.healthPoint <= 0) {
                this.getLayerChild("hp").active = false;
                this.scheduleOnce(() => {
                    this.endGameView(0);
                }, 2);
            } else {
                if (this.curSchedule >= this.duiListConfig.length) {
                    this.getLayerChild("hp").active = false;
                    this.scheduleOnce(() => {
                        this.endGameView(1);
                    }, 2);
                } else if (this.curSchedule == this.duiListConfig.length - 1) {
                    this.getLayerChild("chadianNodeList").active = true;
                    this.refreshSchedule();
                } else {
                    this.refreshSchedule();
                }
            }
        };

        this.showDuiHua(duiChooseInfo, () => {
            if (duiChooseInfo.isCorrect) {
                CccGame.playAnimation2(this.getLayerChild("员工"), "待机", true);
                this.showDuiHua(dui_.npcLog[1], () => {
                    const role = this.getLayerChild("老板");
                    CccGame.playAnimation2(role, "笑", true);
                    Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/大拇指');
                    this.unlockLevel(this.curSchedule);
                    this.scheduleOnce(() => {
                        CccGame.playAnimation2(role, "待机", true);
                        callback();
                    }, 1.5);
                });
            } else {
                const role = this.getLayerChild("员工");
                CccGame.playAnimation2(role, "被劈", true);
                Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/打雷');
                this.healthPoint--;
                CccGame.playAnimation2(this.getLayerChild("hp").children[this.healthPoint], "心碎");
                this.scheduleOnce(() => {
                    this.getLayerChild("hp").children[this.healthPoint].active = false;
                }, 1);
                this.scheduleOnce(() => {
                    CccGame.playAnimation2(role, "待机", true);
                    callback();
                }, 1.5);
            }
        });
    }

    //触摸回调
    normalTouchCallback(targetNode: cc.Node) {
        const normalTouchLogic = {
            剃须刀: () => {
                this.duiChoosePrefab.active = false;
                this.closeDuiHua(0);
                const dui_ = this.duiListConfig[this.curSchedule];
                this.showDuiHua(dui_.npcLog[1], () => {
                    const role = this.getLayerChild("老板");
                    CccGame.playAnimation2(role, "笑", true);
                    //Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/雷');
                    this.unlockLevel(this.curSchedule);
                    this.scheduleOnce(() => {
                        CccGame.playAnimation2(role, "待机", true);
                        this.curSchedule++;
                        this.getLayerChild("hp").active = false;
                        this.scheduleOnce(() => {
                            this.endGameView(1);
                        }, 2);
                    }, 1.5);
                });
            },
            prop: () => { },
        }


        normalTouchLogic[targetNode.name]?.(targetNode);
    }

    //显示对话
    showDuiHua(curLog, func?) {
        console.log("curLog==", curLog);

        if (curLog.effectUrl) {
            Common5.playEffectCustom(Common5.selectGameInfo.bundle, curLog.effectUrl);
        }

        let string_ = curLog.str;
        let qiPaoPos_ = curLog.qiPaoPos;
        let delayTime_ = curLog.delayTime || 3;

        let node = this.layerNode[this.curLayerIndex];
        if (qiPaoPos_ != -1) {
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions()
            qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            cc.tween(qiPao)
                .set({ active: true, scale: 0 })
                .to(0.2, { scale: 1 })
                .delay(delayTime_)
                .set({ active: false })
                .start();
        }
        else {
            let logPrefab = this.node.getChildByName('logPrefab');
            let script_: logPrefabScript = logPrefab.getComponent('logPrefabScript');
            script_.setDailogShow(string_, delayTime_);
        }

        this.scheduleOnce(() => {
            if (func) {
                func();
            }
        }, delayTime_);
    }

    closeDuiHua(qiPaoPos_) {
        if (qiPaoPos_ != -1) {
            let node = this.layerNode[this.curLayerIndex];
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions();
            qiPao.active = false;
        } else {
            let logPrefab = this.node.getChildByName('logPrefab');
            let duihua = logPrefab.getChildByName('duihua');
            duihua.stopAllActions();
            duihua.active = false;
        }
    }

    //获取当前场景下的节点
    getLayerChild(...path: string[]) {
        let child = this.layerNode[this.curLayerIndex];
        if (path.length > 1) {
            for (const name of path) {
                child = child.getChildByName(name);
            }
            return child;
        }
        return child.getChildByName(path[0]);
    }

    //结束弹窗
    endGameView(touchIndex) {
        this.node.getChildByName("inputBlock").active = true;
        Game.ins.stopTime();
        if (touchIndex == 0) {
            Game.ins.showFail();
        } else {
            Game.ins.showSuccess();
        }
    }

    //解锁提示
    unlockLevel(sublevel) {
        Game.ins.tipUnlock(sublevel)
    }

    //update(dt) { }
}