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

#include <QRegularExpression>

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

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

class Union::Properties::TextPropertyGroupPrivate
{
public:
    std::unique_ptr<AlignmentPropertyGroup> alignment;
    std::optional<QFont> font;
    std::optional<Union::Color> color;
    std::optional<Union::Properties::TextWrapMode> wrapMode;
    std::optional<Union::Properties::TextElide> elide;
};

TextPropertyGroup::TextPropertyGroup()
    : d(std::make_unique<TextPropertyGroupPrivate>())
{
}

TextPropertyGroup::TextPropertyGroup(const TextPropertyGroup &other)
    : d(std::make_unique<TextPropertyGroupPrivate>())
{
    d->alignment = std::make_unique<AlignmentPropertyGroup>();
    *(d->alignment) = *(other.d->alignment);
    d->font = other.d->font;
    d->color = other.d->color;
    d->wrapMode = other.d->wrapMode;
    d->elide = other.d->elide;
}

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

TextPropertyGroup::~TextPropertyGroup() = default;

TextPropertyGroup &TextPropertyGroup::operator=(const TextPropertyGroup &other)
{
    if (this != &other) {
        *(d->alignment) = *(other.d->alignment);
        d->font = other.d->font;
        d->color = other.d->color;
        d->wrapMode = other.d->wrapMode;
        d->elide = other.d->elide;
    }
    return *this;
}

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

AlignmentPropertyGroup *TextPropertyGroup::alignment() const
{
    return d->alignment.get();
}

void TextPropertyGroup::setAlignment(std::unique_ptr<AlignmentPropertyGroup> &&newValue)
{
    d->alignment = std::move(newValue);
}

std::optional<QFont> TextPropertyGroup::font() const
{
    return d->font;
}

void TextPropertyGroup::setFont(const std::optional<QFont> &newValue)
{
    if (newValue == d->font) {
        return;
    }

    d->font = newValue;
}

std::optional<Union::Color> TextPropertyGroup::color() const
{
    return d->color;
}

void TextPropertyGroup::setColor(const std::optional<Union::Color> &newValue)
{
    if (newValue == d->color) {
        return;
    }

    d->color = newValue;
}

std::optional<Union::Properties::TextWrapMode> TextPropertyGroup::wrapMode() const
{
    return d->wrapMode;
}

void TextPropertyGroup::setWrapMode(const std::optional<Union::Properties::TextWrapMode> &newValue)
{
    if (newValue == d->wrapMode) {
        return;
    }

    d->wrapMode = newValue;
}

std::optional<Union::Properties::TextElide> TextPropertyGroup::elide() const
{
    return d->elide;
}

void TextPropertyGroup::setElide(const std::optional<Union::Properties::TextElide> &newValue)
{
    if (newValue == d->elide) {
        return;
    }

    d->elide = newValue;
}

bool TextPropertyGroup::hasAnyValue() const
{
    if (d->alignment && d->alignment->hasAnyValue()) {
        return true;
    }
    if (d->font.has_value()) {
        return true;
    }
    if (d->color.has_value()) {
        return true;
    }
    if (d->wrapMode.has_value()) {
        return true;
    }
    if (d->elide.has_value()) {
        return true;
    }
    return false;
}

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

    if (d->alignment && !d->alignment->isEmpty()) {
        return false;
    }
    if (d->font.has_value() && d->font.value() != emptyValue<QFont>()) {
        return false;
    }
    if (d->color.has_value() && d->color.value() != emptyValue<Union::Color>()) {
        return false;
    }
    if (d->wrapMode.has_value() && d->wrapMode.value() != emptyValue<Union::Properties::TextWrapMode>()) {
        return false;
    }
    if (d->elide.has_value() && d->elide.value() != emptyValue<Union::Properties::TextElide>()) {
        return false;
    }

    return true;
}

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

    out << indent(indentation, multiline, true) << "alignment: ";
    if (d->alignment) {
        out << d->alignment->toString(indentation + 2, flags);
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "font: ";
    if (d->font) {
        out << d->font->toString() << maybeNewLine;
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "color: ";
    if (d->color) {
        out << d->color->toString() << maybeNewLine;
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "wrapMode: ";
    if (d->wrapMode) {
        out << d->wrapMode.value() << maybeNewLine;
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "elide: ";
    if (d->elide) {
        out << d->elide.value() << maybeNewLine;
    } else {
        out << empty << maybeNewLine;
    }

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

    out.flush();

    return result;
}

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

    if (source->d->alignment) {
        if (!destination->d->alignment) {
            destination->d->alignment = std::make_unique<AlignmentPropertyGroup>();
        }
        AlignmentPropertyGroup::resolveProperties(source->d->alignment.get(), destination->d->alignment.get());
    }
    if (!destination->d->font.has_value()) {
        destination->d->font = source->d->font;
    }
    if (!destination->d->color.has_value()) {
        destination->d->color = source->d->color;
    }
    if (!destination->d->wrapMode.has_value()) {
        destination->d->wrapMode = source->d->wrapMode;
    }
    if (!destination->d->elide.has_value()) {
        destination->d->elide = source->d->elide;
    }
}

std::unique_ptr<TextPropertyGroup> TextPropertyGroup::empty()
{
    auto result = std::make_unique<TextPropertyGroup>();
    result->d->alignment = AlignmentPropertyGroup::empty();
    result->d->font = emptyValue<QFont>();
    result->d->color = emptyValue<Union::Color>();
    result->d->wrapMode = emptyValue<Union::Properties::TextWrapMode>();
    result->d->elide = emptyValue<Union::Properties::TextElide>();
    return result;
}

bool Union::Properties::operator==(const TextPropertyGroup &left, const TextPropertyGroup &right)
{
    if (left.alignment() && right.alignment()) {
        if (*(left.alignment()) != *(right.alignment())) {
            return false;
        }
    } else if (left.alignment() != right.alignment()) {
        return false;
    }
    if (left.font() != right.font()) {
        return false;
    }
    if (left.color() != right.color()) {
        return false;
    }
    if (left.wrapMode() != right.wrapMode()) {
        return false;
    }
    if (left.elide() != right.elide()) {
        return false;
    }
    return true;
}

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

QDataStream &operator<<(QDataStream &stream, const Union::Properties::TextPropertyGroup *type)
{
    {
        auto data = type->alignment();
        stream << bool(data);
        if (data) {
            stream << data;
        }
    }
    stream << type->font();
    stream << type->color();
    stream << type->wrapMode();
    stream << type->elide();
    return stream;
}

QDataStream &operator>>(QDataStream &stream, std::unique_ptr<Union::Properties::TextPropertyGroup> &type)
{
    {
        bool hasData;
        stream >> hasData;

        if (hasData) {
            auto data = std::make_unique<AlignmentPropertyGroup>();
            stream >> data;
            type->setAlignment(std::move(data));
        }
    }
    {
        std::optional<QFont> data;
        stream >> data;
        type->setFont(data);
    }
    {
        std::optional<Union::Color> data;
        stream >> data;
        type->setColor(data);
    }
    {
        std::optional<Union::Properties::TextWrapMode> data;
        stream >> data;
        type->setWrapMode(data);
    }
    {
        std::optional<Union::Properties::TextElide> data;
        stream >> data;
        type->setElide(data);
    }

    return stream;
}