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

HINSTANCE hInst;
HWND hSlider;
IAudioEndpointVolume* pVolume = NULL;
HWND hWnd = NULL; // Store the window handle

bool isWindowVisible = true; // Track window visibility

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

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.6), LWA_ALPHA); // You can remove or adjust this for transparency
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_KEYDOWN:
        if (wParam == VK_F3) {
            ShowWindow(hWnd, SW_HIDE); // Hide the window when F3 is pressed
            isWindowVisible = false;
        } else if (wParam == VK_F4) {
            if (!isWindowVisible) {
                ShowWindow(hWnd, SW_RESTORE); // Restore the window if it's minimized
                SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); // Bring to the top
                UpdateWindow(hWnd); // Force the window to redraw
                isWindowVisible = true;
            }
        }
        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, L"DiscordGainGUI", L"Jakacord", WS_OVERLAPPED | 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, 200));
    SendMessage(hSlider, TBM_SETPOS, TRUE, 100);

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

    ShowWindow(hWnd, SW_SHOW);
    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 this dll like when i click F3 it opens the menu and F4 it closes the menu