/*
 * Copyright (C) 2026, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, 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 General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: iaom <zhangpengfei@kylinos.cn>
 */

#ifndef KYWC_OUTPUT_MANAGER_H
#define KYWC_OUTPUT_MANAGER_H

#include <QObject>
#include <QList>
#include <KWayland/Client/event_queue.h>

struct kywc_output_v1;
struct kywc_output_mode_v1;
struct kywc_output_manager_v1;
using namespace KWayland::Client;

namespace UkuiQuick::WaylandClient {
namespace Test {
class KywcOutputManagerTest;
}

class KywcOutput;

class KywcOutputMode : public QObject
{
    Q_OBJECT
public:
    ~KywcOutputMode() override;

    void setup(kywc_output_mode_v1 *mode);
    void release();
    void destroy();
    bool isValid() const;

    QSize size() const;
    int refreshRate() const;
    bool isPreferred() const;

Q_SIGNALS:
    void sizeChanged(const QSize &size);
    void refreshRateChanged(int refreshRate);
    void preferredChanged(bool isPreferred);
    void finished();

private:
    friend class KywcOutput;
    friend class Test::KywcOutputManagerTest;
    explicit KywcOutputMode(QObject *parent = nullptr);
    class Private;
    QScopedPointer<Private> d;
};

class KywcOutputManager : public QObject
{
    Q_OBJECT
public:
    explicit KywcOutputManager(QObject *parent = nullptr);
    ~KywcOutputManager() override;

    bool isValid() const;
    void release();
    void destroy();
    void setup(kywc_output_manager_v1 *manager);
    void setEventQueue(EventQueue *queue);
    EventQueue *eventQueue() const;

    void stop();
    QList<KywcOutput *> outputs() const;
    KywcOutput *primaryOutput() const;

Q_SIGNALS:
    void outputCreated(KywcOutput *output, const QString &uuid);
    void primaryOutputChanged(KywcOutput *output);
    void done();
    void interfaceAboutToBeReleased();
    void interfaceAboutToBeDestroyed();
    void removed();

private:
    class Private;
    QScopedPointer<Private> d;
};

class KywcOutput : public QObject
{
    Q_OBJECT
public:
    ~KywcOutput() override;

    void setup(kywc_output_v1 *output);
    void release();
    void destroy();
    bool isValid() const;

    QString name() const;
    QString make() const;
    QString model() const;
    QString serialNumber() const;
    QString description() const;
    QSize physicalSize() const;
    uint32_t capabilities() const;
    bool isEnabled() const;
    QPoint position() const;
    int transform() const;
    qreal scale() const;
    uint32_t power() const;
    uint32_t brightness() const;
    uint32_t colorTemp() const;
    QString uuid() const;
    QList<KywcOutputMode *> supportedModes() const;
    KywcOutputMode *currentMode() const;

Q_SIGNALS:
    void nameChanged(const QString &name);
    void makeChanged(const QString &make);
    void modelChanged(const QString &model);
    void serialNumberChanged(const QString &serial);
    void descriptionChanged(const QString &description);
    void physicalSizeChanged(const QSize &size);
    void capabilitiesChanged(uint32_t capabilities);
    void enabledChanged(bool enabled);
    void positionChanged(const QPoint &pos);
    void transformChanged(int transform);
    void scaleChanged(qreal scale);
    void powerChanged(uint32_t power);
    void brightnessChanged(uint32_t brightness);
    void colorTempChanged(uint32_t colorTemp);
    void finished();

private:
    friend class KywcOutputManager;
    friend class Test::KywcOutputManagerTest;
    explicit KywcOutput(QObject *parent = nullptr);
    class Private;
    QScopedPointer<Private> d;
};

}

#endif //KYWC_OUTPUT_MANAGER_H
