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

const { ccclass, property } = cc._decorator;

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

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

    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule = 0;
    chadianNum = 0;
    chadianIndex = 0;
    chooseIndex = -1;

    //文本配置
    titleArrayConfig: string[] = [];
    conclusionArrayConfig: any[] = [];
    topDialogConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chooseArrayConfig: any = null;
    chadianListConfig: any = null;

    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        //this.openTouchEvent(this.node.getChildByName('bg'));
        Common5.stopMusic();
        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle, 'script/ShengSiJueZeConfig', (assest) => {
            this.jsonData = assest.json
            this.initParameters();
            this.initComponent();
        })
    }


    //初始化参数
    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;
        this.chadianNum = 0;
        this.chadianIndex = 0;
        this.chooseIndex = -1;
        this.titleArrayConfig = this.jsonData.titleArray;
        this.conclusionArrayConfig = this.jsonData.conclusionArray;
        this.duihuaArrayConfig = this.jsonData.duihuaArray;
        this.chooseArrayConfig = this.jsonData.chooseArray;
        this.chadianListConfig = this.jsonData.chadianList;
        this.topDialogConfig = this.jsonData.topDialog;

        Common.Type = 0;
        Common.subLevel = this.curLayerIndex;
        Common.GameSubTipConfigs = this.jsonData.tipsArray;
        Common.GameSubAnswerConfigs = this.jsonData.answersArray;

        EventMgr.onEvent_custom(ryw_Event.touchChaDianCheck, (data_) => {
            this.gameChaDianStep(data_.touchIndex);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.chooseResult, (data_) => {
            this.chooseResultCallback(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);

    }

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

        this.refreshLayer();
        //Common5.playMusicCustom(Common5.selectGameInfo.bundle, 'sound/关卡背景,需要在关卡里循环播放');
    }

    //刷新场景
    refreshLayer() {
        let node = this.layerNode[this.curLayerIndex];
        node.active = true;
        Game.ins.setGameTitle(this.titleArrayConfig[this.curLayerIndex]);
        // let topDialog = node.getChildByName('view').getChildByName('顶部对话框');
        // topDialog.getChildByName('str').getComponent(cc.Label).string = this.topDialogConfig[this.curLayerIndex];

        if (this.curLayerIndex == 0) {
            //Common5.playMusicCustom(Common5.selectGameInfo.bundle, 'sound/场景一背景音');
            Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/进入场景一最开始播一次');
            this.showDuiHua(this.duihuaArrayConfig[0]);
            this.refreshChadian();
        } else if (this.curLayerIndex == 1) {
            //Common5.playMusicCustom(Common5.selectGameInfo.bundle, 'sound/场景二背景音');
            this.node.getChildByName('inputBlock').active = true;
            CccGame.playAnimation2(node.getChildByName("view").getChildByName("眨眼"), '眨眼');
            this.scheduleOnce(() => {
                this.node.getChildByName('inputBlock').active = false;
                node.getChildByName("view").getChildByName("眨眼").active = false;
                this.showDuiHua(this.duihuaArrayConfig[1]);
                this.refreshChadian();
            }, 3);
        }
    }


    //刷新茬点
    refreshChadian() {
        let node = this.layerNode[this.curLayerIndex];
        let headNodeList = node.getChildByName('HeadNodeList');
        let chadianIconList = node.getChildByName('chadianIconList');
        let chadianTouchList = node.getChildByName('view').getChildByName('chadianTouchList');
        headNodeList.active = false;
        chadianIconList.active = true;
        chadianTouchList.active = true;

        let config = this.chadianListConfig[this.curLayerIndex];
        if (config) {
            let chadianIconList = node.getChildByName('chadianIconList');
            let scr: ChaDianListScript = chadianIconList.getComponent('ChaDianListScript');
            scr.setChaDianData(config.chadianIconList);
        }

        this.curSchedule = 1;
    }

    //刷新结语
    refreshOperate() {
        let node = this.layerNode[this.curLayerIndex];
        let chadianIconList = node.getChildByName('chadianIconList');
        let chadianTouchList = node.getChildByName('view').getChildByName('chadianTouchList');
        chadianIconList.active = false;
        chadianTouchList.active = false;

        switch (this.curLayerIndex) {
            case 0:
                let blackCurtain = node.getChildByName("view").getChildByName("blackCurtain");
                blackCurtain.getChildByName("bar").active = false;
                cc.tween(blackCurtain)
                    .set({ active: true, opacity: 0 })
                    .to(1, { opacity: 255 })
                    .call(() => {
                        blackCurtain.getChildByName("全图").active = true;
                    })
                    .delay(3)
                    .call(() => {
                        this.nextLayer();
                    })
                    .start();
                break;
            case 1:
                this.showDuiHua(this.duihuaArrayConfig[2], () => {
                    CccGame.playAnimation2(node.getChildByName("view").getChildByName("电视"), '电视');
                    this.scheduleOnce(() => {
                        this.showDuiHua(this.duihuaArrayConfig[3], () => {
                            this.showDuiHua(this.duihuaArrayConfig[4], () => {
                                this.showChooseLayer(0);
                                let second = 10;
                                this['onTimer'] = true;
                                CccGame.playAnimation2(node.getChildByName("view").getChildByName("机关"), '机关');
                                node.getChildByName("view").getChildByName("机关").getComponent(sp.Skeleton).timeScale = 0.5;
                                let progressBar = this.chooseLayer.getChildByName("ProgressBar").getComponent(cc.ProgressBar);
                                progressBar.progress = second / 10;
                                this.schedule(() => {
                                    second--;
                                    progressBar.progress = second / 10;
                                    if (this['onTimer'] && progressBar.progress == 0) {
                                        this['onTimer'] = false;
                                        this.chooseLayer.active = false;
                                        node.getChildByName("view").getChildByName("机关").getComponent(sp.Skeleton).timeScale = 10;
                                        this.endGameView(0);
                                    }
                                }, 1, 10, 1);
                            });
                        });
                    }, 0.5);
                });
                break;
            default:
                break;
        }
    }

    //茬点步骤
    gameChaDianStep(touchIndex) {
        let config = this.chadianListConfig[this.curLayerIndex];
        let chadianLog = config.chadianLog;

        console.log(this.chadianNum, '  this.chadianNum++=');
        this.showDuiHua(chadianLog[touchIndex], () => {
            if (this.curSchedule == 1) {
                this.chadianNum++;
                if (this.chadianNum == chadianLog.length) {
                    this.chadianNum = 0;
                    this.refreshOperate();
                }
            }
        });
    }

    //选择回调
    chooseResultCallback(touchIndex: number) {
        this.node.getChildByName("inputBlock").active = true;
        let node = this.layerNode[this.curLayerIndex];
        if (this.chooseIndex == 0) {
            this['onTimer'] = false;
            CccGame.playAnimation2(node.getChildByName("view").getChildByName("女"), '女3剪刀', true);
            node.getChildByName("view").getChildByName("机关").getComponent(sp.Skeleton).timeScale = 0;
            if (touchIndex == 0) {
                CccGame.playAnimation2(node.getChildByName("view").getChildByName("男"), '男人剪刀', true);
                node.getChildByName("view").getChildByName("机关").active = false;
                this.showDuiHua(this.duihuaArrayConfig[7], () => {
                    this.endGameView(1);
                });
            } else if (touchIndex == 1) {
                CccGame.playAnimation2(node.getChildByName("view").getChildByName("男"), '男人布', true);
                this.showDuiHua(this.duihuaArrayConfig[5], () => {
                    node.getChildByName("view").getChildByName("机关").getComponent(sp.Skeleton).timeScale = 10;
                    this.endGameView(0);
                });
            } else {
                CccGame.playAnimation2(node.getChildByName("view").getChildByName("男"), '男人石头', true);
                this.showDuiHua(this.duihuaArrayConfig[6], () => {
                    node.getChildByName("view").getChildByName("机关").getComponent(sp.Skeleton).timeScale = 10;
                    this.endGameView(0);
                });
            }
        }
    }

    //触摸回调
    normalTouchCallback(targetNode: cc.Node) {
        console.log("+++++++++++++++++++++>" + targetNode.name);
        let node = this.layerNode[this.curLayerIndex];
        switch (targetNode.name) {
            case '刀':
                break;
            default:
                break;
        }
    }

    //显示选项
    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].getChildByName('view');
        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_);
    }

    //跳转场景
    nextLayer() {
        this.curLayerIndex++;
        Common.subLevel = this.curLayerIndex;
        Game.ins.updateTishiBtn();
        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 })
                    .call(() => {
                        this.layerNode[i].active = false;
                        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)
    }

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

}