import { EEnemySize } from "../../GameData/index.js";
import { ISimAction, SimCommandStartNextWave, SimEnemy, SimLevel, SimMain } from "../index.js";

export class SimActionSpawnEnemies implements ISimAction {
    public execute(simMain: SimMain) {
        const simLevel = simMain.currentLevel as SimLevel;
        const gdLevel = simLevel.gdLevel;
        const step = simLevel.currentStep;
        const wave = gdLevel.waves[simLevel.currentWave];
        let spawnDelay = simMain.gdRoot.simulation.spawnDelay;
        switch (wave.size) {
            case EEnemySize.Huge:
                spawnDelay *= 2;
                break;
            case EEnemySize.Tiny:
                spawnDelay *= 0.5;
                break;
        }

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

            const route = Math.floor(Math.random() * gdLevel.enemyRoutes.length);
            const enemy = new SimEnemy(simMain.gdRoot, wave, route, simLevel);
            enemy.onPathUpdated();
            simLevel.simEnemies.push(enemy);
        }

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