// This file was generated automatically by Qleany's generator, edit at your own risk! 
// If you do, be careful to not overwrite it when you run the generator again.
#include "{{ single.class_name_snake }}.h"
#include "event_dispatcher.h"
#include "{{ single.entity_name_snake }}/{{ single.entity_name_snake }}_controller.h"

using namespace {{ application_cpp_domain_name }}::Controller;
using namespace {{ application_cpp_domain_name }}::Presenter;

{{ single.class_name_pascal }}::{{ single.class_name_pascal }}(QObject *parent) : QObject{parent}
{
    connect(EventDispatcher::instance()->{{ single.entity_name_camel }}(), &{{ single.entity_name_pascal }}Signals::removed, this, [this](QList<int> removedIds) {
        if (removedIds.contains(id()))
        {
            resetId();
        }
    });
    connect(EventDispatcher::instance()->{{ single.entity_name_camel }}(), &{{ single.entity_name_pascal }}Signals::updated, this, [this]({{ single.entity_name_pascal }}DTO dto) {
        if (dto.id() == id())
        {
        {% for field in single.fields %}
        if (m_{{ field.name_camel }} != dto.{{ field.name_camel }}()){
            m_{{ field.name_camel }} = dto.{{ field.name_camel }}();
            emit {{ field.name_camel }}Changed();
            }
        {%- endfor %}

        }
    });
}

int {{ single.class_name_pascal }}::id() const
{
    return m_id;
}

void {{ single.class_name_pascal }}::setId(int newId)
{
    if (m_id == newId)
        return;
    m_id = newId;
    emit idChanged();

    // clear
    if (m_id == 0)
    {
        {% for field in single.fields %}
        {% if field.name_camel != "id" %}
            m_{{ field.name_camel }} = {{ field.type }}{};
            emit {{ field.name_camel }}Changed();
        {% endif %}
        {%- endfor %}
    }

    // set
    else
    {
        {{ single.entity_name_pascal }}::{{ single.entity_name_pascal }}Controller::instance()->get(m_id).then(
            [this](const {{ application_cpp_domain_name }}::Contracts::DTO::{{ single.entity_name_pascal }}::{{ single.entity_name_pascal }}DTO &{{ single.entity_name_camel }}) { 
                if({{ single.entity_name_camel }}.isInvalid()){
                    qCritical() << Q_FUNC_INFO << "Invalid {{ single.entity_name_camel }}Id";
                    return;
                }
                {% for field in single.fields %}
                {% if field.name_camel != "id" %}
                    m_{{ field.name_camel }} = {{ single.entity_name_camel }}.{{ field.name_camel }}();
                    emit {{ field.name_camel }}Changed();
                {% endif %}
                {%- endfor %}
                });
    }
}

void {{ single.class_name_pascal }}::resetId()
{
    setId(0);
}

{% for field in single.fields %}
{% if field.name_camel != "id" %}
{{ field.type }} {{ single.class_name_pascal }}::{{ field.name_camel }}() const
{
    return m_{{ field.name_camel }};
}

{% if not single.read_only -%}
void {{ single.class_name_pascal }}::set{{ field.name_pascal }}({% if field.type in ['int', 'double', 'float', 'bool'] %}{{ field.type }} new{{ field.name_pascal }}{% else %} const {{ field.type }} &new{{ field.name_pascal }}{% endif %})
{
    if (m_{{ field.name_camel }} == new{{ field.name_pascal }})
        return;

    Update{{ single.entity_name_pascal }}DTO dto;
    dto.setId(id());
    dto.set{{ field.name_pascal }}(new{{ field.name_pascal }});
    {{ single.entity_name_pascal }}::{{ single.entity_name_pascal }}Controller::instance()->update(dto).then(
            [this](const {{ application_cpp_domain_name }}::Contracts::DTO::{{ single.entity_name_pascal }}::{{ single.entity_name_pascal }}DTO &{{ single.entity_name_camel }}) {   
                if({{ single.entity_name_camel }}.isInvalid()){
                    qCritical() << Q_FUNC_INFO << "Invalid {{ single.entity_name_camel }}Id";
                    return;
                }
                m_{{ field.name_camel }} = {{ single.entity_name_camel }}.{{ field.name_camel }}();
                emit {{ field.name_camel }}Changed();
            });
}
{% endif %}
{% endif %}
{%- endfor %}
