#pragma once

#include "xdg-shell-client-protocol.h"
#include "wayland-client.h"

#include "Window.h"
#include "SharedMemory.h"
#include "WaylandBuffer.h"

#include "IPlatformWindow.h"

struct wl_surface;
struct xdg_surface;
struct xdg_toplevel;

class WaylandEglInterface;
class WaylandEglWindowInterface;

class FontsManager;

class WaylandSurface : public IPlatformWindow
{
public:

    static void add(mt::Window* window, wl_compositor* compositor, xdg_wm_base* xdg_wm_base, std::shared_ptr<WaylandBuffer> buffer, FontsManager* fontsManager, WaylandEglInterface* eglInterface);

    WaylandSurface(mt::Window* window, wl_compositor* compositor, xdg_wm_base* xdg_wm_base, std::shared_ptr<WaylandBuffer> buffer, WaylandEglInterface* eglInterface);

    ~WaylandSurface();

    void onConfigure(xdg_surface *xdg_surface, uint32_t serial);

    void show();

    void map();

    void clear();

    void onResize(unsigned width, unsigned height)
    {

    }

private:
    void initialize();

    void beforePaint(mt::Screen* screen) override;
    void afterPaint(mt::Screen* screen) override;

    void beforePaintSoftware();
    void afterPaintSoftware();

    unsigned getImageBufferSize() const;

    mt::Window* mWindow{nullptr};

    wl_compositor* mCompositor{nullptr};
    xdg_wm_base* mXdgWmBase{nullptr};
    std::shared_ptr<WaylandBuffer> mBuffer;
    std::unique_ptr<WaylandEglWindowInterface> mEglWindowInterface;

    wl_surface* mSurface{nullptr};
    xdg_surface* mXdgSurface{nullptr};
    xdg_surface_listener mXdgSurfaceListener;

    xdg_toplevel* mXdgTopLevel{nullptr};
    unsigned mWorkingBitDepth{4};
    unsigned mNumFrameBuffers{1};
};

using WaylandSurfacePtr = std::unique_ptr<WaylandSurface>;
