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

#include <QRegularExpression>

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

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

class Union::Properties::BorderPropertyGroupPrivate
{
public:
    std::unique_ptr<LinePropertyGroup> left;
    std::unique_ptr<LinePropertyGroup> right;
    std::unique_ptr<LinePropertyGroup> top;
    std::unique_ptr<LinePropertyGroup> bottom;
};

BorderPropertyGroup::BorderPropertyGroup()
    : d(std::make_unique<BorderPropertyGroupPrivate>())
{
}

BorderPropertyGroup::BorderPropertyGroup(const BorderPropertyGroup &other)
    : d(std::make_unique<BorderPropertyGroupPrivate>())
{
    d->left = std::make_unique<LinePropertyGroup>();
    *(d->left) = *(other.d->left);
    d->right = std::make_unique<LinePropertyGroup>();
    *(d->right) = *(other.d->right);
    d->top = std::make_unique<LinePropertyGroup>();
    *(d->top) = *(other.d->top);
    d->bottom = std::make_unique<LinePropertyGroup>();
    *(d->bottom) = *(other.d->bottom);
}

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

BorderPropertyGroup::~BorderPropertyGroup() = default;

BorderPropertyGroup &BorderPropertyGroup::operator=(const BorderPropertyGroup &other)
{
    if (this != &other) {
        *(d->left) = *(other.d->left);
        *(d->right) = *(other.d->right);
        *(d->top) = *(other.d->top);
        *(d->bottom) = *(other.d->bottom);
    }
    return *this;
}

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

LinePropertyGroup *BorderPropertyGroup::left() const
{
    return d->left.get();
}

void BorderPropertyGroup::setLeft(std::unique_ptr<LinePropertyGroup> &&newValue)
{
    d->left = std::move(newValue);
}

LinePropertyGroup *BorderPropertyGroup::right() const
{
    return d->right.get();
}

void BorderPropertyGroup::setRight(std::unique_ptr<LinePropertyGroup> &&newValue)
{
    d->right = std::move(newValue);
}

LinePropertyGroup *BorderPropertyGroup::top() const
{
    return d->top.get();
}

void BorderPropertyGroup::setTop(std::unique_ptr<LinePropertyGroup> &&newValue)
{
    d->top = std::move(newValue);
}

LinePropertyGroup *BorderPropertyGroup::bottom() const
{
    return d->bottom.get();
}

void BorderPropertyGroup::setBottom(std::unique_ptr<LinePropertyGroup> &&newValue)
{
    d->bottom = std::move(newValue);
}

