/*
 * Copyright (C) 2024, KylinSoft Co., Ltd.

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "context.h"
#include <libkywc.h>

class Workspace::Private
{
  public:
    Private(Workspace *workspace);
    ~Private();
    void setup(kywc_workspace *workspace);

    QString uuid;
    QString name;
    uint32_t position;
    bool activated;

    kywc_workspace *k_workspace;

  private:
    Workspace *w_space;
    static void stateHandle(kywc_workspace *workspace, uint32_t mask);
    static void destroyHandle(kywc_workspace *workspace);
    static struct kywc_workspace_interface workspace_impl;
};

Workspace::Private::Private(Workspace *workspace) : w_space(workspace) {}

Workspace::Private::~Private() {}

void Workspace::Private::stateHandle(kywc_workspace *workspace, uint32_t mask)
{
    Workspace *w = (Workspace *)kywc_workspace_get_user_data(workspace);
    Workspace::Masks masks;
    if (mask & KYWC_WORKSPACE_STATE_NAME)
        masks |= Workspace::Mask::Name;
    if (mask & KYWC_WORKSPACE_STATE_POSITION)
        masks |= Workspace::Mask::Position;
    if (mask & KYWC_WORKSPACE_STATE_ACTIVATED)
        masks |= Workspace::Mask::Activated;

    w->pri->name = QString(workspace->name);
    w->pri->position = workspace->position;
    w->pri->activated = workspace->activated;

    emit w->stateUpdated(masks);
}

void Workspace::Private::destroyHandle(kywc_workspace *workspace)
{
    Workspace *w = (Workspace *)kywc_workspace_get_user_data(workspace);
    emit w->deleted();
}

struct kywc_workspace_interface Workspace::Private::workspace_impl = {
    stateHandle,
    destroyHandle,
};

void Workspace::Private::setup(kywc_workspace *workspace)
{
    uuid = workspace->uuid;
    name = workspace->name;
    position = workspace->position;
    activated = workspace->activated;
    k_workspace = workspace;
    kywc_workspace_set_user_data(workspace, w_space);
    kywc_workspace_set_interface(workspace, &workspace_impl);
}

Workspace::Workspace(QObject *parent) : pri(new Private(this)) {}

Workspace::~Workspace() {}

void Workspace::setup(kywc_workspace *workspace)
{
    pri->setup(workspace);
}

QString Workspace::name() const
{
    return pri->name;
}

QString Workspace::uuid() const
{
    return pri->uuid;
}

int Workspace::position() const
{
    return pri->position;
}

bool Workspace::isActivated() const
{
    return pri->activated;
}

void Workspace::setActivate()
{
    kywc_workspace_activate(pri->k_workspace);
}

void Workspace::move(int position)
{
    kywc_workspace_set_position(pri->k_workspace, position);
}

void Workspace::remove()
{
    kywc_workspace_remove(pri->k_workspace);
}

void Workspace::setName(const QString name)
{
    kywc_workspace_set_name(pri->k_workspace, name.toLocal8Bit().data());
}
