#include "pch.h" // Precompiled header
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <shellapi.h>

// Function prototype for GUI and Hotkey handling
DWORD WINAPI OpenGUI(LPVOID lpParam);
void RestartDiscord();
void ToggleWindow();

// Global variables for the GUI
HWND hWnd;
HINSTANCE hInstance;
BOOL isWindowVisible = FALSE;

// Custom Drawing Method for a more "ImGui-like" appearance
void PaintWindow(HDC hdc) {
    // Fill background with a dark color
    RECT rect;
    GetClientRect(hWnd, &rect);
    FillRect(hdc, &rect, (HBRUSH)(COLOR_BTNSHADOW + 1)); // Dark background

    // Set up for text drawing
    SetBkMode(hdc, TRANSPARENT);
    SetTextColor(hdc, RGB(255, 255, 255)); // White text

    // Draw title text
    const TCHAR* titleText = _T("Jakacord");
    TextOut(hdc, 10, 10, titleText, _tcslen(titleText));

    // Draw a button-like rectangle (simulating an ImGui button)
    RECT buttonRect = { 50, 80, 400, 120 }; // Coordinates for Restart Discord button
    FillRect(hdc, &buttonRect, (HBRUSH)(COLOR_WINDOW + 1)); // Button background
    DrawText(hdc, _T("Restart Discord"), -1, &buttonRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

// Window Procedure
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_PAINT: {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        PaintWindow(hdc);
        EndPaint(hwnd, &ps);
        break;
    }
    case WM_LBUTTONDOWN: {
        // Check if the mouse is inside the button area
        POINT pt = { LOWORD(lParam), HIWORD(lParam) };
        RECT buttonRect = { 50, 80, 400, 120 };

        if (PtInRect(&buttonRect, pt)) {
            // Call RestartDiscord when the button is clicked
            RestartDiscord();
        }
        break;
    }
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_HOTKEY: // Hotkey processing
        if (wParam == 1) { // F5 hotkey
            ToggleWindow();
        }
        break;
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

// Function to toggle GUI visibility
void ToggleWindow() {
    isWindowVisible = !isWindowVisible;
    ShowWindow(hWnd, isWindowVisible ? SW_SHOW : SW_HIDE);
}

// Function to create GUI
DWORD WINAPI OpenGUI(LPVOID lpParam) {
    // Get the current instance
    hInstance = GetModuleHandle(NULL);

    // Register window class
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = _T("Jaka"); // Use consistent class name
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    if (!RegisterClass(&wc)) {
        MessageBox(NULL, _T("Failed to register window class!"), _T("Error"), MB_ICONERROR);
        return 1;
    }

    // Create the window
    hWnd = CreateWindowEx(
        WS_EX_TOOLWINDOW, // Tool window style
        _T("Jaka"), // Consistent class name
        _T("Jakacord"), // Window title
        WS_OVERLAPPEDWINDOW, // Use overlapped window style
        CW_USEDEFAULT, CW_USEDEFAULT, 450, 250,
        NULL, NULL, hInstance, NULL);

    if (!hWnd) {
        DWORD error = GetLastError();
        TCHAR errorMsg[256];
        _stprintf_s(errorMsg, _T("Failed to create window! Error code: %lu"), error);
        MessageBox(NULL, errorMsg, _T("Error"), MB_ICONERROR);
        return 1;
    }

    // Create the hotkey (F5)
    if (!RegisterHotKey(hWnd, 1, 0, VK_F5)) {
        MessageBox(NULL, _T("Failed to register hotkey!"), _T("Error"), MB_ICONERROR);
        return 1;
    }

    ShowWindow(hWnd, SW_HIDE); // Start hidden
    UpdateWindow(hWnd);

    // Message loop to keep the GUI responsive
    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // Unregister the hotkey before exiting
    UnregisterHotKey(hWnd, 1);
    return 0;
}

// Function to restart Discord
void RestartDiscord() {
    system("taskkill /F /IM discord.exe");
    Sleep(3000); // Wait for a moment to ensure Discord has closed

    char discordPath[MAX_PATH];
    if (GetEnvironmentVariableA("LOCALAPPDATA", discordPath, MAX_PATH)) {
        strcat_s(discordPath, "\\Discord\\Update.exe");
        ShellExecuteA(0, "open", discordPath, "--processStart Discord.exe", NULL, SW_SHOW);
        MessageBox(NULL, _T("Discord has been restarted."), _T("Notification"), MB_OK | MB_ICONINFORMATION);
    }
    else {
        MessageBox(NULL, _T("Failed to get Discord path."), _T("Error"), MB_OK | MB_ICONERROR);
    }
}

// Entry point of the DLL - manages loading and unloading
extern "C" __declspec(dllexport) BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
    switch (ul_reason_for_call) {
    case DLL_PROCESS_ATTACH:
        AllocConsole(); // Optional: allocate a console for debug output
        {
            FILE* fp;
            freopen_s(&fp, "CONOUT$", "w", stdout);
            std::cout << "Welcome to Jakasauce! You are now 5dB over the limit." << std::endl;
        }

        // Start the GUI thread
        CreateThread(NULL, 0, OpenGUI, NULL, 0, NULL);
        break;

    case DLL_PROCESS_DETACH:
        FreeConsole();
        break;
    }
    return TRUE;
}





i dont see the gain and restart button? and enhance gui