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

// import ShengRiJingXiaConfig from "./ShengRiJingXiaConfig";
const { ccclass, property } = cc._decorator;

@ccclass
export default class ShengRiJingXiaScript extends WordGameBaseComponent {

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

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

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

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


    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule: number = 0;
    completedBehavior: Set<string> = null;

    //文本配置
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any = [];
    unitListConfig: any[] = [];


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

            this.jsonData =assest.json

            this.initParameters();
            this.initComponent();
        } )

    }

    initParameters() {
        this.curSchedule = 0;
        this.completedBehavior = new Set();
        Common.Type = 0;
        Common.subLevel = this.curLayerIndex;
        this.titleArrayConfig = this.jsonData.titleArray//ShengRiJingXiaConfig.getInstance().getGameConfig('titleArray') as string[];
        this.duihuaArrayConfig = this.jsonData.duihuaArray//ShengRiJingXiaConfig.getInstance().getGameConfig('duihuaArray') as any[];
        this.unitListConfig = this.jsonData.unitList//ShengRiJingXiaConfig.getInstance().getGameConfig('unitList') as any[];
        Common.GameSubTipConfigs = this.jsonData.tipsArray//ShengRiJingXiaConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray//ShengRiJingXiaConfig.getInstance().getGameConfig('answersArray');
    }

    initComponent() {
        for (const node of this.layerNode) {
            node.active = false;
        }

        this.refreshLayer();

        EventMgr.onEvent_custom(ryw_Event.itemIconTouchTrue, (data_) => {
            this.itemIconTouchCallBack(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
            this.normalTouchEndCheckCallBack(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
            this.endGameView(0);
        }, this);
    }


    refreshLayer() {
        let node = this.layerNode[this.curLayerIndex];
        node.active = true;

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

        
        Game.ins.setGameTitle(title)


        this.itemIconList.active = true;
        let scr: ItemIconList = this.itemIconList.getComponent('ItemIconList');
        scr.setIconListData(this.unitListConfig[0].unitIconList, this.checkNodeList.children, this.unitNodeList);
    }

    itemIconTouchCallBack(targetNode: any) {
        this.completedBehavior.add(targetNode.checkNode.name);
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.checkNode.name);
        this.showQiPao(this.unitListConfig[0].unitLog[targetNode.iconIndex]);

        // if (this.completedBehavior.size == this.unitListConfig[0].unitLog.length - 1) {
        //     this.scheduleOnce(() => {
        //         let curLayerNode = this.layerNode[this.curLayerIndex];
        //         curLayerNode.getChildByName("背景").getChildByName("门关上").active = false;
        //         curLayerNode.getChildByName("背景").getChildByName("门开").active = true;
        //         curLayerNode.getChildByName("role").getChildByName("女2").active = true;
        //         curLayerNode.getChildByName("checkNodeList").getChildByName("清洁服1").active = true;
        //     }, 3);
        // } else 
        if (this.completedBehavior.size == this.unitListConfig[0].unitLog.length) {
            this.scheduleOnce(() => {
                this.endGameView(1);
            }, 3);
        }
    }

    normalTouchEndCheckCallBack(targetNode: any) {
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        switch (targetNode.name) {
            case "kaiMenTouchArea":
                Common5.playEffectCustom("shengrijingxia", 'sound/开门');
                break;

            default:
                break;
        }
    }

    showQiPao(curLog, func?) {
        console.log("curLog==", curLog);

        if (curLog.effectUrl) {
            Common5.playEffectCustom("shengrijingxia", curLog.effectUrl);
        }

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

        if (qiPaoPos_ != -1) {
            let qiPao = this.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(3)
                .call(() => {
                    qiPao.active = false
                })
                .start();
        }
        else {
            let logPrefab = this.node.getChildByName('logPrefab')
            let script_: logPrefabScript = logPrefab.getComponent('logPrefabScript')
            script_.setDailogShow(string_)
        }

        this.scheduleOnce(() => {
            if (func) {
                func();
            }
        }, delayTime_);
    }

    endGameView(touchIndex) {
        Game.ins.stopTime();
        this.node.getChildByName("mask").active = true;
        this.scheduleOnce(() => {

            // this.node.getChildByName("mask").active = true;
            if (touchIndex == 0) {
                Game.ins.showFail();;
            } else {
                 Game.ins.showSuccess();;
            }
        }, 3)
    }

    nextLayer() {
        this.curLayerIndex++;
        Common.subLevel = this.curLayerIndex;
        Game.ins.updateTishiBtn()
        if (this.curLayerIndex > 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(() => {
                            this.layerNode[i].active = false;
                            this.refreshLayer();
                        })
                        .start();
                } else {
                    this.layerNode[i].active = false;
                }
            };
        }
    }

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


}