import { LQCollide } from "./Collide/lq_collide_system/lq_collide";
import { ConfigMgr } from "./game/cfg/configMgr";
import { Nest } from "./game/monster/nest";
import { ResMgr } from "./resMgr";


let pass = 5;


enum LevelState {
    none,
    loading,
    playing,
}

/**
 * 1 切换关卡
 */
export class LevelMgr {

    parent: cc.Node;
    title: cc.Label;
    passCnt: number = 0;
    state = LevelState.none;

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

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

    update(dt){
        if(this.state != LevelState.playing) return;
        if(this.parent.getComponentsInChildren(LQCollide).length > 0) return;
        this.next();

    }

    end(){
        cc.director.getScheduler().unschedule(this.update,this);
    }
    
    async begin(){
        this.passCnt = 0;
        this.load();

        // let level = 1;

        // let node = await ResMgr.inst.loadPrefab(`level/levelEmpty`, this.parent);
        // let levelView = node.getComponent(levelempty);

        // let levelData = ConfigMgr.inst.getCfgById('level', level);

        
        // let nestData = ConfigMgr.inst.getCfgById('nest', levelData.nest);
        // let nestCreator = new Nest(nestData, levelView.$top_node);
        // nestCreator.begin();
    }

    next(){
        this.passCnt ++;
        if(this.passCnt >= pass) return;

        this.load();
    }
    
    private load() {
        this.state = LevelState.loading
        this.title.string = `level${this.passCnt + 1}`;
        this.parent.destroyAllChildren();
        ResMgr.inst.loadPrefab(`level/level${this.passCnt + 1}`, this.parent).then(()=>{
            this.state = LevelState.playing;
        });
    }
}