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

#include <QRegularExpression>

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

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

class Union::Properties::CornersPropertyGroupPrivate
{
public:
    std::unique_ptr<CornerPropertyGroup> topLeft;
    std::unique_ptr<CornerPropertyGroup> topRight;
    std::unique_ptr<CornerPropertyGroup> bottomLeft;
    std::unique_ptr<CornerPropertyGroup> bottomRight;
};

CornersPropertyGroup::CornersPropertyGroup()
    : d(std::make_unique<CornersPropertyGroupPrivate>())
{
}

CornersPropertyGroup::CornersPropertyGroup(const CornersPropertyGroup &other)
    : d(std::make_unique<CornersPropertyGroupPrivate>())
{
    d->topLeft = std::make_unique<CornerPropertyGroup>();
    *(d->topLeft) = *(other.d->topLeft);
    d->topRight = std::make_unique<CornerPropertyGroup>();
    *(d->topRight) = *(other.d->topRight);
    d->bottomLeft = std::make_unique<CornerPropertyGroup>();
    *(d->bottomLeft) = *(other.d->bottomLeft);
    d->bottomRight = std::make_unique<CornerPropertyGroup>();
    *(d->bottomRight) = *(other.d->bottomRight);
}

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

CornersPropertyGroup::~CornersPropertyGroup() = default;

CornersPropertyGroup &CornersPropertyGroup::operator=(const CornersPropertyGroup &other)
{
    if (this != &other) {
        *(d->topLeft) = *(other.d->topLeft);
        *(d->topRight) = *(other.d->topRight);
        *(d->bottomLeft) = *(other.d->bottomLeft);
        *(d->bottomRight) = *(other.d->bottomRight);
    }
    return *this;
}

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

CornerPropertyGroup *CornersPropertyGroup::topLeft() const
{
    return d->topLeft.get();
}

void CornersPropertyGroup::setTopLeft(std::unique_ptr<CornerPropertyGroup> &&newValue)
{
    d->topLeft = std::move(newValue);
}

CornerPropertyGroup *CornersPropertyGroup::topRight() const
{
    return d->topRight.get();
}

void CornersPropertyGroup::setTopRight(std::unique_ptr<CornerPropertyGroup> &&newValue)
{
    d->topRight = std::move(newValue);
}

CornerPropertyGroup *CornersPropertyGroup::bottomLeft() const
{
    return d->bottomLeft.get();
}

void CornersPropertyGroup::setBottomLeft(std::unique_ptr<CornerPropertyGroup> &&newValue)
{
    d->bottomLeft = std::move(newValue);
}

CornerPropertyGroup *CornersPropertyGroup::bottomRight() const
{
    return d->bottomRight.get();
}

void CornersPropertyGroup::setBottomRight(std::unique_ptr<CornerPropertyGroup> &&newValue)
{
    d->bottomRight = std::move(newValue);
}

