{#
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 is the template that is used to generate implementations for QML grouped properties.

REUSE-IgnoreStart
#}
// 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 qml_group.cpp.j2.
// To regenerate, run `tools/propertygenerator/generate_properties.py`.
{# REUSE-IgnoreEnd #}

{% set group_name = type_name + "Group" %}
#include "{{ group_name }}.h"

#include <QQmlEngine>

#include "QuickStyle.h"

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

{{ group_name }}::{{ group_name }}(QuickStyle *style)
    : QObject()
    , m_style(style)
{
{% for property in properties %}
{% if property.is_group %}
    m_{{ property.name }} = std::make_unique<{{ property.type }}Group>(m_style);
{% endif %}
{% endfor %}
}

void {{ group_name }}::update({{ type_name }} *newState)
{
    if (newState == m_state) {
        return;
    }

    m_state = newState;

    if (!newState) {
{% for property in properties %}
    {% if property.is_group %}
        m_{{ property.name }}->update(nullptr);
    {% endif %}
{% endfor %}
    } else {
{% for property in properties %}
    {% if property.is_group %}
        m_{{ property.name }}->update(newState->{{ property.name }}());
    {% endif %}
{% endfor %}
    }

{% for property in properties %}
{% if not property.is_group %}
    Q_EMIT {{ property.name }}Changed();
{% endif %}
{% endfor %}
{% if extra_code and "update" in extra_code %}
{{ extra_code["update"] | render }}
{% endif %}
    Q_EMIT updated();
}

void {{ group_name }}::refreshColors()
{
{% for property in properties %}
{% if property.is_group %}
    m_{{ property.name }}->refreshColors();
{% elif property.type == "Union::Color" %}
    Q_EMIT {{ property.name }}Changed();
{% endif %}
{% endfor %}
}

{% for property in properties %}
{% if property.is_group %}
{{ property.type }}Group *{{ group_name }}::{{ property.name }}() const
{
    return m_{{ property.name }}.get();
}

{% else %}
QJSValue {{ group_name }}::{{ property.name }}() const
{
    if (!m_state) {
        return QJSValue(QJSValue::UndefinedValue);
    }

    auto value = m_state->{{ property.name }}();
    if (value) {
{% if property.type == "Union::Color" %}
        return m_style->engine()->toScriptValue(value.value().toQColor());
{% else %}
        return m_style->engine()->toScriptValue(value.value());
{% endif %}
    }

    return QJSValue(QJSValue::UndefinedValue);
}

{% endif %}
{% endfor %}
{% if extra_code and "global" in extra_code %}
{{ extra_code["global"] | render }}

{% endif %}
#include "moc_{{ type_name }}Group.cpp"
