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

const { ccclass, property } = cc._decorator;

@ccclass
export default class MaJiaJue extends WordGameBaseComponent {

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

    @property(cc.Node)
    yingziTarget1: cc.Node[] = [];
    @property(cc.Node)
    yingziTarget2: cc.Node[] = [];
    @property(cc.Node)
    yingziTarget3: cc.Node[] = [];

    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule = 0;
    yingziNum = 0;
    chadianNum = 0;

    //文本配置
    titleArray: string[] = [];
    prologueArray: string[] = [];
    renyingConfig: any = null;
    chadianListConfig: any = null;
    yingziTargetArray: any = null;

    //计时
    Timer = 0;

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

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

        Common5.playMusicCustom('majiajue','sound/背景音效');
    }

    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;
        this.yingziTargetArray = [this.yingziTarget1, this.yingziTarget2, this.yingziTarget3];
        Common.subLevel = this.curLayerIndex;
        this.titleArray = this.jsonData.titleArray //MaJiaJueConfig.getInstance().getGameConfig('titleArray') as string[];
        this.prologueArray = this.jsonData.prologueArray //MaJiaJueConfig.getInstance().getGameConfig('prologueArray') as string[];
        this.renyingConfig = this.jsonData.renying //MaJiaJueConfig.getInstance().getGameConfig('renying');
        this.chadianListConfig = this.jsonData.chadianList //MaJiaJueConfig.getInstance().getGameConfig('chadianList');

        Common.Type = 0;
        Common.GameSubTipConfigs = this.jsonData.tipsArray //MaJiaJueConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray //MaJiaJueConfig.getInstance().getGameConfig('answersArray');
        Common.GameSubChooseArray = this.jsonData.chooseArray //MaJiaJueConfig.getInstance().getGameConfig('chooseArray');
    }

    initComponent() {
        for (const node of this.layerNode) {
            node.active = false;
        }
        this.refreshPrologue();
        EventMgr.onEvent_custom(ryw_Event.touchMoveCheck, () => {
            this.gameYingziStep();
        }, this);

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

        EventMgr.onEvent_custom(ryw_Event.chooseResult, (data_) => {
            this.endGameView(data_.result, data_.touchIndex);
        }, this);
    }

    refreshPrologue() {
        let title = this.titleArray[this.curLayerIndex],
            text = this.prologueArray[this.curLayerIndex];
        let lab = this.node.getChildByName("标题").getChildByName("lab"),
            prologue = this.node.getChildByName("序幕").getChildByName("prologue");
        prologue.active = true;
        lab.getComponent(cc.Label).string = title;

        Game.ins.setGameTitle(title)


        
        prologue.getComponent(cc.Label).string = text;
        this.node.getChildByName("tipsBtn").active = false;
        this.curSchedule = 0;
    }

    refreshYingzi() {
        let node = this.layerNode[this.curLayerIndex];
        node.active = true;
        let chadianTouchList = node.getChildByName('chadianTouchList')
        chadianTouchList.active = false;

        if (this.renyingConfig[this.curLayerIndex]) {
            let config = this.renyingConfig[this.curLayerIndex]
            let renyingList = node.getChildByName('renyingList');
            let headNodeList = node.getChildByName('HeadNodeList');
            headNodeList.active = true;
            let scr: HeadNodeList = headNodeList.getComponent('HeadNodeList')
            let targetRenyin = this.yingziTargetArray[this.curLayerIndex]
            for (let i = 0; i < targetRenyin.length; i++) {
                targetRenyin[i].active = false;
            }
            scr.setHeadData(config.HeadIconNodeList, renyingList, this.yingziTargetArray[this.curLayerIndex]);
        }
        this.curSchedule = 1;
    }

    refreshChadian() {
        let node = this.layerNode[this.curLayerIndex];
        node.active = true;
        let headNodeList = node.getChildByName('HeadNodeList');
        let chadianIconList = node.getChildByName('chadianIconList');
        let chadianTouchList = node.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 = 2;
    }

    gameYingziStep() {
        if (this.curSchedule == 1) {
            this.yingziNum++;
            let targetRenyin = this.yingziTargetArray[this.curLayerIndex];
            if (this.yingziNum == targetRenyin.length) {
                this.yingziNum = 0;
                this.refreshChadian();
            }
        }
    }

    gameChaDianStep(touchIndex) {
        let config = this.chadianListConfig[this.curLayerIndex];
        let chadianLog = config.chadianLog;

        this.showQiPao(chadianLog[touchIndex]);
        console.log(this.chadianNum, '  this.chadianNum++=');

        if (chadianLog[touchIndex].effectUrl && chadianLog[touchIndex].effectUrl.length > 0) {
            Common5.playEffectCustom("majiajue", chadianLog[touchIndex].effectUrl);
        }

        if (this.curSchedule == 2) {
            this.chadianNum++;
            if (this.chadianNum == chadianLog.length) {
                this.chadianNum = 0;
                this.curLayerIndex++;
                Common.subLevel = this.curLayerIndex;
                Game.ins.updateTishiBtn()
                if (this.curLayerIndex > 2) {
                    this.curLayerIndex = 2;
                    Common.subLevel = this.curLayerIndex;
                    this.scheduleOnce(() => {
                        this.chooseGameView();
                    }, 2);
                }
                else {
                    for (let i = 0; i < this.layerNode.length; i++) {
                        if (this.layerNode[i].active) {
                            cc.tween(this.layerNode[i])
                                .delay(3.0)
                                .to(1, { opacity: 0 })
                                .call(() => {
                                    let logPrefab = this.node.getChildByName('logPrefab');
                                    logPrefab.getChildByName('duihua').active = false;
                                    this.layerNode[i].active = false
                                    this.refreshPrologue();
                                })
                                .start();
                        } else {
                            this.layerNode[i].active = false;
                        }
                    };
                }
            }
        }
    }

    chooseGameView() {
        let ChooseLayer = this.node.getChildByName('ChooseLayer')
        ChooseLayer.active = true

        let script_: ChooseBtnLayer = ChooseLayer.getComponent('ChooseBtnLayer')
        script_.setChooseBtnView()
    }


    endGameView(result, touchIndex) {
        let endLayer = this.node.getChildByName('endLayer')
        endLayer.active = true

        let resultlayer = endLayer.getChildByName(result)
        resultlayer.active = true
        if (result == 'result1') {
            Common5.playEffect('majiajue/成功播音')
            this.scheduleOnce(() => {
                 Game.ins.showSuccess();;
            }, 3.0);
        } else {
            //Common5.playEffect('majiajue/失败插播')
            this.scheduleOnce(() => {
                Game.ins.showFail();;
            }, 3.0);
        }
        console.log(touchIndex, 'touchIndex++++++++')
    }

    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;
        if (this.curSchedule == 0) {
            this.node.getChildByName("序幕").getChildByName("prologue").active = false;
            this.node.getChildByName("tipsBtn").active = true;



            if (this.curLayerIndex == 2) {
                this.refreshChadian();
            } else {
                this.refreshYingzi();
            }
        }
    }

    showQiPao(curLog) {
        console.log("curLog==", curLog)
        if (!curLog) {
            console.log("xxxxxx")
            return
        }
        let string_ = curLog.str
        let qiPaoPos_ = curLog.qiPaoPos

        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_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(2)
                .call(() => {
                    qiPao.active = false
                })
                .start();
        }
        else {
            let logPrefab = this.node.getChildByName('logPrefab')
            let script_: logPrefabScript = logPrefab.getComponent('logPrefabScript')
            script_.setDailogShow(string_)
        }
    }

    //update(dt) { }
}