import { SmallLevelItem } from "../cfg/smallLevelItem";
import { GameSysLogic } from "../update/gameSysLogic";
import { NestCondition } from "./nestCondition";

export class NestConditionMgr {
    conditionArr: NestCondition[] = [];
    cnt = 0;
    call: Function;

    init(levelData: SmallLevelItem){
        for (let index = 2; index <= 6; index++) {
            let nestKey = `nest${index}`;
            let intervalKey = `interval${index}`;
            if(levelData.data[nestKey] == '' || levelData.data[intervalKey] == '') break;
            
            let condition = new NestCondition();
            condition.name = `interval-${index}`;
            condition.fill(levelData.data[intervalKey]);

            this.conditionArr.push(condition);
        }
    }

    start(){
        this.cnt = 0;
        this.do();
    }

    destroy(){
        this.conditionArr.forEach(v=>{v.end()})
        this.conditionArr = [];
    }

    do(){
        if(this.cnt >= this.conditionArr.length) return;
        
        let condition = this.conditionArr[this.cnt];
        condition.call = ()=>{
            this.call && this.call(this.cnt);
            this.cnt ++;
            this.do();
        }
        condition.start();
    }

    isDone(){
        return this.cnt >= this.conditionArr.length;
    }

}