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 ChooseBtnLayer from "../../../WordGame/gameComScript/ChooseBtnLayer";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";

const { ccclass, property } = cc._decorator;

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

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

    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule: number = 0;
    chooseIndex: number = -1;
    savePropNameList: string[] = [];

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

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

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

    //初始化参数
    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;
        this.savePropNameList = [];

        this.titleArrayConfig = this.jsonData.titleArray;
        this.duihuaArrayConfig = this.jsonData.duihuaArray;
        this.chadianArrayConfig = this.jsonData.chadianArray;
        this.wrongAnswerConfig = this.jsonData.wrongAnswer;
        this.answersIndexConfig = this.jsonData.answersIndex;
        this.chooseArrayConfig = this.jsonData.chooseArray;

        Common.Type = 0;
        Common.subLevel = this.curLayerIndex;
        Common.GameSubTipConfigs = this.jsonData.tipsArray;
        Common.GameSubAnswerConfigs = 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.chooseResult, (data_) => {
            this.chooseResultCallback(data_.touchIndex);
        }, this);
    }

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

        Game.ins.setGameTitle(this.titleArrayConfig[0]);
        //Common5.playMusicCustom(Common5.selectGameInfo.bundle, 'sound/撞门声');

        this.refreshLayer();
    }

    //刷新层
    refreshLayer() {
        let node = this.layerNode[this.curLayerIndex];
        node.active = true;

        switch (this.curLayerIndex) {
            case 0:
                this.node.getChildByName('inputBlock').active = true;
                this.showDuiHua(this.duihuaArrayConfig[0], () => {
                    const logPrefab2 = this.node.getChildByName('logPrefab2');
                    logPrefab2.active = true;
                    CccGame.playAnimation2(logPrefab2.getChildByName('文字'), '吸引注意');
                    Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/系统出现打字音');
                    this.scheduleOnce(() => {
                        this.node.getChildByName('inputBlock').active = false;
                        logPrefab2.active = false;
                        logPrefab2.getChildByName('文字').active = false;
                        this.showChooseLayer(0);
                    }, 6);
                });
                break;
            case 1:
                this.refreshChooseLayer();
                break;
            case 2:
                if (this.savePropNameList[0] == '服装3' && this.savePropNameList[1] == '美脸' && this.savePropNameList[2] == '发型4') {
                    this.getLayerChild('成功').active = true;
                    this.scheduleOnce(() => {
                        CccGame.playAnimation2(this.getLayerChild('皇上'), '成功');
                        this.getLayerChild('成功', '害羞').active = true;
                        this.showDuiHua(this.duihuaArrayConfig[9], () => {
                            const logPrefab2 = this.node.getChildByName('logPrefab2');
                            logPrefab2.active = true;
                            CccGame.playAnimation2(logPrefab2.getChildByName('文字2'), '回复身份');
                            Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/恭喜小主,你获得了皇上的喜爱,逃离了冷宫并且回复了贵妃身份');
                            this.scheduleOnce(() => {
                                this.endGameView(1);
                            }, 6);
                        });
                    }, 1);
                } else {
                    CccGame.playAnimation2(this.getLayerChild('皇上'), '扇巴掌');
                    this.getLayerChild('失败').active = true;
                    this.getLayerChild('失败', '服装', this.savePropNameList[0]).active = true;
                    this.getLayerChild('失败', '脸庞', this.savePropNameList[1]).active = true;
                    this.getLayerChild('失败', '发型', this.savePropNameList[2]).active = true;
                    this.scheduleOnce(() => {
                        this.getLayerChild('失败').active = false;
                        this.getLayerChild('失败2').active = true;
                        this.getLayerChild('失败2', '发型', this.savePropNameList[2]).active = true;
                        this.getLayerChild('失败2', '服装', this.savePropNameList[0]).active = true;
                        Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/巴掌');
                    }, 0.5);
                    this.scheduleOnce(() => {
                        this.showDuiHua(this.duihuaArrayConfig[8], () => {
                            this.getLayerChild('失败2', '乌云').active = true;
                            Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/被打后人物变黑');
                            this.scheduleOnce(() => {
                                const logPrefab2 = this.node.getChildByName('logPrefab2');
                                logPrefab2.active = true;
                                CccGame.playAnimation2(logPrefab2.getChildByName('文字'), '冒犯圣上');
                                Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/你因面容失仪冒犯圣上,被打入冷宫,永世不得踏出冷宫');
                                this.scheduleOnce(() => {
                                    this.endGameView(0);
                                }, 6);
                            }, 3);
                        });
                    }, 1);

                }
                break;
            case 3:
                this.node.getChildByName('inputBlock').active = true;
                this.showDuiHua(this.duihuaArrayConfig[2], () => {
                    this.node.getChildByName('inputBlock').active = false;
                    this.curLayerIndex = 0;
                    this.nextLayer();
                });
                break;
            default:
                break;
        }
    }

    //刷新选项
    refreshChooseLayer() {
        const systomLogNameList = ['更衣', '修容神器', '发型'];
        this.node.getChildByName('inputBlock').active = true;
        const logPrefab2 = this.node.getChildByName('logPrefab2');
        logPrefab2.active = true;
        CccGame.playAnimation2(logPrefab2.getChildByName('文字'), systomLogNameList[this.curSchedule]);
        Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/系统出现打字音');
        this.scheduleOnce(() => {
            this.node.getChildByName('inputBlock').active = false;
            logPrefab2.active = false;
            logPrefab2.getChildByName('文字').active = false;
            this.getLayerChild('chooseLayerList').children[this.curSchedule].active = true;
            if (this.curSchedule == 2) {
                this.getLayerChild('chadianNodeList', '拖动-发簪').active = true;
            }
        }, 4);
    }

    //触摸回调
    normalTouchCallback(targetNode: cc.Node) {
        const checkNameList = [['服装1', '服装2', '服装3'], ['丑妆', '美脸', '痘痘'], ['发型1', '发型2', '发型3', '发型4']];
        const itemLogic = {
            choose0: (target: cc.Node) => {
                this.getLayerChild('chooseLayerList').children[this.curSchedule].active = false;
                this.showDuiHua(this.duihuaArrayConfig[3], () => {
                    this.getLayerChild('人物1').active = false;
                    this.getLayerChild('身体').active = true;
                    this.getLayerChild('身体', '服装', target.name).active = true;
                    this.savePropNameList[this.curSchedule] = target.name;
                    this.showDuiHua(this.duihuaArrayConfig[4], () => {
                        this.curSchedule++;
                        this.refreshChooseLayer();
                    });
                });
            },
            choose1: (target: cc.Node) => {
                const callback = () => {
                    this.savePropNameList[this.curSchedule] = target.name;
                    this.curSchedule++;
                    this.refreshChooseLayer();
                };

                this.getLayerChild('chooseLayerList').children[this.curSchedule].active = false;
                if (target.name == '痘痘') {
                    this.getLayerChild('身体', '脸庞', target.name).active = true;
                    this.showDuiHua(this.duihuaArrayConfig[6], callback);
                } else if (target.name == '丑妆') {
                    this.getLayerChild('身体', '脸庞', target.name).active = true;
                    this.showDuiHua(this.duihuaArrayConfig[5], callback);
                } else if (target.name == '美脸') {
                    this.getLayerChild('身体', '黄瓜片').active = true;
                    this.scheduleOnce(() => {
                        this.getLayerChild('身体', '黄瓜片').active = false;
                        this.getLayerChild('身体', '脸庞', target.name).active = true;
                        this.showDuiHua(this.duihuaArrayConfig[5], callback);
                    }, 2);
                }
            },
            choose2: (target: cc.Node) => {
                this.getLayerChild('chooseLayerList').children[this.curSchedule].active = false;
                this.getLayerChild('身体', '发型', target.name).active = true;
                this.getLayerChild('身体', '初始发型').active = false;
                this.savePropNameList[this.curSchedule] = target.name;
                this.showDuiHua(this.duihuaArrayConfig[7], () => {
                    this.curSchedule++;
                    this.curLayerIndex = 2;
                    this.nextLayer();
                });
            },
        }

        console.log("+++++++++++++++++++++>" + targetNode.name);
        if (checkNameList[this.curSchedule].includes(targetNode.name)) {
            itemLogic["choose" + this.curSchedule]?.(targetNode);
        }
    }

    //选择结果回调
    chooseResultCallback(touchIndex: number) {
        if (touchIndex == 1) {
            this.showDuiHua(this.duihuaArrayConfig[1], () => {
                this.curLayerIndex = 1;
                this.nextLayer();
            });
        } else {
            this.curLayerIndex = 3;
            this.nextLayer();
        }
    }

    //显示选项
    showChooseLayer(chooseIndex: number) {
        this.chooseLayer.active = true;
        this.chooseIndex = chooseIndex;
        Common.GameSubChooseArray = this.chooseArrayConfig[chooseIndex];
        let script_: ChooseBtnLayer = this.chooseLayer.getComponent('ChooseBtnLayer');
        script_.setChooseBtnView();
    }

    //显示对话
    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_);
    }

    //获取当前场景下的节点
    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]);
    }

    //跳转场景
    nextLayer() {
        for (let i = 0; i < this.layerNode.length; i++) {
            if (this.layerNode[i].active) {
                cc.tween(this.layerNode[i])
                    .delay(1.0)
                    .to(1, { opacity: 0 })
                    .set({ active: false, opacity: 255 })
                    .call(() => {
                        this.refreshLayer();
                    })
                    .start();
            } else {
                this.layerNode[i].active = false;
            }
        };
    }

    //结束弹窗
    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) { }
}