import { LevelMgr } from "../../levelMgr";
import { Utils } from "../common/utils";
import { GameSysLogic } from "../update/gameSysLogic";

enum ENestConditionState {
    none,
    start,
    done,
}

export class NestCondition extends GameSysLogic {
    time: number = 0;
    num: number;
    call: Function = null;
    state: ENestConditionState = ENestConditionState.none;

    name: string;
    _str:string;

    fill(str: string = '[4;5]'){
        str = Utils.strReplaceSeparator(str);
        let obj = JSON.parse(str);
        // console.log(obj);
        this.time = obj[0];
        this.num = obj[1];

        this._str = str;

        console.log(this.name,`[${this._str}]`,'create');
    }

    start(){
        this.state = ENestConditionState.start;
        console.log(this.name,`[${this._str}]`,'start');
    }

    isDone(){
        return this.state == ENestConditionState.done;
    }

    gameUpdate(dt){
        if(this.state != ENestConditionState.start) return;
        this.time -= dt;
        if(this.time <= 0 && LevelMgr.inst.getMonsterCnt() <= this.num) {
            console.log(this.name,`[${this._str}]`,'done');
            this.state = ENestConditionState.done;
            this.call && this.call();
        }
    }
}