#pragma once

#include <optional>
#include <vector>

#include <QVector2D>

#include "Building.h"
#include "FactionComponent.h"
#include "BeamFiredEvent.h"
#include "GameConfig.h"
#include "PositionComponent.h"
#include "Tick.h"
#include "WeaponComponent.h"

#include "entt/entity/entity.hpp"

class EntityAdmin;

class CombatSystem
{
public:
    explicit CombatSystem(const GameConfig& config);

    void tick(Tick currentTick,
              EntityAdmin& admin,
              std::vector<BeamFiredEvent>& outBeamFiredEvents);

    void applyPendingDamage(Tick currentTick, EntityAdmin& admin);

private:
    struct PendingDamage
    {
        entt::entity target;
        float        amount;
        Tick         appliesAt;
    };

    std::vector<PendingDamage> m_pendingDamage;

    void resolveWeapon(
        entt::entity shipEntity,
        WeaponComponent& weapon,
        const PositionComponent& ownPos,
        const FactionComponent& ownFaction,
        Tick currentTick,
        EntityAdmin& admin,
        std::vector<BeamFiredEvent>& out);

    const GameConfig& m_config;
};
