// SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
// SPDX-FileCopyrightText: 2024 Arjen Hiemstra <ahiemstra@heimr.nl>
//
// This file is automatically generated from property.cpp.j2.
// To regenerate, run `tools/propertygenerator/generate_properties.py`.

#include "DisplayPropertyGroup.h"

#include <QRegularExpression>

#include "PropertiesTypes.h"
#include "QDataStreamExtras.h"

using namespace Union::Properties;
using namespace Qt::StringLiterals;

class Union::Properties::DisplayPropertyGroupPrivate
{
public:
    std::optional<bool> visible;
    std::optional<qreal> opacity;
};

DisplayPropertyGroup::DisplayPropertyGroup()
    : d(std::make_unique<DisplayPropertyGroupPrivate>())
{
}

DisplayPropertyGroup::DisplayPropertyGroup(const DisplayPropertyGroup &other)
    : d(std::make_unique<DisplayPropertyGroupPrivate>())
{
    d->visible = other.d->visible;
    d->opacity = other.d->opacity;
}

DisplayPropertyGroup::DisplayPropertyGroup(DisplayPropertyGroup &&other)
    : d(std::move(other.d))
{
}

DisplayPropertyGroup::~DisplayPropertyGroup() = default;

DisplayPropertyGroup &DisplayPropertyGroup::operator=(const DisplayPropertyGroup &other)
{
    if (this != &other) {
        d->visible = other.d->visible;
        d->opacity = other.d->opacity;
    }
    return *this;
}

DisplayPropertyGroup &DisplayPropertyGroup::operator=(DisplayPropertyGroup &&other)
{
    std::swap(d, other.d);
    return *this;
}

std::optional<bool> DisplayPropertyGroup::visible() const
{
    return d->visible;
}

void DisplayPropertyGroup::setVisible(const std::optional<bool> &newValue)
{
    if (newValue == d->visible) {
        return;
    }

    d->visible = newValue;
}

std::optional<qreal> DisplayPropertyGroup::opacity() const
{
    return d->opacity;
}

void DisplayPropertyGroup::setOpacity(const std::optional<qreal> &newValue)
{
    if (newValue == d->opacity) {
        return;
    }

    d->opacity = newValue;
}

bool DisplayPropertyGroup::hasAnyValue() const
{
    if (d->visible.has_value()) {
        return true;
    }
    if (d->opacity.has_value()) {
        return true;
    }
    return false;
}

bool DisplayPropertyGroup::isEmpty() const
{
    if (!hasAnyValue()) {
        return true;
    }

    if (d->visible.has_value() && d->visible.value() != emptyValue<bool>()) {
        return false;
    }
    if (d->opacity.has_value() && d->opacity.value() != emptyValue<qreal>()) {
        return false;
    }

    return true;
}

QString DisplayPropertyGroup::toString(int indentation, ToStringFlags flags) const
{
    if (!hasAnyValue()) {
        return u"(empty)"_s;
    }

    const bool multiline = flags & ToStringFlag::MultiLine;
    const bool types = flags & ToStringFlag::Types;

    QString result;
    QTextStream out(&result);

    constexpr auto indent = [](int amount, bool multiline, bool first) {
        if (multiline) {
            return QByteArray(amount, ' ');
        } else if (!first) {
            return QByteArray(", ");
        } else {
            return QByteArray(" ");
        }
    };

    const QByteArray maybeNewLine = multiline ? "\n" : "";
    const QByteArray empty = "(empty)";

    if (types) {
        out << "DisplayPropertyGroup(" << maybeNewLine;
    } else if (indentation > 0) {
        out << maybeNewLine;
    }

    out << indent(indentation, multiline, true) << "visible: ";
    if (d->visible) {
        out << d->visible.value() << maybeNewLine;
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "opacity: ";
    if (d->opacity) {
        out << d->opacity.value() << maybeNewLine;
    } else {
        out << empty << maybeNewLine;
    }

    if (types) {
        out << indent(indentation - 2, multiline, true) << ")";
    }
    out << maybeNewLine;

    out.flush();

    return result;
}

void DisplayPropertyGroup::resolveProperties(const DisplayPropertyGroup *source, DisplayPropertyGroup *destination)
{
    if (!source || !destination) {
        return;
    }

    if (!destination->d->visible.has_value()) {
        destination->d->visible = source->d->visible;
    }
    if (!destination->d->opacity.has_value()) {
        destination->d->opacity = source->d->opacity;
    }
}

std::unique_ptr<DisplayPropertyGroup> DisplayPropertyGroup::empty()
{
    auto result = std::make_unique<DisplayPropertyGroup>();
    result->d->visible = emptyValue<bool>();
    result->d->opacity = emptyValue<qreal>();
    return result;
}

bool Union::Properties::operator==(const DisplayPropertyGroup &left, const DisplayPropertyGroup &right)
{
    if (left.visible() != right.visible()) {
        return false;
    }
    if (left.opacity() != right.opacity()) {
        return false;
    }
    return true;
}

QDebug operator<<(QDebug debug, Union::Properties::DisplayPropertyGroup *type)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << qPrintable(type->toString(0, ToStringFlag::Types));
    return debug;
}

QDataStream &operator<<(QDataStream &stream, const Union::Properties::DisplayPropertyGroup *type)
{
    stream << type->visible();
    stream << type->opacity();
    return stream;
}

QDataStream &operator>>(QDataStream &stream, std::unique_ptr<Union::Properties::DisplayPropertyGroup> &type)
{
    {
        std::optional<bool> data;
        stream >> data;
        type->setVisible(data);
    }
    {
        std::optional<qreal> data;
        stream >> data;
        type->setOpacity(data);
    }

    return stream;
}