bool CornersPropertyGroup::hasAnyValue() const
{
    if (d->topLeft && d->topLeft->hasAnyValue()) {
        return true;
    }
    if (d->topRight && d->topRight->hasAnyValue()) {
        return true;
    }
    if (d->bottomLeft && d->bottomLeft->hasAnyValue()) {
        return true;
    }
    if (d->bottomRight && d->bottomRight->hasAnyValue()) {
        return true;
    }
    return false;
}

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

    if (d->topLeft && !d->topLeft->isEmpty()) {
        return false;
    }
    if (d->topRight && !d->topRight->isEmpty()) {
        return false;
    }
    if (d->bottomLeft && !d->bottomLeft->isEmpty()) {
        return false;
    }
    if (d->bottomRight && !d->bottomRight->isEmpty()) {
        return false;
    }

    return true;
}

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

    out << indent(indentation, multiline, true) << "topLeft: ";
    if (d->topLeft) {
        out << d->topLeft->toString(indentation + 2, flags);
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "topRight: ";
    if (d->topRight) {
        out << d->topRight->toString(indentation + 2, flags);
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "bottomLeft: ";
    if (d->bottomLeft) {
        out << d->bottomLeft->toString(indentation + 2, flags);
    } else {
        out << empty << maybeNewLine;
    }
    out << indent(indentation, multiline, false) << "bottomRight: ";
    if (d->bottomRight) {
        out << d->bottomRight->toString(indentation + 2, flags);
    } else {
        out << empty << maybeNewLine;
    }

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

    out.flush();

    return result;
}

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

    if (source->d->topLeft) {
        if (!destination->d->topLeft) {
            destination->d->topLeft = std::make_unique<CornerPropertyGroup>();
        }
        CornerPropertyGroup::resolveProperties(source->d->topLeft.get(), destination->d->topLeft.get());
    }
    if (source->d->topRight) {
        if (!destination->d->topRight) {
            destination->d->topRight = std::make_unique<CornerPropertyGroup>();
        }
        CornerPropertyGroup::resolveProperties(source->d->topRight.get(), destination->d->topRight.get());
    }
    if (source->d->bottomLeft) {
        if (!destination->d->bottomLeft) {
            destination->d->bottomLeft = std::make_unique<CornerPropertyGroup>();
        }
        CornerPropertyGroup::resolveProperties(source->d->bottomLeft.get(), destination->d->bottomLeft.get());
    }
    if (source->d->bottomRight) {
        if (!destination->d->bottomRight) {
            destination->d->bottomRight = std::make_unique<CornerPropertyGroup>();
        }
        CornerPropertyGroup::resolveProperties(source->d->bottomRight.get(), destination->d->bottomRight.get());
    }
}

std::unique_ptr<CornersPropertyGroup> CornersPropertyGroup::empty()
{
    auto result = std::make_unique<CornersPropertyGroup>();
    result->d->topLeft = CornerPropertyGroup::empty();
    result->d->topRight = CornerPropertyGroup::empty();
    result->d->bottomLeft = CornerPropertyGroup::empty();
    result->d->bottomRight = CornerPropertyGroup::empty();
    return result;
}

CornersPropertyGroup::CornerRadii CornersPropertyGroup::radii() const
{
    CornerRadii result;

    if (d->topLeft) {
        result.topLeft = d->topLeft->radius().value_or(0.0);
    }
    if (d->topRight) {
        result.topRight = d->topRight->radius().value_or(0.0);
    }
    if (d->bottomLeft) {
        result.bottomLeft = d->bottomLeft->radius().value_or(0.0);
    }
    if (d->bottomRight) {
        result.bottomRight = d->bottomRight->radius().value_or(0.0);
    }

    return result;
}

bool Union::Properties::operator==(const CornersPropertyGroup &left, const CornersPropertyGroup &right)
{
    if (left.topLeft() && right.topLeft()) {
        if (*(left.topLeft()) != *(right.topLeft())) {
            return false;
        }
    } else if (left.topLeft() != right.topLeft()) {
        return false;
    }
    if (left.topRight() && right.topRight()) {
        if (*(left.topRight()) != *(right.topRight())) {
            return false;
        }
    } else if (left.topRight() != right.topRight()) {
        return false;
    }
    if (left.bottomLeft() && right.bottomLeft()) {
        if (*(left.bottomLeft()) != *(right.bottomLeft())) {
            return false;
        }
    } else if (left.bottomLeft() != right.bottomLeft()) {
        return false;
    }
    if (left.bottomRight() && right.bottomRight()) {
        if (*(left.bottomRight()) != *(right.bottomRight())) {
            return false;
        }
    } else if (left.bottomRight() != right.bottomRight()) {
        return false;
    }
    return true;
}

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

QDataStream &operator<<(QDataStream &stream, const Union::Properties::CornersPropertyGroup *type)
{
    {
        auto data = type->topLeft();
        stream << bool(data);
        if (data) {
            stream << data;
        }
    }
    {
        auto data = type->topRight();
        stream << bool(data);
        if (data) {
            stream << data;
        }
    }
    {
        auto data = type->bottomLeft();
        stream << bool(data);
        if (data) {
            stream << data;
        }
    }
    {
        auto data = type->bottomRight();
        stream << bool(data);
        if (data) {
            stream << data;
        }
    }
    return stream;
}

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

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

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

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

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

    return stream;
}