#include "ukuiwaylandpointer.h"
#include "ukuishell.h"

class Q_DECL_HIDDEN UkuiShell::Private
{
public:
    UkuiWaylandPointer<ukui_shell, ukui_shell_destroy> shell;
    EventQueue *queue = nullptr;
};

class Q_DECL_HIDDEN UkuiShellSurface::Private
{
public:
    Private(UkuiShellSurface *q);
    ~Private();
    void setup(ukui_surface *surface);

    UkuiWaylandPointer<ukui_surface, ukui_surface_destroy> surface;
    QSize size;
    QPointer<Surface> parentSurface;
    UkuiShellSurface::Role role;
    UkuiShellSurface::SurfaceProperty property;

    static UkuiShellSurface *get(Surface *surface);

private:
    UkuiShellSurface *q;
    static QVector<Private *> s_surfaces;
};

QVector<UkuiShellSurface::Private *> UkuiShellSurface::Private::s_surfaces;

UkuiShell::UkuiShell(QObject *parent)
    : QObject(parent)
    , d(new Private)
{
}

UkuiShell::~UkuiShell()
{
    release();
}

void UkuiShell::destroy()
{
    if (!d->shell) {
        return;
    }
    Q_EMIT interfaceAboutToBeDestroyed();
    d->shell.destroy();
}

void UkuiShell::release()
{
    if (!d->shell) {
        return;
    }
    Q_EMIT interfaceAboutToBeReleased();
    d->shell.release();
}

void UkuiShell::setup(ukui_shell *shell)
{
    Q_ASSERT(!d->shell);
    Q_ASSERT(shell);
    d->shell.setup(shell);
}

void UkuiShell::setEventQueue(EventQueue *queue)
{
    d->queue = queue;
}

EventQueue *UkuiShell::eventQueue()
{
    return d->queue;
}

UkuiShellSurface *UkuiShell::createSurface(wl_surface *surface, QObject *parent)
{
    Q_ASSERT(isValid());
    auto kwS = Surface::get(surface);
    if (kwS) {
        if (auto s = UkuiShellSurface::Private::get(kwS)) {
            return s;
        }
    }
    UkuiShellSurface *s = new UkuiShellSurface(parent);
    connect(this, &UkuiShell::interfaceAboutToBeReleased, s, &UkuiShellSurface::release);
    connect(this, &UkuiShell::interfaceAboutToBeDestroyed, s, &UkuiShellSurface::destroy);
    auto w = ukui_shell_create_surface(d->shell, surface);
    if (d->queue) {
        d->queue->addProxy(w);
    }
    s->setup(w);
    s->d->parentSurface = QPointer<Surface>(kwS);
    return s;
}

UkuiShellSurface *UkuiShell::createSurface(Surface *surface, QObject *parent)
{
    return createSurface(*surface, parent);
}

bool UkuiShell::isValid() const
{
    return d->shell.isValid();
}

UkuiShell::operator ukui_shell *()
{
    return d->shell;
}

UkuiShell::operator ukui_shell *() const
{
    return d->shell;
}

UkuiShellSurface::Private::Private(UkuiShellSurface *q)
    : role(UkuiShellSurface::Role::Normal)
    , q(q)
{
    s_surfaces << this;
}

UkuiShellSurface::Private::~Private()
{
    s_surfaces.removeAll(this);
}

UkuiShellSurface *UkuiShellSurface::Private::get(Surface *surface)
{
    if (!surface) {
        return nullptr;
    }
    for (auto it = s_surfaces.constBegin(); it != s_surfaces.constEnd(); ++it) {
        if ((*it)->parentSurface == surface) {
            return (*it)->q;
        }
    }
    return nullptr;
}

void UkuiShellSurface::Private::setup(ukui_surface *s)
{
    Q_ASSERT(s);
    Q_ASSERT(!surface);
    surface.setup(s);
}

UkuiShellSurface::UkuiShellSurface(QObject *parent)
    : QObject(parent)
    , d(new Private(this))
{
}

UkuiShellSurface::~UkuiShellSurface()
{
    release();
}

void UkuiShellSurface::release()
{
    d->surface.release();
}

void UkuiShellSurface::destroy()
{
    d->surface.destroy();
}

void UkuiShellSurface::setup(ukui_surface *surface)
{
    d->setup(surface);
}

UkuiShellSurface *UkuiShellSurface::get(Surface *surface)
{
    if (auto s = UkuiShellSurface::Private::get(surface)) {
        return s;
    }

    return nullptr;
}

bool UkuiShellSurface::isValid() const
{
    return d->surface.isValid();
}

UkuiShellSurface::operator ukui_surface *()
{
    return d->surface;
}

