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 WaiMaiQiuJiuScript 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/WaiMaiQiuJiuConfig', (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(() => {
                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);

        this.showDuiHua(duiChooseInfo, () => {
            if (duiChooseInfo.isCorrect) {
                this.curSchedule++;
                if (this.curSchedule >= this.duiListConfig.length) {
                    this.unlockLevel(this.curSchedule - 2);
                    cc.tween(this.getLayerChild('success'))
                        .delay(1)
                        .set({ active: true, opacity: 0 })
                        .to(1, { opacity: 255 }, { easing: 'sineOut' })
                        .call(() => {
                            this.getLayerChild('success', 'tabLab2').active = true;
                        })
                        .delay(3)
                        .call(() => {
                            this.endGameView(1);
                        })
                        .start();
                } else if (this.curSchedule == this.duiListConfig.length - 2) {
                    this.unlockLevel(this.curSchedule - 1);
                    this.getLayerChild("chadianNodeList").active = true;
                    this.refreshSchedule();
                } else {
                    this.unlockLevel(this.curSchedule - 1);
                    this.refreshSchedule();
                }
            } else {
                this.node.getChildByName("inputBlock").active = true;
                if (!duiChooseInfo.badEnd) {
                    this.showDuiHua(this.duihuaArrayConfig[0], () => {
                        Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/挥刀');
                        CccGame.playAnimation2(this.getLayerChild('突脸'), '突脸', false, null, () => {
                            Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/女生尖叫');
                            this.scheduleOnce(() => {
                                this.endGameView(0);
                            }, 3);
                        });
                    })
                } else if (duiChooseInfo.badEnd == 1) {
                    this.showDuiHua(this.duihuaArrayConfig[1], () => {
                        this.getLayerChild('治安岗亭', '没团外卖').active = false;
                        CccGame.playAnimation2(this.getLayerChild('治安岗亭', '骑摩托'), '骑摩托', false, null, () => {
                            this.showDuiHua(this.duihuaArrayConfig[0], () => {
                                Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/挥刀');
                                CccGame.playAnimation2(this.getLayerChild('突脸'), '突脸', false, null, () => {
                                    Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/女生尖叫');
                                    this.scheduleOnce(() => {
                                        this.endGameView(0);
                                    }, 3);
                                });
                            })
                        });
                    })
                } else if (duiChooseInfo.badEnd == 2) {
                    this.showDuiHua(this.duihuaArrayConfig[2], () => {
                        this.getLayerChild('治安岗亭', '没团外卖').active = false;
                        CccGame.playAnimation2(this.getLayerChild('治安岗亭', '骑摩托'), '骑摩托', false, null, () => {
                            this.showDuiHua(this.duihuaArrayConfig[0], () => {
                                Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/挥刀');
                                CccGame.playAnimation2(this.getLayerChild('突脸'), '突脸', false, null, () => {
                                    Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/女生尖叫');
                                    this.scheduleOnce(() => {
                                        this.endGameView(0);
                                    }, 3);
                                });
                            })
                        });
                    })
                }
            }
        });
    }

    //触摸回调
    normalTouchCallback(targetNode: cc.Node) {
        const normalTouchLogic = {
            毯子: () => {
                this.curSchedule++;
                const dui_ = this.duiListConfig[this.curSchedule];
                if (dui_) {
                    let scr: DuiChoosePrefab = this.duiChoosePrefab.getComponent("DuiChoosePrefab");
                    scr.content.children.forEach(element => {
                        scr['setItem'](element);
                    });
                    scr.setDuiChooseList(dui_.answerArray);
                    this.duiChoosePrefab.active = true;
                }
            },
            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) { }
}