#pragma once

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

#include "native_ui/ui_types.hpp"
#include "native_ui/tooltip_view.hpp"
#include "native_ui/radar_hud.hpp"
#include "native_ui/settings_panel.hpp"

#include <memory>
#include <string_view>

namespace poe2::ui {

class OverlayWindow {
public:
    OverlayWindow();
    ~OverlayWindow();

    OverlayWindow(const OverlayWindow&) = delete;
    OverlayWindow& operator=(const OverlayWindow&) = delete;

    /// @brief Khởi tạo cửa sổ trong suốt Layered Window
    bool Create(int width = 1920, int height = 1080);

    /// @brief Hủy bỏ cửa sổ và giải phóng toàn bộ tài nguyên GDI
    void Destroy();

    /// @brief Bật/tắt chế độ Streamer an toàn (WDA_EXCLUDEFROMCAPTURE - Ẩn khỏi OBS/Discord)
    void SetStreamerMode(bool enabled);
    bool IsStreamerMode() const noexcept { return streamerMode_; }

    /// @brief Tự động bám theo tọa độ và kích thước cửa sổ game Path of Exile 2
    bool FollowGameWindow(HWND hGame);

    /// @brief Bắt đầu chu trình vẽ khung hình mới
    void BeginFrame();

    /// @brief Xóa trắng bề mặt đệm về độ trong suốt 100%
    void Clear(Color bgColor = Color{0, 0, 0, 0});

    /// @brief Vẽ hình chữ nhật có viền và màu nền (Hỗ trợ Alpha Blending)
    void DrawRect(const Rect& rect, Color fillColor, Color borderColor, int borderWidth = 1);

    /// @brief Vẽ chuỗi văn bản công thái học
    void DrawTextLine(int x, int y, std::string_view text, Color color, bool bold = false);

    /// @brief Vẽ vòng tròn la bàn Radar
    void DrawCircle(int cx, int cy, int radius, Color color, bool fill = false);

    /// @brief Hoàn tất khung hình và đẩy lên màn hình qua UpdateLayeredWindow
    void EndFrame();

    /// @brief Hiển thị bảng soi đồ Tooltip
    void RenderTooltip(const TooltipLayout& layout);

    /// @brief Hiển thị Radar la bàn POI
    void RenderRadar(const RadarLayout& layout);

    /// @brief Hiển thị Bảng điều khiển cấu hình In-Game
    void RenderSettings(const SettingsPanelLayout& layout);

    HWND GetHwnd() const noexcept { return hwnd_; }
    int GetWidth() const noexcept { return width_; }
    int GetHeight() const noexcept { return height_; }
    bool IsCreated() const noexcept { return hwnd_ != nullptr; }

private:
    void ResizeBuffer(int width, int height);

    HWND hwnd_{nullptr};
    HDC hMemDc_{nullptr};
    HBITMAP hBitmap_{nullptr};
    HBITMAP hOldBitmap_{nullptr};
    uint32_t* pixelBuffer_{nullptr};
    HFONT hFontNormal_{nullptr};
    HFONT hFontBold_{nullptr};

    int width_{1920};
    int height_{1080};
    int posX_{0};
    int posY_{0};
    bool streamerMode_{false};
};

} // namespace poe2::ui
