// 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 "CornerPropertyGroup.h"

#include <QRegularExpression>

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

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

class Union::Properties::CornerPropertyGroupPrivate
{
public:
    std::optional<qreal> radius;
};

CornerPropertyGroup::CornerPropertyGroup()
    : d(std::make_unique<CornerPropertyGroupPrivate>())
{
}

CornerPropertyGroup::CornerPropertyGroup(const CornerPropertyGroup &other)
    : d(std::make_unique<CornerPropertyGroupPrivate>())
{
    d->radius = other.d->radius;
}

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

CornerPropertyGroup::~CornerPropertyGroup() = default;

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

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

std::optional<qreal> CornerPropertyGroup::radius() const
{
    return d->radius;
}

void CornerPropertyGroup::setRadius(const std::optional<qreal> &newValue)
{
    if (newValue == d->radius) {
        return;
    }

    d->radius = newValue;
}

bool CornerPropertyGroup::hasAnyValue() const
{
    if (d->radius.has_value()) {
        return true;
    }
    return false;
}

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

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

    return true;
}

QString CornerPropertyGroup::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 << "CornerPropertyGroup(" << maybeNewLine;
    } else if (indentation > 0) {
        out << maybeNewLine;
    }

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

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

    out.flush();

    return result;
}

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

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

std::unique_ptr<CornerPropertyGroup> CornerPropertyGroup::empty()
{
    auto result = std::make_unique<CornerPropertyGroup>();
    result->d->radius = emptyValue<qreal>();
    return result;
}

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

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

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

QDataStream &operator>>(QDataStream &stream, std::unique_ptr<Union::Properties::CornerPropertyGroup> &type)
{
    {
        std::optional<qreal> data;
        stream >> data;
        type->setRadius(data);
    }

    return stream;
}