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

#include <QRegularExpression>

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

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

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

TextProperty::TextProperty()
    : d(std::make_unique<TextPropertyPrivate>())
{
}

TextProperty::TextProperty(const TextProperty &other)
    : d(std::make_unique<TextPropertyPrivate>())
{
    d->alignment = std::make_unique<AlignmentProperty>();
    *(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;
}

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

TextProperty::~TextProperty() = default;

TextProperty &TextProperty::operator=(const TextProperty &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;
}

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

AlignmentProperty *TextProperty::alignment() const
{
    return d->alignment.get();
}

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

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

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

    d->font = newValue;
}

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

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

    d->color = newValue;
}

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

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

    d->wrapMode = newValue;
}

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

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

    d->elide = newValue;
}

bool TextProperty::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 TextProperty::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 TextProperty::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 << "TextProperty(" << 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 TextProperty::resolveProperties(const TextProperty *source, TextProperty *destination)
{
    if (!source || !destination) {
        return;
    }

    if (source->d->alignment) {
        if (!destination->d->alignment) {
            destination->d->alignment = std::make_unique<AlignmentProperty>();
        }
        AlignmentProperty::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<TextProperty> TextProperty::empty()
{
    auto result = std::make_unique<TextProperty>();
    result->d->alignment = AlignmentProperty::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 TextProperty &left, const TextProperty &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::TextProperty *type)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << qPrintable(type->toString(0, ToStringFlag::Types));
    return debug;
}

QDataStream &operator<<(QDataStream &stream, const Union::Properties::TextProperty *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::TextProperty> &type)
{
    {
        bool hasData;
        stream >> hasData;

        if (hasData) {
            auto data = std::make_unique<AlignmentProperty>();
            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;
}