#include "Get_screen_size.h"
#include <cfloat>

#ifdef _WIN32
#include <windows.h>
#endif

#include <raylib.h>
#include <stdio.h>

RLVector2 screenSize_ = { 0, 0 };

void GetWorkArea(RLVector2& screenSize)
{
    // 调用新的GetScreenArea函数，使用工作区域模式
    GetScreenArea(screenSize, SCREEN_WORK_AREA);
}


void GetScreenArea(RLVector2& screenSize, ScreenAreaType areaType)
{
    RECT rc = {0};
    HMONITOR hMonitor = NULL;
    POINT pt = {0, 0};
    
    hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
    
    if (hMonitor != NULL)
    {
        MONITORINFOEX mi;
        mi.cbSize = sizeof(MONITORINFOEX);
        if (GetMonitorInfo(hMonitor, (MONITORINFO*)&mi))
        {
            switch (areaType)
            {
                case SCREEN_WORK_AREA:
                    rc = mi.rcWork;
                    printf("GetScreenArea: 使用工作区域（排除任务栏）");
                    break;
                    
                case SCREEN_MONITOR_AREA:
                    rc = mi.rcMonitor;
                    printf("GetScreenArea: 使用完整显示器区域");
                    break;
                    
                case SCREEN_PHYSICAL_AREA:
                    // 获取物理分辨率
                    DEVMODE devMode;
                    devMode.dmSize = sizeof(DEVMODE);
                    if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode))
                    {
                        rc.left = 0;
                        rc.top = 0;
                        rc.right = devMode.dmPelsWidth;
                        rc.bottom = devMode.dmPelsHeight;
                        printf("GetScreenArea: 使用物理分辨率 %dx%d", devMode.dmPelsWidth, devMode.dmPelsHeight);
                    }
                    else
                    {
                        printf("GetScreenArea: EnumDisplaySettings失败，使用显示器区域");
                        rc = mi.rcMonitor;
                    }
                    break;
            }
            
            // 记录详细信息
            printf("GetScreenArea: 显示器 '%s'", mi.szDevice);
            printf("GetScreenArea: 显示器完整区域: 位置(%d,%d) 大小(%dx%d)",
                      mi.rcMonitor.left, mi.rcMonitor.top, 
                      mi.rcMonitor.right - mi.rcMonitor.left, 
                      mi.rcMonitor.bottom - mi.rcMonitor.top);
            printf("GetScreenArea: 工作区域: 位置(%d,%d) 大小(%dx%d)",
                      mi.rcWork.left, mi.rcWork.top, 
                      mi.rcWork.right - mi.rcWork.left, 
                      mi.rcWork.bottom - mi.rcWork.top);
            
            // 获取DPI信息和缩放比例
            HDC hdc = GetDC(NULL);
            if (hdc)
            {
                int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
                int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
                float scaleX = (float)dpiX / 96.0f * 100.0f;
                float scaleY = (float)dpiY / 96.0f * 100.0f;
                printf("GetScreenArea: 系统DPI: X=%d, Y=%d (96 DPI为100%%)", dpiX, dpiY);
                printf("GetScreenArea: 缩放比例: X=%.0f%%, Y=%.0f%%", scaleX, scaleY);
                
                ReleaseDC(NULL, hdc);
            }
        }
        else
        {
            // 如果获取失败，回退到 SystemParametersInfo
            SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
            printf("GetScreenArea: GetMonitorInfo失败，使用SystemParametersInfo");
        }
    }
    else
    {
        // 如果没有找到显示器，回退到 SystemParametersInfo
        SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
        printf("GetScreenArea: MonitorFromPoint失败，使用SystemParametersInfo");
    }

    screenSize.x = rc.right - rc.left;
    screenSize.y = rc.bottom - rc.top;
    screenSize_.x = rc.right - rc.left;
    screenSize_.y = rc.bottom - rc.top;
    
    printf("GetScreenArea: 最终区域位置(%d,%d) 大小(%dx%d)", rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}

RLVector2 GetScreenSize() {
	return screenSize_;
}