#include "Initialization.h"
#include "../Framework/UIFramework.h"
#include "../Menu/Menu.h"
#include "raylib.h"
#include "../../resource.h"
#include <string.h>
#include <stdio.h>
#include "../Backend/Font/FontManager.h"

static auto Font = Font::Get();

Initialization* Initialization::Get()
{
    static Initialization instance;
    return &instance;
}

bool Initialization::Init_Windows() {
    system("chcp 65001 > nul");
    Win_Flags |= RL_E_FLAG_WINDOW_UNDECORATED;
    RLSetConfigFlags(Win_Flags);
    RLInitWindow((int)Win_size.x, (int)Win_size.y, Win_Tittle);
    if (!RLIsWindowReady())
        return false;

    // 设置窗口图标（从资源加载）
    HWND hwnd = (HWND)RLGetWindowHandle();
    if (hwnd) {
        HICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
        if (hIcon) {
            SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
            SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
        }
    }

    RLSetExitKey(RL_E_KEY_NULL);
    Font->LoadCustom();
    

    return false;
}

void Initialization::Renderer() {
    Framework::Get()->NewFrame();
    ClearDragBlockRegions();

    RLBeginDrawing();
    {
        RLClearBackground({0,0,0,0});

        Menu::Get()->Render();
    }
    RLEndDrawing();
}

void Initialization::UpdateWindowDrag() {
    if (!m_dragEnabled)
        return;

    bool leftDown = RLIsMouseButtonDown(RL_E_MOUSE_BUTTON_LEFT);

    // Edge-triggered: start drag only when button just went down AND not in a widget area
    if (leftDown && !m_drag.wasDown && !IsMouseInBlockRegion()) {
        m_drag.dragging = true;
        GetCursorPos(&m_drag.mouseStart);

        HWND hwnd = (HWND)RLGetWindowHandle();
        RECT rc;
        GetWindowRect(hwnd, &rc);
        m_drag.windowStart = { rc.left, rc.top };
    }

    // Release left button: stop drag
    if (!leftDown)
        m_drag.dragging = false;

    m_drag.wasDown = leftDown;

    if (!m_drag.dragging)
        return;

    HWND hwnd = (HWND)RLGetWindowHandle();
    if (!hwnd)
        return;

    POINT mouse;
    GetCursorPos(&mouse);

    int dx = mouse.x - m_drag.mouseStart.x;
    int dy = mouse.y - m_drag.mouseStart.y;

    RECT win;
    GetWindowRect(hwnd, &win);
    int winW = win.right - win.left;
    int winH = win.bottom - win.top;

    // 当前屏幕工作区（多屏支持）
    HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
    MONITORINFO mi{};
    mi.cbSize = sizeof(mi);
    GetMonitorInfo(monitor, &mi);
    RECT work = mi.rcWork;

    int targetX = m_drag.windowStart.x + dx;
    int targetY = m_drag.windowStart.y + dy;

    targetX = Clamp(targetX, work.left, work.right - winW);
    targetY = Clamp(targetY, work.top, work.bottom - winH);

    SetWindowPos(hwnd, nullptr, targetX, targetY, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}

void Initialization::Clear() {
    Font->Clear();
    RLCloseWindow();
}

void Initialization::Set_WinSize(RLVector2 New_WinSize) {
    if (New_WinSize.x == 0 && New_WinSize.y == 0)
        return;
    if (New_WinSize.x == Win_size.x && New_WinSize.y == Win_size.y)
        return;

    RLSetWindowSize((int)New_WinSize.x, (int)New_WinSize.y);
    Win_size = New_WinSize;
}

bool Initialization::Set_Tittle(const char* New_Tittle) {
    if (New_Tittle == nullptr)
        return false;
    if (strcmp(Win_Tittle, New_Tittle) == 0)
        return false;

    RLSetWindowTitle(New_Tittle);
    Win_Tittle = New_Tittle;
    return true;
}

void Initialization::Set_Flags(int New_Flags) {
    RLSetWindowState(New_Flags);
}

void Initialization::Clear_Flags(int New_Flags) {
    RLClearWindowState(New_Flags);
}

void Initialization::AddDragBlockRegion(RLRectangle Rect) {
    m_dragBlockRegions.push_back({ Rect.x, Rect.y, Rect.width, Rect.height });
}

void Initialization::ClearDragBlockRegions() {
    m_dragBlockRegions.clear();
}

bool Initialization::IsMouseInBlockRegion() const {
    RLVector2 mouse = RLGetMousePosition();
    for (const auto& r : m_dragBlockRegions) {
        if (RLCheckCollisionPointRec(mouse, r))
            return true;
    }
    return false;
}

