#pragma once

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

#include <string>
#include <vector>
#include <unordered_map>
#include <memory>
#include <mutex>
#include <cstdint>

#include "combat/skill_definition.hpp"

// ==========================================================
// SkillProfile - Tập hợp Kỹ Năng & Trạng Thái Biến Đếm (Trackers)
// ==========================================================

class SkillProfile {
public:
    SkillProfile() = default;

    bool LoadFromToml(const std::string& tomlPath);
    bool ParseFromString(const std::string& content);

    const std::string& Name() const { return m_name; }
    void SetName(const std::string& name) { m_name = name; }

    const std::vector<FlexibleSkill>& Skills() const { return m_skills; }
    std::vector<FlexibleSkill>& Skills() { return m_skills; }

    const FlexibleSkill* FindSkillByVk(uint8_t vk) const;
    FlexibleSkill* FindSkillByVk(uint8_t vk);

    const FlexibleSkill* FindSkillByName(const std::string& name) const;
    FlexibleSkill* FindSkillByName(const std::string& name);

    // Quản lý biến đếm / Trackers (vd: combo_points, staff_charges, rage)
    int64_t GetTracker(const std::string& name) const;
    void SetTracker(const std::string& name, int64_t value);
    void IncrementTracker(const std::string& name, int64_t delta = 1);
    void ResetTracker(const std::string& name);
    const std::unordered_map<std::string, int64_t>& Trackers() const { return m_trackers; }

    void Clear();

private:
    std::string m_name = "Default";
    std::string m_version = "1.0";
    std::vector<FlexibleSkill> m_skills;
    std::unordered_map<std::string, int64_t> m_trackers;
};

// ==========================================================
// SkillEngine - Dedicated Skill Engine & Animation Lock FSM
// ==========================================================

class SkillEngine {
public:
    SkillEngine();
    ~SkillEngine() = default;

    // 1. Quản lý Profile & Nạp Động (Hot-Reload / Dual-Buffer Swapping)
    bool LoadProfile(const std::string& tomlPath);
    bool HotSwapProfile(std::shared_ptr<SkillProfile> newProfile);
    std::shared_ptr<SkillProfile> ActiveProfile() const;

    // 2. Tuyển chọn hành động tối ưu thời gian thực (120Hz Hot Path)
    SkillDecision SelectBestAction(CombatContext ctx, uint64_t nowMs);

    // 3. Vòng đời hoạt ảnh & Phản hồi thi triển
    void OnSkillCastStarted(uint8_t vkCode, uint64_t nowMs);
    void OnSkillExecuted(uint8_t vkCode, uint64_t nowMs);
    void OnSkillInterrupted(uint64_t nowMs);

    // 4. Áp dụng hiệu ứng phụ (Effects / Tag / Debuff / Resonance Bell)
    void ApplySkillEffects(const FlexibleSkill& skill, uint64_t nowMs);

    // 5. Cấu hình & Trạng thái an toàn
    void SetEmergencyCancelEnabled(bool enabled) { m_emergencyCancelEnabled = enabled; }
    bool IsEmergencyCancelEnabled() const { return m_emergencyCancelEnabled; }

    void SetEmergencyEsThreshold(float threshold) { m_emergencyEsPct = threshold; }
    float EmergencyEsThreshold() const { return m_emergencyEsPct; }

    void SetEmergencyDodgeVk(uint8_t vk) { m_emergencyDodgeVk = vk; }
    uint8_t EmergencyDodgeVk() const { return m_emergencyDodgeVk; }

    const AnimationState& GetAnimationState() const { return m_animState; }

    // Quản lý trạng thái thực thể thực địa (Resonance Bell, Mark)
    bool IsTargetMarked(uint64_t nowMs) const;
    void SetTargetMark(uint32_t targetId, uint32_t durationMs, uint64_t nowMs);
    void ClearTargetMark();

    bool IsBellActive(uint64_t nowMs) const;
    void SpawnBell(float x, float y, uint32_t durationMs, uint64_t nowMs);
    void ClearBell();

    void ResetState();

private:
    mutable std::mutex m_profileMutex;
    std::shared_ptr<SkillProfile> m_activeProfile;

    AnimationState m_animState;

    bool m_emergencyCancelEnabled = true;
    float m_emergencyEsPct = 0.70f;
    uint8_t m_emergencyDodgeVk = VK_SPACE;

    // Trạng thái Debuff trên mục tiêu
    uint32_t m_markedTargetId = 0;
    uint64_t m_markExpiryMs = 0;

    // Trạng thái Thực thể phụ trợ trên sân (Resonance Bell)
    bool m_bellActive = false;
    float m_bellX = 0.0f;
    float m_bellY = 0.0f;
    uint64_t m_bellExpiryMs = 0;
};
