#pragma once
#include "WindowClassManager.hpp"
#include <Windows.h>
#include <functional>
#include <memory>
#include <string>

using OnClipboardContentChangedCallback = std::function<void(const std::wstring& content)>;

class Clipboard
{
    friend std::default_delete<Clipboard>;

public:
    static void Initialize(const wchar_t* windowClassName);

    static void SetCallback(OnClipboardContentChangedCallback callback);
    static void SetClipboardText(const std::wstring& newText);
    static std::wstring GetClipboardText();

private:
    Clipboard(const wchar_t* windowClassName);
    ~Clipboard();

    static DWORD WINAPI ThreadProcStatic(LPVOID lpParam);
    void ThreadMain();
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

    void SetClipboardTextImpl(const std::wstring& newText);
    std::wstring GetClipboardTextImpl();

    std::unique_ptr<WindowClassManager> mWindowClassManager;
    HWND mhWindow = nullptr;
    HANDLE mhThread = nullptr;
    HANDLE mhReadyEvent = nullptr;

    const std::wstring mWindowsClassName;

    OnClipboardContentChangedCallback mCallback = nullptr;
    SRWLOCK mCallbackLock;

    static std::unique_ptr<Clipboard> sInstance;
};