#pragma once
#include "game/config.hpp"
#include "algos/dinkyecs.hpp"
#include <optional>
#include "game/components.hpp"
#include <unordered_map>
#include "ai/ai.hpp"

namespace combat {

  enum class BattleHostState {
    not_host = 0,
    agree = 1,
    disagree = 2,
    out_of_ap = 3
  };

  struct Combatant {
    DinkyECS::Entity entity = DinkyECS::NONE;
    ai::EntityAI* ai = nullptr;
    components::Combat* combat = nullptr;
    bool is_host=false;
  };

  struct BattleResult {
    Combatant enemy;
    std::string wants_to;
    int cost;
    BattleHostState host_state;
  };

  struct BattleEngine {
    std::unordered_map<DinkyECS::Entity, Combatant> $combatants;
    std::vector<BattleResult> $pending_actions;
    std::unordered_map<std::string, ai::Action> $player_requests;
    components::Combat* $host_combat = nullptr;

    void add_enemy(Combatant ba);
    Combatant& get_enemy(DinkyECS::Entity entity);
    bool plan();
    std::optional<BattleResult> next();
    void dump();
    void set(DinkyECS::Entity entity, const std::string& state, bool setting);
    void set_all(const std::string& state, bool setting);
    bool player_request(const std::string& request);
    int player_pending_ap();
    void clear_requests();
    void ap_refresh();
  };
}
