#include "WorkNodeFormModel.h"

#include <QQmlEngine>

WorkNodeFormModel* WorkNodeFormModel::pInstance = nullptr;

WorkNodeFormModel::WorkNodeFormModel(QObject *parent)
    : QAbstractListModel(parent)
{

}

WorkNodeFormModel *WorkNodeFormModel::instance()
{
    if (!pInstance) {
        pInstance = new WorkNodeFormModel(nullptr);
    }

    return pInstance;
}

WorkNodeFormModel *WorkNodeFormModel::create(QQmlEngine *engine, QJSEngine *)
{
    engine->setObjectOwnership(instance(), QQmlEngine::CppOwnership);
    return instance();
}

void WorkNodeFormModel::append(const FormControl &form)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_formList.append(form);
    endInsertRows();
}

void WorkNodeFormModel::clear()
{
    beginResetModel();
    m_formList.clear();
    endResetModel();
}

QString WorkNodeFormModel::getType(int index)
{
    return m_formList[index].m_type;
}

int WorkNodeFormModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        return 0;
    }
    return m_formList.count();
}

QHash<int, QByteArray> WorkNodeFormModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[Id] = "id";
    roles[Label] = "label";
    roles[Name] = "name";
    roles[Options] = "options";
    roles[Placeholder] = "placeholder";
    roles[Required] = "required";
    roles[RequiredMessage] = "requiredMessage";
    roles[Type] = "type";
    roles[Value] = "value";
    roles[GridColumns] = "gridColumns";
    roles[GroupName] = "gridGroupName";
    roles[GridCount] = "gridCount";
    roles[GridIndex] = "groupIndex";
    roles[TextAreaHeight] = "showTextAreaHeight";

    return roles;
}

QVariant WorkNodeFormModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() > m_formList.count()) {
        return QVariant();
    }

    const FormControl& formControl = m_formList[index.row()];
    if (role == Id) {
        return formControl.m_id;
    }
    else if (role == Label) {
        return formControl.m_label;
    }
    else if (role == Name) {
        return formControl.m_name;
    }
    else if (role == Options) {
        return formControl.m_options;
    }
    else if (role == Placeholder) {
        return formControl.m_placeholder;
    }
    else if (role == Required) {
        return formControl.m_required;
    }
    else if (role == RequiredMessage) {
        return formControl.m_requiredMsg;
    }
    else if (role == Type) {
        return formControl.m_type;
    }
    else if (role == Value) {
        return formControl.m_value;
    }
    else if (role == GroupName) {
        return formControl.m_gridGroupName;
    }
    else if (role == GridColumns) {
        return formControl.m_gridColumns;
    }
    else if (role == GridCount) {
        return formControl.m_gridCount;
    }
    else if (role == GridIndex) {
        return formControl.m_gridIndex;
    }
    else if (role == TextAreaHeight) {
        return formControl.m_TextAreaHeight;
    }

    return QVariant();
}

