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";


const { ccclass, property } = cc._decorator;

@ccclass
export default class DaSaoWoShi extends WordGameBaseComponent {
    @property(cc.Node)
    layerNode: 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/DaSaoWoShiConfig', (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 = 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.timeOut, (tab) => {
            this.endGameView(0);
        }, 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/背景音');

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

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

        const targetNameList = ['洗发水', '假发', '一个面膜', '绳子', '洗衣服', '窗帘', '鸡蛋', '剪刀', '地上丝袜'];
        const itemLogic = {
            拖鞋: () => {
                this['shoe'] = true;
                this.unlockLevel(this.answersIndexConfig[0]);
                this.showDuiHua(this.duihuaArrayConfig[1], () => {
                    this.determine();
                });
            },
            围裙: () => {
                this['apron'] = true;
                this.unlockLevel(this.answersIndexConfig[1]);
                this.showDuiHua(this.duihuaArrayConfig[3], () => {
                    this.determine();
                });
            },
            点击被子: () => {
                this.getLayerChild('床', '被子乱').active = false;
                this.getLayerChild('床', '叠好的被子').active = true;
                this.unlockLevel(this.answersIndexConfig[2]);
                this.showDuiHua(this.duihuaArrayConfig[5], () => {
                    this.determine();
                });
            },
            打开盖子: () => {
                this.getLayerChild('洗衣机', '洗衣机门关').active = false;
                this.getLayerChild('洗衣机', '洗衣机门开').active = true;
                this['打开盖子'] = true;
            },
            衣服1: () => {
                if (this['打开盖子']) {
                    this['衣服1'] = true;
                    this.getLayerChild('洗衣机', '衣服1').active = true;
                    if (this['衣服1'] && this['衣服2']) {
                        this.getLayerChild('洗衣机', '衣服1').active = false;
                        this.getLayerChild('洗衣机', '衣服2').active = false;
                        this.getLayerChild('洗衣机', '洗衣机门开').active = false;
                        this.getLayerChild('洗衣机', '洗衣机').active = true;
                        this.unlockLevel(this.answersIndexConfig[3]);
                        this.showDuiHua(this.duihuaArrayConfig[6], () => {
                            this.determine();
                        });
                    }
                } else {
                    targetNode.active = true;
                    if (targetNode['checkNode']) {
                        targetNode['checkNode'].active = true;
                    }
                }
            },
            衣服2: () => {
                if (this['打开盖子']) {
                    this['衣服2'] = true;
                    this.getLayerChild('洗衣机', '衣服2').active = true;
                    if (this['衣服1'] && this['衣服2']) {
                        this.getLayerChild('洗衣机', '衣服1').active = false;
                        this.getLayerChild('洗衣机', '衣服2').active = false;
                        this.getLayerChild('洗衣机', '洗衣机门开').active = false;
                        this.getLayerChild('洗衣机', '洗衣机').active = true;
                        this.unlockLevel(this.answersIndexConfig[3]);
                        this.showDuiHua(this.duihuaArrayConfig[6], () => {
                            this.determine();
                        });
                    }
                } else {
                    targetNode.active = true;
                    if (targetNode['checkNode']) {
                        targetNode['checkNode'].active = true;
                    }
                }
            },
            扫把: () => {
                this.getLayerChild('站立女').active = false;
                this.node.getChildByName('inputBlock').active = true;
                CccGame.playAnimation2(this.getLayerChild('扫地animation'), '扫地', false, null, (spineNode) => {
                    spineNode.active = false;
                    this.node.getChildByName('inputBlock').active = false;
                    this.getLayerChild('站立女').active = true;
                    this.getLayerChild('地上垃圾').active = false;
                    this.unlockLevel(this.answersIndexConfig[4]);
                    this.showDuiHua(this.duihuaArrayConfig[7], () => {
                        this.determine();
                    });
                });
            },
            喷雾机: () => {
                this.node.getChildByName('inputBlock').active = true;
                CccGame.playAnimation2(this.getLayerChild('喷雾animation'), '喷雾', false, null, (spineNode) => {
                    spineNode.active = false;
                    this.node.getChildByName('inputBlock').active = false;
                    this.getLayerChild('泡沫').active = true;
                    this.unlockLevel(this.answersIndexConfig[5]);
                    this['喷雾机'] = true;
                    this.determine();
                });
            },
            拖把: () => {
                if (this['喷雾机']) {
                    this.getLayerChild('站立女').active = false;
                    this.node.getChildByName('inputBlock').active = true;
                    CccGame.playAnimation2(this.getLayerChild('拖地animation'), '拖地', false, null, (spineNode) => {
                        spineNode.active = false;
                        this.node.getChildByName('inputBlock').active = false;
                        this.getLayerChild('站立女').active = true;
                        this.getLayerChild('污渍').active = false;
                        this.getLayerChild('泡沫').active = false;
                        this.unlockLevel(this.answersIndexConfig[6]);
                        this.showDuiHua(this.duihuaArrayConfig[8], () => {
                            this.determine();
                        });
                    });
                } else {
                    this.getLayerChild('站立女').active = false;
                    this.node.getChildByName('inputBlock').active = true;
                    CccGame.playAnimation2(this.getLayerChild('拖地animation'), '拖地', false, null, (spineNode) => {
                        spineNode.active = false;
                        this.node.getChildByName('inputBlock').active = false;
                        this.getLayerChild('站立女').active = true;
                        targetNode.active = true;
                        targetNode['checkNode'].active = true;
                        this.showDuiHua(this.duihuaArrayConfig[9]);
                    });
                }
            },
            下滑抽屉: () => {
                this.getLayerChild('柜子前面').active = true;
            },
            胶带: () => {
                this.node.getChildByName('inputBlock').active = true;
                CccGame.playAnimation2(this.getLayerChild('裂缝'), '胶带', false, null, (spineNode) => {
                    spineNode.active = false;
                    this.getLayerChild('胶带持续').active = false;
                    this.node.getChildByName('inputBlock').active = false;
                    this.unlockLevel(this.answersIndexConfig[7]);
                    this.showDuiHua(this.duihuaArrayConfig[10], () => {
                        this.determine();
                    });
                });
            },
            地上垃圾2: () => {
                this['地上垃圾2'] = true;
                this.showDuiHua(this.duihuaArrayConfig[11]);
                if (this['地上垃圾2'] && this['地上垃圾3']) {
                    this.getLayerChild('垃圾桶', '垃圾桶里面的垃圾').active = true;
                    this.unlockLevel(this.answersIndexConfig[8]);
                    this.scheduleOnce(() => {
                        this.determine();
                    }, 3);
                }
            },
            地上垃圾3: () => {
                this['地上垃圾3'] = true;
                this.showDuiHua(this.duihuaArrayConfig[11]);
                if (this['地上垃圾2'] && this['地上垃圾3']) {
                    this.getLayerChild('垃圾桶', '垃圾桶里面的垃圾').active = true;
                    this.unlockLevel(this.answersIndexConfig[8]);
                    this.scheduleOnce(() => {
                        this.determine();
                    }, 3);
                }
            },
            抹布: () => {
                this.node.getChildByName('inputBlock').active = true;
                CccGame.playAnimation2(this.getLayerChild('抹布animation'), 'caganjing', false, null, (spineNode) => {
                    spineNode.active = false;
                    this.node.getChildByName('inputBlock').active = false;
                    this.getLayerChild('电脑灰尘').active = false;
                    this.getLayerChild('chadianNodeList', '点击电脑主机').active = true;
                    this.unlockLevel(this.answersIndexConfig[9]);
                    this.showDuiHua(this.duihuaArrayConfig[12], () => {
                        this.determine();
                    });
                });
            },
            学习资料: () => {
                this.unlockLevel(this.answersIndexConfig[10]);
                this.showDuiHua(this.duihuaArrayConfig[13], () => {
                    this.determine();
                });
            }
        }

        if (targetNode.name == '拖鞋' || targetNode.name == '围裙') {
            itemLogic[targetNode.name]();
        } else if (!this['shoe']) {
            targetNode.active = true;
            if (targetNode['checkNode']) {
                targetNode['checkNode'].active = true;
            }
            this.showDuiHua(this.duihuaArrayConfig[2]);
        } else if (!this['apron']) {
            targetNode.active = true;
            if (targetNode['checkNode']) {
                targetNode['checkNode'].active = true;
            }
            this.showDuiHua(this.duihuaArrayConfig[4]);
        } else {
            itemLogic[targetNode.name]?.(targetNode);
        }
    }

    determine() {
        this.curSchedule++;
        if (this.curSchedule >= 11) {
            this.showDuiHua(this.duihuaArrayConfig[14], () => {
                this.endGameView(1);
            });
        }

    }

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