#include "pch.h"
#include <windows.h>
#include <commctrl.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <iostream>

HINSTANCE hInst;
HWND hSlider;
HWND hMinimizeButton; // Button for minimizing the window
HWND hMaximizeButton; // Button for maximizing the window
HWND hCloseButton;    // Button for closing the window
IAudioEndpointVolume* pVolume = NULL;
HWND hWnd = NULL; // Store the window handle

bool isMenuVisible = true; // Track menu visibility
POINT dragStart; // Start position for dragging
bool isDragging = false; // Track drag state
bool isMaximized = false; // Track window maximized state

void SetMicrophoneGain(float gain) {
    if (pVolume) {
        pVolume->SetMasterVolumeLevelScalar(gain / 50000.0f, NULL);
    }
}

void CloseWindow() {
    PostQuitMessage(0); // Post quit message to close the application
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
    case WM_HSCROLL:
        if ((HWND)lParam == hSlider) {
            int gainValue = SendMessage(hSlider, TBM_GETPOS, 0, 0);
            SetMicrophoneGain((float)gainValue);
        }
        break;
    case WM_CREATE:
        SetLayeredWindowAttributes(hWnd, 0, (BYTE)(255 * 0.9), LWA_ALPHA); // Set window transparency

        // Create the Minimize button
        hMinimizeButton = CreateWindow(L"BUTTON", L"Min", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
            10, 10, 40, 30, hWnd, NULL, hInst, NULL);

        // Create the Maximize button
        hMaximizeButton = CreateWindow(L"BUTTON", L"Max", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
            60, 10, 40, 30, hWnd, NULL, hInst, NULL);

        // Create the Close button
        hCloseButton = CreateWindow(L"BUTTON", L"X", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
            110, 10, 40, 30, hWnd, NULL, hInst, NULL);
        break;

    case WM_COMMAND:
        if ((HWND)lParam == hMinimizeButton) {
            ShowWindow(hWnd, SW_MINIMIZE); // Minimize the window
        }
        else if ((HWND)lParam == hMaximizeButton) {
            if (isMaximized) {
                ShowWindow(hWnd, SW_RESTORE); // Restore the window to its original size
                isMaximized = false; // Update maximized state
            }
            else {
                ShowWindow(hWnd, SW_MAXIMIZE); // Maximize the window
                isMaximized = true; // Update maximized state
            }
        }
        else if ((HWND)lParam == hCloseButton) {
            CloseWindow(); // Close the window
        }
        break;

    case WM_LBUTTONDOWN:
        // Start dragging when the left mouse button is pressed
        if (lParam) {
            dragStart.x = LOWORD(lParam);
            dragStart.y = HIWORD(lParam);
            isDragging = true;
            SetCapture(hWnd); // Capture mouse input
        }
        break;

    case WM_MOUSEMOVE:
        // Handle dragging the window
        if (isDragging) {
            POINT pt;
            GetCursorPos(&pt); // Get global cursor position
            SetWindowPos(hWnd, NULL, pt.x - dragStart.x, pt.y - dragStart.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
        }
        break;

    case WM_LBUTTONUP:
        // Stop dragging when the left mouse button is released
        isDragging = false;
        ReleaseCapture(); // Release mouse capture
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

DWORD WINAPI CreateGUI(LPVOID lpParam) {
    WNDCLASS wc = { 0 };
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInst;
    wc.lpszClassName = L"DiscordGainGUI";
    wc.hbrBackground = CreateSolidBrush(RGB(30, 30, 30)); // Dark background

    RegisterClass(&wc);

    hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOOLWINDOW, L"DiscordGainGUI", L"Jakacord", WS_POPUP | WS_SYSMENU,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 200, NULL, NULL, hInst, NULL);

    hSlider = CreateWindow(TRACKBAR_CLASS, NULL, WS_CHILD | WS_VISIBLE | TBS_HORZ,
        50, 80, 300, 30, hWnd, NULL, hInst, NULL);

    SendMessage(hSlider, TBM_SETRANGE, TRUE, MAKELPARAM(0, 100));
    SendMessage(hSlider, TBM_SETPOS, TRUE, 50);  // Default position at 50%

    SetLayeredWindowAttributes(hWnd, 0, (BYTE)(255 * 0.7), LWA_ALPHA); // 70% Transparency (optional)

    ShowWindow(hWnd, SW_SHOW); // Start with the window visible
    UpdateWindow(hWnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

void InitializeAudio() {
    CoInitialize(NULL);
    IMMDeviceEnumerator* pEnumerator = NULL;
    IMMDevice* pDevice = NULL;
    CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnumerator);
    if (pEnumerator) {
        pEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pDevice);
        if (pDevice) {
            pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pVolume);
            pDevice->Release();
        }
        pEnumerator->Release();
    }
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
    if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
        hInst = hModule;
        AllocConsole();
        FILE* f;
        freopen_s(&f, "CONOUT$", "w", stdout);
        std::cout << "Injected Successfully" << std::endl;
        InitializeAudio();
        CreateThread(NULL, 0, CreateGUI, NULL, 0, NULL);
    }
    return TRUE;
}




make it to where i press F5 it opens and closes the menu