UkuiShellSurface::operator ukui_surface *() const
{
    return d->surface;
}

void UkuiShellSurface::setPosition(const QPoint &point)
{
    Q_ASSERT(isValid());
    ukui_surface_set_position(d->surface, point.x(), point.y());
}

void UkuiShellSurface::setRole(UkuiShellSurface::Role role)
{
    Q_ASSERT(isValid());
    uint32_t wlRole = UKUI_SURFACE_ROLE_NORMAL;
    switch (role) {
    case Role::Normal:
        wlRole = UKUI_SURFACE_ROLE_NORMAL;
        break;
    case Role::Desktop:
        wlRole = UKUI_SURFACE_ROLE_DESKTOP;
        break;
    case Role::Panel:
        wlRole = UKUI_SURFACE_ROLE_PANEL;
        break;
    case Role::OnScreenDisplay:
        wlRole = UKUI_SURFACE_ROLE_ONSCREENDISPLAY;
        break;
    case Role::Notification:
        wlRole = UKUI_SURFACE_ROLE_NOTIFICATION;
        break;
    case Role::ToolTip:
        wlRole = UKUI_SURFACE_ROLE_TOOLTIP;
        break;
    case Role::CriticalNotification:
        wlRole = UKUI_SURFACE_ROLE_CRITICALNOTIFICATION;
        break;
    case Role::AppletPop:
        wlRole = UKUI_SURFACE_ROLE_APPLETPOPUP;
        break;
    case Role::ScreenLock:
        wlRole = UKUI_SURFACE_ROLE_SCREENLOCK;
        break;
    case Role::Watermark:
        wlRole = UKUI_SURFACE_ROLE_WATERMARK;
        break;
    case Role::SystemWindow:
        wlRole = UKUI_SURFACE_ROLE_SYSTEMWINDOW;
        break;
    case Role::InputPanel:
        wlRole = UKUI_SURFACE_ROLE_INPUTPANEL;
        break;
    case  Role::Logout:
        wlRole = UKUI_SURFACE_ROLE_LOGOUT;
        break;
    case  Role::ScreenLockNotification:
        wlRole = UKUI_SURFACE_ROLE_SCREENLOCKNOTIFICATION;
        break;
    default:
        Q_UNREACHABLE();
        break;
    }
    ukui_surface_set_role(d->surface, wlRole);
    d->role = role;
}

UkuiShellSurface::Role UkuiShellSurface::role() const
{
    return d->role;
}

void UkuiShellSurface::setSurfaceProperty(SurfaceProperty property,uint32_t value)
{
    uint32_t wlPropery = -1;
    switch(property)
    {
    case SurfaceProperty::NoTitleBar:
        wlPropery = UKUI_SURFACE_PROPERTY_NO_TITLEBAR;
        break;
    case SurfaceProperty::Theme:
        wlPropery = UKUI_SURFACE_PROPERTY_THEME;
        break;
    case SurfaceProperty::WindowRadius:
        wlPropery = UKUI_SURFACE_PROPERTY_WINDOW_RADIUS;
        break;
    case SurfaceProperty::BorderWidth:
        wlPropery = UKUI_SURFACE_PROPERTY_BORDER_WIDTH;
        break;
    case SurfaceProperty::BorderColor:
        wlPropery = UKUI_SURFACE_PROPERTY_BORDER_COLOR;
        break;
    case SurfaceProperty::ShadowRadius:
        wlPropery = UKUI_SURFACE_PROPERTY_SHADOW_RADIUS;
        break;
    case SurfaceProperty::ShadowOffset:
        wlPropery = UKUI_SURFACE_PROPERTY_SHADOW_OFFSET;
        break;
    case SurfaceProperty::ShadowColor:
        wlPropery = UKUI_SURFACE_PROPERTY_SHADOW_COLOR;
        break;
    default:
        Q_UNREACHABLE();
        break;
    }

    ukui_surface_set_property(d->surface, wlPropery,value);
    d->property = property;
}

UkuiShellSurface::SurfaceProperty UkuiShellSurface::surfaceProperty() const
{
    return d->property;
}

void UkuiShellSurface::setSkipTaskbar(bool skip)
{
    ukui_surface_set_skip_taskbar(d->surface, skip);
}

void UkuiShellSurface::setSkipSwitcher(bool skip)
{
    ukui_surface_set_skip_switcher(d->surface, skip);
}

void UkuiShellSurface::setPanelTakesFocus(bool takesFocus)
{
    ukui_surface_set_panel_takes_focus(d->surface, takesFocus);
}

void UkuiShellSurface::setPanelAutoHide(bool autoHide)
{
    ukui_surface_set_panel_auto_hide(d->surface, autoHide);
}

