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


const { ccclass, property } = cc._decorator;

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

    @property(cc.Node)
    operateNodeList: cc.Node[] = [];

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

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

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

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

    }

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

        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;

        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);
    }

    //初始化组件
    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]);
        //Common5.playMusicCustom(Common5.selectGameInfo.bundle, 'sound/背景音');

        cc.tween(this.getLayerChild('prop'))
            .to(0.5, { angle: -1 })
            .to(0.5, { angle: 1 })
            .union()
            .repeatForever()
            .start();

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

        const props = this.getLayerChild('prop').children;
        this['wawaProps'] = [];
        for (let index = props.length - 1; index >= 0; index--) {
            this['wawaProps'].push(props[index]);
        }
    }

    refreshSchedule() {
        if (this.curSchedule >= this['wawaProps'].length) {
            this.endGameView(1);
        } else {
            const prop = this['wawaProps'][this.curSchedule];
            const callbacks = CccGame.onNodeTouchMoreCheckEvent(prop, this.getLayerChild('checkNodeList').children, {}, this.getLayerChild('chadianNodeList'));
            callbacks.setSuccessListener((data_) => {
                this.nodeCheckSuccessCallback(data_.targetNode);
            });
            this.curSchedule++;
        }
    }

    nodeCheckSuccessCallback(targetNode: cc.Node) {
        let checkLogic = {
            妈妈: () => {
                this.showWaWa(this.getLayerChild('妈妈', '娃娃'), targetNode.name);
                this.showDuiHua(this.duihuaArrayConfig[1]);
                this.refreshSchedule();
            },
            男1: () => {
                this.getLayerChild('男', '左手2').active = false;
                this.getLayerChild('男', '男左手').active = true;
                this.getLayerChild('checkNodeList', '男2').active = true;
                this.showWaWa(this.getLayerChild('男', '娃娃3'), targetNode.name);
                this.showDuiHua(this.duihuaArrayConfig[3]);
                this.refreshSchedule();
            },
            男2: () => {
                this.getLayerChild('男', '右手2').active = false;
                this.getLayerChild('男', '男右手').active = true;
                this.getLayerChild('checkNodeList', '男3').active = true;
                this.showWaWa(this.getLayerChild('男', '娃娃4'), targetNode.name);
                this.showDuiHua(this.duihuaArrayConfig[4]);
                this.refreshSchedule();
            },
            男3: () => {
                CccGame.playAnimation2(this.getLayerChild('男', '男身体'), '男背人身体', true);
                this.getLayerChild('checkNodeList', '男4').active = true;
                this.showWaWa(this.getLayerChild('男', '娃娃2'), targetNode.name);
                this.showDuiHua(this.duihuaArrayConfig[5]);
                this.refreshSchedule();
            },
            男4: () => {
                CccGame.playAnimation2(this.getLayerChild('男', '男头'), '男歪头', true);
                this.getLayerChild('checkNodeList', '男0').active = true;
                this.showWaWa(this.getLayerChild('男', '娃娃1'), targetNode.name);
                this.showDuiHua(this.duihuaArrayConfig[6]);
                this.refreshSchedule();
            },
            男0: () => {
                targetNode.active = true;
                targetNode['checkNode'].active = true;
                this.showDuiHua(this.duihuaArrayConfig[7]);
            },
            狗: () => {
                this.getLayerChild('狗1').active = false;
                this.getLayerChild('狗2').active = true;
                this.showWaWa(this.getLayerChild('狗2', '娃娃'), targetNode.name);
                this.showDuiHua(this.duihuaArrayConfig[8]);
                this.refreshSchedule();
            },
            老鼠: () => {
                CccGame.playAnimation2(this.getLayerChild('老鼠'), '老鼠举孩子', true, targetNode.name);
                this.refreshSchedule();
            },
            奶奶1: () => {
                CccGame.playAnimation2(this.getLayerChild('老奶奶', '老奶奶手'), '老奶奶抱小孩手', true);
                this.getLayerChild('checkNodeList', '奶奶2').active = true;
                this.showWaWa(this.getLayerChild('老奶奶', '娃娃2'), targetNode.name);
                this.showDuiHua(this.duihuaArrayConfig[9]);
                this.refreshSchedule();
            },
            奶奶2: () => {
                CccGame.playAnimation2(this.getLayerChild('老奶奶', '老奶奶身体'), '老奶奶背人身体', true);
                this.getLayerChild('checkNodeList', '奶奶0').active = true;
                this.showWaWa(this.getLayerChild('老奶奶', '娃娃1'), targetNode.name);
                this.showDuiHua(this.duihuaArrayConfig[10]);
                this.refreshSchedule();
            },
            奶奶0: () => {
                targetNode.active = true;
                targetNode['checkNode'].active = true;
                this.showDuiHua(this.duihuaArrayConfig[11]);
            },
            蜘蛛网: () => {
                CccGame.playAnimation2(this.getLayerChild('蜘蛛网'), '蜘蛛网吊小孩', true, targetNode.name);
                this.refreshSchedule();
            },
            玩具车: () => {
                CccGame.playAnimation2(this.getLayerChild('木马'), '木马2', true, targetNode.name);
                this.refreshSchedule();
            },
            坏人: () => {
                this.showDuiHua(this.duihuaArrayConfig[12], () => {
                    this.getLayerChild('阳光幼儿园', '大叔').active = false;
                    this.endGameView(0);
                });
            },
            老师: () => {
                this.showWaWa(this.getLayerChild('阳光幼儿园', '老师', '娃娃'), targetNode.name);
                this.scheduleOnce(() => {
                    this.getLayerChild('阳光幼儿园', '老师').active = false;
                    this.refreshSchedule();
                }, 2);
            },
            水管: () => {
                CccGame.playAnimation2(this.getLayerChild('缺口', '水柱'), '水柱', true, targetNode.name);
                this.refreshSchedule();
            },
        }

        console.log("[GameReport]++++++++++++++++++++++>" + targetNode.checkNode.name);
        checkLogic[targetNode.checkNode.name]?.();
    }

    //触摸回调
    normalTouchCallback(targetNode: cc.Node) {
        console.log("+++++++++++++++++++++>" + targetNode.name);

        const itemLogic = {
            左滑门: () => {
                this.scheduleOnce(() => {
                    this.getLayerChild('卫生间', '男子').active = false;
                    this.getLayerChild('男').active = true;
                    this.getLayerChild('checkNodeList', '男1').active = true;
                    this.showDuiHua(this.duihuaArrayConfig[2]);
                }, 1);
            },
            坏人: () => {
                CccGame.playAnimation2(this.getLayerChild('锤子animation2'), '锤子');
                Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/砸');
                this.scheduleOnce(() => {
                    targetNode.active = true;
                    this.getLayerChild('锤子animation2').active = false;
                    CccGame.playAnimation2(this.getLayerChild('阳光幼儿园', '大叔'), '大叔晕', true);
                    this.scheduleOnce(() => {
                        this.getLayerChild('阳光幼儿园', '大叔').active = false;
                        this.getLayerChild('阳光幼儿园', '老师').active = true;
                        this.getLayerChild('checkNodeList', '坏人').active = false;
                        this.getLayerChild('checkNodeList', '老师').active = true;
                        this.showDuiHua(this.duihuaArrayConfig[13]);
                    }, 2);
                }, 0.5);
            },
            裂缝: () => {
                CccGame.playAnimation2(this.getLayerChild('锤子animation1'), '锤子');
                Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/砸');
                this.scheduleOnce(() => {
                    targetNode.active = true;
                    this.getLayerChild('锤子animation1').active = false;
                    this.getLayerChild('缺口').active = true;
                    this.getLayerChild('checkNodeList', '水管').active = true;
                }, 0.5);
            },
        }

        console.log("[GameReport]++++++++++++++++++++++>" + targetNode.name);
        itemLogic[targetNode.name]?.();
        if (targetNode.name == "锤子") {
            itemLogic[targetNode['checkNode'].name]?.();
        }
    }

    //展示娃娃
    showWaWa(wawa: cc.Node, targetName: string) {
        wawa.active = true;
        const url = "texture/娃娃睡觉/" + targetName;
        Common5.getSpriteFrameFromBundle(Common5.selectGameInfo.bundle, url, wawa.getComponent(cc.Sprite));
    }

    //判定
    determine() {
        this.curSchedule++;
        if (this.curSchedule >= 4) {
            this.getLayerChild('chadianNodeList4').active = true;
        }
    }

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

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

    openTouchEvent(node) {
        node.on(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.on(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    closeTouchEvent(node: cc.Node) {
        node.off(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.off(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    touchStartNode(event) {
        let target = event.target;
        console.log('touchStartNode');
    }

    touchMoveNode(event) {

    }

    touchEndNode(event) {
        let target = event.target;
        let logPrefab = this.node.getChildByName('logPrefab');
        logPrefab.getChildByName('duihua').active = false;
    }

    onClick(event) {
        let target = event.target as cc.Node;
        console.log("+++++++++++++++++++++>" + target.name);
        Common5.playEffect("点击音效");
        switch (target.name) {
            case "BtnAddTimes":
            default:
                break;
        }
    }

    //update(dt) { }
}