import { EEnemySize } from "../../data/EEnemySize";
import { SimCommandStartNextWave } from "../commands/SimCommandStartNextWave";
import { SimEnemy } from "../SimEnemy";
import { SimMain } from "../SimMain";
import { ISimAction } from "./ISimAction";

export class SimActionSpawnEnemies implements ISimAction {
    public execute(simMain: SimMain) {
        const level = simMain.currentLevel;
        if (!level) return;

        const gdLevel = simMain.gdRoot.levels[level.index];
        const step = level.currentStep;
        const gdWave = gdLevel.waves[level.currentWave];
        let spawnDelay = simMain.gdRoot.simulation.spawnDelay;
        switch (gdWave.size) {
            case EEnemySize.Huge:
                spawnDelay *= 2;
                break;
            case EEnemySize.Tiny:
                spawnDelay *= 0.5;
                break;
        }

        if (level.enemiesLeftToSpawn > 0 && level.lastEnemySpawnStep + spawnDelay * simMain.gdRoot.simulation.stepsPerSecond <= level.currentStep) {
            level.enemiesLeftToSpawn -= 1;
            level.lastEnemySpawnStep = step;

            const route = Math.floor(Math.random() * gdLevel.enemyRoutes.length);
            const enemy = new SimEnemy(gdWave.enemy, route);
            enemy.onPathUpdated(level);
            level.enemies.push(enemy);
        }

        if (level.nextWaveStep == step && !!gdLevel.waves[level.currentWave + 1]) {
            simMain.addCommand(new SimCommandStartNextWave());
        }
    }
}