bool BorderPropertyGroup::hasAnyValue() const
{
    if (d->left && d->left->hasAnyValue()) {
        return true;
    }
    if (d->right && d->right->hasAnyValue()) {
        return true;
    }
    if (d->top && d->top->hasAnyValue()) {
        return true;
    }
    if (d->bottom && d->bottom->hasAnyValue()) {
        return true;
    }
    return false;
}

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

    if (d->left && !d->left->isEmpty()) {
        return false;
    }
    if (d->right && !d->right->isEmpty()) {
        return false;
    }
    if (d->top && !d->top->isEmpty()) {
        return false;
    }
    if (d->bottom && !d->bottom->isEmpty()) {
        return false;
    }

    return true;
}

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

    out << indent(indentation, multiline, true) << "left: ";
    if (d->left) {
        out << d->left->toString(indentation + 2, flags);
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "right: ";
    if (d->right) {
        out << d->right->toString(indentation + 2, flags);
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "top: ";
    if (d->top) {
        out << d->top->toString(indentation + 2, flags);
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "bottom: ";
    if (d->bottom) {
        out << d->bottom->toString(indentation + 2, flags);
    } else {
        out << empty << maybeNewLine;
    }

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

    out.flush();

    return result;
}

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

    if (source->d->left) {
        if (!destination->d->left) {
            destination->d->left = std::make_unique<LinePropertyGroup>();
        }
        LinePropertyGroup::resolveProperties(source->d->left.get(), destination->d->left.get());
    }
    if (source->d->right) {
        if (!destination->d->right) {
            destination->d->right = std::make_unique<LinePropertyGroup>();
        }
        LinePropertyGroup::resolveProperties(source->d->right.get(), destination->d->right.get());
    }
    if (source->d->top) {
        if (!destination->d->top) {
            destination->d->top = std::make_unique<LinePropertyGroup>();
        }
        LinePropertyGroup::resolveProperties(source->d->top.get(), destination->d->top.get());
    }
    if (source->d->bottom) {
        if (!destination->d->bottom) {
            destination->d->bottom = std::make_unique<LinePropertyGroup>();
        }
        LinePropertyGroup::resolveProperties(source->d->bottom.get(), destination->d->bottom.get());
    }
}

std::unique_ptr<BorderPropertyGroup> BorderPropertyGroup::empty()
{
    auto result = std::make_unique<BorderPropertyGroup>();
    result->d->left = LinePropertyGroup::empty();
    result->d->right = LinePropertyGroup::empty();
    result->d->top = LinePropertyGroup::empty();
    result->d->bottom = LinePropertyGroup::empty();
    return result;
}

QMarginsF BorderPropertyGroup::sizes() const
{
    QMarginsF result;

    if (d->left) {
        result.setLeft(d->left->size().value_or(0.0));
    }
    if (d->right) {
        result.setRight(d->right->size().value_or(0.0));
    }
    if (d->top) {
        result.setTop(d->top->size().value_or(0.0));
    }
    if (d->bottom) {
        result.setBottom(d->bottom->size().value_or(0.0));
    }

    return result;
}

bool Union::Properties::operator==(const BorderPropertyGroup &left, const BorderPropertyGroup &right)
{
    if (left.left() && right.left()) {
        if (*(left.left()) != *(right.left())) {
            return false;
        }
    } else if (left.left() != right.left()) {
        return false;
    }
    if (left.right() && right.right()) {
        if (*(left.right()) != *(right.right())) {
            return false;
        }
    } else if (left.right() != right.right()) {
        return false;
    }
    if (left.top() && right.top()) {
        if (*(left.top()) != *(right.top())) {
            return false;
        }
    } else if (left.top() != right.top()) {
        return false;
    }
    if (left.bottom() && right.bottom()) {
        if (*(left.bottom()) != *(right.bottom())) {
            return false;
        }
    } else if (left.bottom() != right.bottom()) {
        return false;
    }
    return true;
}

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

QDataStream &operator<<(QDataStream &stream, const Union::Properties::BorderPropertyGroup *type)
{
    {
        auto data = type->left();
        stream << bool(data);
        if (data) {
            stream << data;
        }
    }
    {
        auto data = type->right();
        stream << bool(data);
        if (data) {
            stream << data;
        }
    }
    {
        auto data = type->top();
        stream << bool(data);
        if (data) {
            stream << data;
        }
    }
    {
        auto data = type->bottom();
        stream << bool(data);
        if (data) {
            stream << data;
        }
    }
    return stream;
}

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

        if (hasData) {
            auto data = std::make_unique<LinePropertyGroup>();
            stream >> data;
            type->setLeft(std::move(data));
        }
    }
    {
        bool hasData;
        stream >> hasData;

        if (hasData) {
            auto data = std::make_unique<LinePropertyGroup>();
            stream >> data;
            type->setRight(std::move(data));
        }
    }
    {
        bool hasData;
        stream >> hasData;

        if (hasData) {
            auto data = std::make_unique<LinePropertyGroup>();
            stream >> data;
            type->setTop(std::move(data));
        }
    }
    {
        bool hasData;
        stream >> hasData;

        if (hasData) {
            auto data = std::make_unique<LinePropertyGroup>();
            stream >> data;
            type->setBottom(std::move(data));
        }
    }

    return stream;
}