import { GameSysLogic } from "./gameSysLogic";



export class GameLogicMgr {
    private static _inst: GameLogicMgr;
    public static get inst(): GameLogicMgr {
        if (this._inst == null) {
            this._inst = new GameLogicMgr();
            this._inst.init();
        }
        return this._inst;
    }

    isGamePause: boolean = false;

    sysLogics: Map<number, GameSysLogic> = new Map();

    switch(){
        this.isGamePause = !this.isGamePause;
        //@ts-ignore
        dragonBones.timeScale = this.isGamePause ? 0 : 1;
    }

    init(){
        //@ts-ignore
        let aniMgr = cc.director.getAnimationManager();
        let actionMgr = cc.director.getActionManager();
        
        if(!CC_EDITOR){
            aniMgr._oldUpdate = aniMgr.update;
            aniMgr.update = function(dt){
                if(GameLogicMgr.inst.isGamePause) return;
                aniMgr._oldUpdate(dt*0.5);
            }

            // @ts-ignore
            actionMgr._oldUpdate = actionMgr.update;
            actionMgr.update = function(dt){
                if(GameLogicMgr.inst.isGamePause) return;
                // @ts-ignore
                actionMgr._oldUpdate(dt);
            }
        }

        cc.director.getScheduler().enableForTarget(this);
        cc.director.getScheduler().schedule(this.update, this, 0, cc.macro.REPEAT_FOREVER, 0, false);
        cc.director.getScheduler().schedule(this.updateSec, this, 1, cc.macro.REPEAT_FOREVER, 0, false);
    }

    update(dt){
        if (this.isGamePause) return;
        this.sysLogics.forEach((v)=>{
            // v['update'](dt);
            v.gameUpdate(dt);
        });
    }

    updateSec(dt) {
        if (this.isGamePause) return;
        this.sysLogics.forEach((v)=>{
            // v['updateSec'](dt);
            v.gameUpdateSec(dt);
        });
    }

    end(){
        cc.director.getScheduler().unschedule(this.update,this);
        cc.director.getScheduler().unschedule(this.updateSec,this);
    }

    addSysLogic(logic: GameSysLogic){
        this.sysLogics.set(logic.logicId, logic);
    }

    removeSysLogic(logic: GameSysLogic){
        this.sysLogics.delete(logic.logicId);
    }
};

