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 WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
import DaDianScript from "../../../../FrameWork/Base/DaDianScript";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
import GZTipBtnScript from "../../../WordGame/guizeGame/comScript/GZTipBtnScript";
import GuiZeLayer from "../../../WordGame/guizeGame/comScript/GuiZeLayer";
import FenBianSiBaoTaiConfig from "./FenBianSiBaoTaiConfig";
import CccGame from "../../../WordGame/gameComScript/CccGame/CccGame";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FenBianSiBaoTaiScript extends WordGameBaseComponent {

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

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

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

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

    //当前层级
    curLayerIndex = 0;
    //当前进度
    isReadGuize = 0;
    chooseArrayName: string = "";
    stepIndex: number = 0;
    chadianIndex = 0;

    //文件配置
    titleArray: string[] = null;
    duihuaArray: [] = null;
    resultDuihuaArray: [] = null;
    sortLayoutContent: cc.Node[] = null;

    bundle = null
    start() {
        this.bundle = Common5.gameConfig.zmGameConfig[Common5.selectGameNum].bundle
        super.start();
        DaDianScript.userEnterDaDian();
        this.initParameters();
        this.initComponent();
        Common5.playMusicCustom(this.bundle,'sound/背景音乐');
    }

    //初始化参数
    initParameters() {
        this.isReadGuize = 0;
        this.titleArray = FenBianSiBaoTaiConfig.getInstance().getGameConfig('titleArray');
        this.duihuaArray = FenBianSiBaoTaiConfig.getInstance().getGameConfig('duihuaArray');
        this.resultDuihuaArray = FenBianSiBaoTaiConfig.getInstance().getGameConfig('resultDuihuaArray');

        Common.Type = 0
        Common.subLevel = this.curLayerIndex
        Common.GameSubTipConfigs = FenBianSiBaoTaiConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubChooseArray = FenBianSiBaoTaiConfig.getInstance().getGameConfig('chooseArray');
        Common.GameguizeArray = FenBianSiBaoTaiConfig.getInstance().getGameConfig('GameguizeArray');
        Common.GameSubAnswerConfigs = FenBianSiBaoTaiConfig.getInstance().getGameConfig('answersArray');
        EventMgr.onEvent_custom(ryw_Event.openGuizeLayerEvent, (data) => {
            this.guizeBtnCallback();
        }, this);

        EventMgr.onEvent_custom(ryw_Event.GuiZeGameBtnClick, (data) => {
            this.guizePaperCallback(data.touchIndex);
        }, this);

        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.layerNodeArray) {
            node.active = false;
        }

        this.sortLayoutContent = [];
        let sortLayout = this.layerNodeArray[this.curLayerIndex].getChildByName("sortLayout");
        for (let index = 0; index < sortLayout.children.length; index++) {
            this.sortLayoutContent[index] = sortLayout.children[index];
        }

        this.refreshLayer();
        this.scheduleOnce(this.setTimeNodePos, 0);
    }

    //设置计时位置
    setTimeNodePos() {
        // let timeNode = this.node.getChildByName("timeNode");
        // let addtimeNode = this.node.getChildByName("addtimeNode");
        // let timeNodePos = timeNode.getPosition();
        // let addtimeNodePos = addtimeNode.getPosition();
        // Game.ins.setTimePos(timeNodePos.x, timeNodePos.y);
        // Game.ins.setTimeScale(0.7)
        // Game.ins.setTimeBtnPos(addtimeNodePos.x, addtimeNodePos.y);
    }

    //更新层级
    refreshLayer() {
        this.layerNodeArray[this.curLayerIndex].active = true;

        let title = this.titleArray[this.curLayerIndex];
        let titleLab = this.node.getChildByName('标题').getChildByName('lab');
        titleLab.getComponent(cc.Label).string = title;

        this.isReadGuize = 0;
        this.refreshTipBtn();
        this.onNodeMoveEvent();
    }

    //更新规则
    refreshTipBtn() {
        Common.subLevel = this.curLayerIndex * 2 + this.isReadGuize;
        // let tipScr: GZTipBtnScript = this.GZTipBtn.getComponent('GZTipBtnScript');
        // tipScr.refreshTipsCurView();
        Game.ins.updateTishiBtn()
    }

    //注册节点移动事件
    onNodeMoveEvent() {
        let node = this.layerNodeArray[this.curLayerIndex];
        let sortLayout = node.getChildByName("sortLayout");
        let moveNode = node.getChildByName("moveNode");
        for (let index = 0; index < sortLayout.children.length; index++) {
            const element = sortLayout.children[index].getChildByName('item');
            let checkNodeArray = [];
            for (const item of sortLayout.children) {
                if (item.name != element.name) {
                    checkNodeArray.push(item);
                }
            }

            sortLayout.children[index]['touchIndex'] = index;
            let callbacks = CccGame.onNodeUnidirMoveCheckEvent(element, checkNodeArray, { touchIndex: index }, 0, moveNode);
            callbacks.setSuccessListener((data_) => {
                data_.targetNode.active = true;
                data_.targetNode.checkNode.active = true;
                let targetIndex = sortLayout.children.indexOf(data_.targetNode.recoveparent),
                    checkIndex = sortLayout.children.indexOf(data_.targetNode.checkNode);
                let element = sortLayout.children[checkIndex];
                sortLayout.children[checkIndex] = sortLayout.children[targetIndex];
                sortLayout.children[targetIndex] = element;
            });
        }

        this.node.getChildByName('button').active = false;
    }

    //规则按键回调
    guizeBtnCallback() {
        this.GuiZeLayer.active = true;
        let script_: GuiZeLayer = this.GuiZeLayer.getComponent('GuiZeLayer');
        script_.refreshView();
    }

    //规则纸回调
    guizePaperCallback(touchIndex: number) {
        // if (this.isReadGuize == 1) return;
        // this.isReadGuize = 1;
        this.node.getChildByName('button').active = true;

        this.refreshTipBtn();

        this.GuiZeLayer.active = true;
        let script_: GuiZeLayer = this.GuiZeLayer.getComponent('GuiZeLayer')
        script_.setGuizeArray(this.curLayerIndex);
    }

    //触摸移动回调
    normalTouchCallback(targetNode: cc.Node) {
        console.log("+++++++++++++++++++++>" + targetNode.name);
        switch (targetNode.name) {
            case "xianyifu":
                let node1 = targetNode.parent;
                CccGame.playAnimation(node1, '一号 老二掀衣服', true);
                this.scheduleOnce(() => {
                    if (this['tiaowu']) {
                        CccGame.playAnimation(node1, '一号 老二跳舞', true);
                    } else {
                        CccGame.playAnimation(node1, '一号 老二待机', true);
                    }
                    targetNode.active = true;
                }, 2);
                break;
            case "xianyifu1":
                let node2 = targetNode.parent;
                CccGame.playAnimation(node2, '一号 老大老三老四掀衣服', true);
                this.scheduleOnce(() => {
                    if (this['tiaowu'] && node2.name == '四胞胎3') {
                        CccGame.playAnimation(node2, '一号 老三跳舞', true);
                    } else {
                        CccGame.playAnimation(node2, '一号 老大老三老四待机', true);
                    }
                    targetNode.active = true;
                }, 2);
                break;
            case "收音机":
                let attrNodeArray1 = targetNode['attrNodeArray'];
                CccGame.playAnimation(attrNodeArray1[0], '一号 老二跳舞', true);
                CccGame.playAnimation(attrNodeArray1[1], '一号 老三跳舞', true);
                Common5.playMusicCustom(this.bundle,'sound/收音机播放的音乐');
                this['tiaowu'] = true;
                break;
            case "shouyinji":
                let attrNodeArray2 = targetNode['attrNodeArray'];
                CccGame.playAnimation(attrNodeArray2[0], '一号 老二待机', true);
                CccGame.playAnimation(attrNodeArray2[1], '一号 老大老三老四待机', true);
                Common5.playMusicCustom(this.bundle,'sound/背景音乐');
                this['tiaowu'] = false;
                break;
            case "mojing1":
                let mojing1Node = targetNode.parent;
                CccGame.playAnimation(mojing1Node, '二号 老三上滑墨镜', false);
                this.scheduleOnce(() => {
                    CccGame.playAnimation(mojing1Node, '二号 老三待机', true);
                    targetNode.active = true;
                }, 1.5);
                break;
            case "mojing2":
                let mojing2Node = targetNode.parent;
                CccGame.playAnimation(mojing2Node, '二号 老四上滑墨镜', false);
                this.scheduleOnce(() => {
                    CccGame.playAnimation(mojing2Node, '二号 老大老四待机', true);
                    targetNode.active = true;
                }, 1.5);
                break;
            case "mojing3":
                let mojing3Node = targetNode.parent;
                CccGame.playAnimation(mojing3Node, '二号 老大上滑墨镜', false);
                this.scheduleOnce(() => {
                    CccGame.playAnimation(mojing3Node, '二号 老大老四待机', true);
                    targetNode.active = true;
                }, 1.5);
                break;
            case "mojing4":
                let mojing4Node = targetNode.parent;
                CccGame.playAnimation(mojing4Node, '二号 老二上滑墨镜', false);
                this.scheduleOnce(() => {
                    CccGame.playAnimation(mojing4Node, '二号 老二待机', true);
                    targetNode.active = true;
                }, 1.5);
                break;
            case "gouwu":
                let gouwuNode = targetNode.parent;
                CccGame.playAnimation(gouwuNode, '二号 老三购物卡', false);
                this.scheduleOnce(() => {
                    CccGame.playAnimation(gouwuNode, '二号 老三待机', true);
                    targetNode.active = true;
                }, 1.5);
                break;
            case "手电筒":
                let checkNode = targetNode['checkNode'] as cc.Node,
                    role = checkNode.parent;
                switch (checkNode.name) {
                    case '手电筒光3':
                        cc.tween(checkNode)
                            .set({ active: true, opacity: 255 })
                            .call(() => {
                                CccGame.playAnimation(role, '三号 老大闭眼', true);
                                this['biyan'] = true;
                            })
                            .delay(2)
                            .set({ active: true, opacity: 0 })
                            .call(() => {
                                this['biyan'] = false;
                                if (this['tiaowu']) {
                                    CccGame.playAnimation(role, '三号 老三跳舞', true);
                                } else {
                                    CccGame.playAnimation(role, '三号 老三待机', true);
                                }
                                targetNode.active = true;
                            })
                            .start();
                        break;
                    case '手电筒光2':
                        cc.tween(checkNode)
                            .set({ active: true, opacity: 255 })
                            .call(() => {
                                CccGame.playAnimation(role, '三号 老二闭眼', true);
                            })
                            .delay(2)
                            .set({ active: true, opacity: 0 })
                            .call(() => {
                                CccGame.playAnimation(role, '三号 老二待机', true);
                                targetNode.active = true;
                            })
                            .start();
                        break;
                    case '手电筒光1':
                        cc.tween(checkNode)
                            .set({ active: true, opacity: 255 })
                            .delay(2)
                            .set({ active: true, opacity: 0 })
                            .call(() => {
                                targetNode.active = true;
                            })
                            .start();
                        break;
                    case '手电筒光4':
                        cc.tween(checkNode)
                            .set({ active: true, opacity: 255 })
                            .call(() => {
                                CccGame.playAnimation(role, '三号 老四闭眼', true);
                            })
                            .delay(2)
                            .set({ active: true, opacity: 0 })
                            .call(() => {
                                CccGame.playAnimation(role, '三号 老四待机', true);
                                targetNode.active = true;
                            })
                            .start();
                        break;
                    default:
                        break;
                }
                break;
            case "电台无信号":
                Common5.playEffectCustom(this.bundle, 'sound/收音机没信号声音');
                this.scheduleOnce(() => {
                    targetNode.active = true;
                }, 2);
                break;
            case "遥控器":
                this['tiaowu'] = true;
                let node3 = targetNode['attrNodeArray'][1];
                CccGame.playAnimation(node3, '三号 老三跳舞', true);
                Common5.playMusicCustom(this.bundle,'sound/收音机播放的音乐');
                this.scheduleOnce(() => {
                    if (this['biyan']) {
                        CccGame.playAnimation(node3, '三号 老大闭眼', true);
                    } else {
                        CccGame.playAnimation(node3, '三号 老三待机', true);
                    }
                    targetNode.active = true;
                    targetNode['checkNode'].active = true;
                    targetNode['attrNodeArray'][0].active = false;
                    Common5.playMusicCustom(this.bundle,'sound/背景音乐');
                    this['tiaowu'] = false;
                }, 5.7);
                break;
            default:
                break;
        }
    }

    determine() {
        const sortConfig = ['1023', '2301', '2103'];
        let sortLayout = this.layerNodeArray[this.curLayerIndex].getChildByName("sortLayout");
        let sortText = '';
        for (const iterator of sortLayout.children) {
            sortText += iterator['touchIndex'];
        }

        if (sortText != sortConfig[this.curLayerIndex]) {
            this.endGameView(0);
            return;
        }

        if (this.curLayerIndex < 2) {
            this.nextLayer();
        } else {
            this.endGameView(1);
        }

    }

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

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

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

        let node = this.layerNodeArray[this.curLayerIndex];
        if (qiPaoPos_ != -1) {
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions()
            qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(delayTime_)
                .call(() => {
                    qiPao.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_);
    }

    //跳转场景
    nextLayer() {
        this.curLayerIndex++;
        Common.subLevel = this.curLayerIndex
        Game.ins.updateTishiBtn()
        Common5.playMusicCustom(this.bundle,'sound/背景音乐');
        this['tiaowu'] = false;
        for (let i = 0; i < this.layerNodeArray.length; i++) {
            if (this.layerNodeArray[i].active) {
                cc.tween(this.layerNodeArray[i])
                    .to(1, { opacity: 0 })
                    .delay(1.0)
                    .set({ active: false })
                    .call(this.refreshLayer, this)
                    .start();
            } else {
                this.layerNodeArray[i].active = false;
            }
        };
    }

    endGameView(touchIndex: number) {
        this.node.getChildByName("mask").active = true;
        Game.ins.stopTime();
        if (touchIndex == 0) {
            this.scheduleOnce(() => {
                Game.ins.showFail();
            }, 1.5);
        } else {
            this.scheduleOnce(() => {
                Game.ins.showSuccess();
            }, 1.5);
        }
    }

    onClick(event) {
        let target = event.target;
        console.log("+++++++++++++++++++>" + target.name);
        Common5.playEffect("点击音效");
        switch (target.name) {
            case "button":
                target.active = false;
                this.determine();
                break;
            default:
                break;
        }
    }
}