// spawn_controller.cpp - Implementació del controlador de spawn
// © 2025 Orni Attack

#include "spawn_controller.hpp"

#include <array>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <utility>

#include "core/types.hpp"
#include "game/entities/enemy.hpp"
#include "stage_config.hpp"

namespace StageSystem {

SpawnController::SpawnController()
    : config_(nullptr),
      temps_transcorregut_(0.0F),
      index_spawn_actual_(0),
      ship_position_(nullptr) {}

void SpawnController::configure(const StageConfig* config) {
    config_ = config;
}

void SpawnController::start() {
    if (config_ == nullptr) {
        std::cerr << "[SpawnController] Error: config_ es null" << '\n';
        return;
    }

    reset();
    generar_spawn_events();

    std::cout << "[SpawnController] Stage " << static_cast<int>(config_->stage_id)
              << ": generats " << spawn_queue_.size() << " spawn events" << '\n';
}

void SpawnController::reset() {
    spawn_queue_.clear();
    temps_transcorregut_ = 0.0F;
    index_spawn_actual_ = 0;
}

void SpawnController::update(float delta_time, std::array<Enemy, 15>& orni_array, bool pausar) {
    if ((config_ == nullptr) || spawn_queue_.empty()) {
        return;
    }

    // Increment timer only when not paused
    if (!pausar) {
        temps_transcorregut_ += delta_time;
    }

    // Process spawn events
    while (index_spawn_actual_ < spawn_queue_.size()) {
        SpawnEvent& event = spawn_queue_[index_spawn_actual_];

        if (event.spawnejat) {
            index_spawn_actual_++;
            continue;
        }

        if (temps_transcorregut_ >= event.temps_spawn) {
            // Find first inactive enemy
            for (auto& enemy : orni_array) {
                if (!enemy.isActive()) {
                    spawn_enemic(enemy, event.type, ship_position_);
                    event.spawnejat = true;
                    index_spawn_actual_++;
                    break;
                }
            }

            // If no slot available, try next frame
            if (!event.spawnejat) {
                break;
            }
        } else {
            // Not yet time for this spawn
            break;
        }
    }
}

bool SpawnController::tots_enemics_spawnejats() const {
    return index_spawn_actual_ >= spawn_queue_.size();
}

bool SpawnController::tots_enemics_destruits(const std::array<Enemy, 15>& orni_array) const {
    if (!tots_enemics_spawnejats()) {
        return false;
    }

    for (const auto& enemy : orni_array) {
        if (enemy.isActive()) {
            return false;
        }
    }

    return true;
}

uint8_t SpawnController::get_enemics_vius(const std::array<Enemy, 15>& orni_array) const {
    uint8_t count = 0;
    for (const auto& enemy : orni_array) {
        if (enemy.isActive()) {
            count++;
        }
    }
    return count;
}

uint8_t SpawnController::get_enemics_spawnejats() const {
    return static_cast<uint8_t>(index_spawn_actual_);
}

void SpawnController::generar_spawn_events() {
    if (config_ == nullptr) {
        return;
    }

    for (uint8_t i = 0; i < config_->total_enemies; i++) {
        float spawn_time = config_->config_spawn.delay_inicial +
            (i * config_->config_spawn.interval_spawn);

        EnemyType type = seleccionar_tipus_aleatori();

        spawn_queue_.push_back({spawn_time, type, false});
    }
}

EnemyType SpawnController::seleccionar_tipus_aleatori() const {
    if (config_ == nullptr) {
        return EnemyType::PENTAGON;
    }

    // Weighted random selection based on distribution
    int rand_val = std::rand() % 100;

    if (std::cmp_less(rand_val, config_->distribucio.pentagon)) {
        return EnemyType::PENTAGON;
    }
    if (rand_val < config_->distribucio.pentagon + config_->distribucio.cuadrado) {
        return EnemyType::QUADRAT;
    }
    return EnemyType::MOLINILLO;
}

void SpawnController::spawn_enemic(Enemy& enemy, EnemyType type, const Vec2* ship_pos) {
    // Initialize enemy (with safe spawn if ship_pos provided)
    enemy.init(type, ship_pos);

    // Apply difficulty multipliers
    aplicar_multiplicadors(enemy);
}

void SpawnController::aplicar_multiplicadors(Enemy& enemy) const {
    if (config_ == nullptr) {
        return;
    }

    // Apply velocity multiplier
    float base_vel = enemy.getBaseVelocity();
    enemy.setVelocity(base_vel * config_->multiplicadors.velocity);

    // Apply rotation multiplier
    float base_rot = enemy.getBaseRotation();
    enemy.setRotation(base_rot * config_->multiplicadors.rotation);

    // Apply tracking strength (only affects QUADRAT)
    enemy.setTrackingStrength(config_->multiplicadors.tracking_strength);
}

}  // namespace StageSystem
