#include "orchestrator.h"
#include <Windows.h>
#include <stdio.h>

typedef struct
{
    HMODULE hModule_dll;
    LPVOID lpPipeNamePointerFromInjector;
} ThreadParams;

DWORD WINAPI decryprion_thread_worker(LPVOID lpParam)
{
    ThreadParams* thread_params = (ThreadParams*)lpParam;

    OrchestratorConfig config;
    if (orchestrator_init(&config, (LPCWSTR)thread_params->lpPipeNamePointerFromInjector)) {
        orchestrator_run(&config);
    }
    orchestrator_cleanup(&config);

    FreeLibraryAndExitThread(thread_params->hModule_dll, 0);
    free(thread_params);
    return 0;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpReserved)
{
    if (reason == DLL_PROCESS_ATTACH) {
        DisableThreadLibraryCalls(hModule);
        
        ThreadParams* thread_params = (ThreadParams*)malloc(sizeof(ThreadParams));
        thread_params->hModule_dll = hModule;
        thread_params->lpPipeNamePointerFromInjector = lpReserved;

        HANDLE hThread = CreateThread(NULL, 0, decryprion_thread_worker, thread_params, 0, NULL);
        if (hThread) {
            CloseHandle(hThread);
        }
        else {
            free(thread_params);
        }
    }

    return TRUE;
}