#include "imetadataplugin.h"

#include <QJsonValue>
#include <QJsonArray>
#include <QLocale>
#include <QDebug>

#include "igridform.h"

IMetaDataPlugin::IMetaDataPlugin()
{
    m_service = NULL;
    m_ui = NULL;
}

IMetaDataPlugin::~IMetaDataPlugin()
{
}

QString IMetaDataPlugin::pluginName()
{
    return m_name;
}

QString IMetaDataPlugin::pluginId()
{
    return m_id;
}

QString IMetaDataPlugin::pluginDescription()
{
    return m_description;
}

int IMetaDataPlugin::schemaVersion()
{
    return m_schemaVersion;
}

QStringList IMetaDataPlugin::schemas()
{
    return m_schemas;
}

QStringList IMetaDataPlugin::dependsOn()
{
    return m_dependsOn;
}

void IMetaDataPlugin::init(const QJsonObject &metaData)
{
    parseMetaData(metaData);
    initServiceUi();

    if (IGridForm *pluginUi = dynamic_cast<IGridForm*>(m_ui))
    {
        pluginUi->setPluginId(pluginId());
    }
}

void IMetaDataPlugin::parseMetaData(const QJsonObject &metaData)
{
    qDebug() << metaData;
    QJsonValue data = metaData["MetaData"];
    if (!data.isObject()) {
        return;
    }

    m_name = parseLocaleText(data.toObject()["name"].toObject());
    m_description = parseLocaleText(data.toObject()["description"].toObject());
    m_id = data.toObject()["id"].toString();
    m_schemaVersion = data.toObject()["schemaVersion"].toInt();

    foreach (QJsonValue schVal, data.toObject()["sql"].toArray()) {
        m_schemas.append(schVal.toString());
    }

    foreach (QJsonValue depVal, data.toObject()["dependecies"].toArray()) {
        m_dependsOn.append(depVal.toString());
    }
}

QString IMetaDataPlugin::parseLocaleText(const QJsonObject &object)
{
    QString locale = QLocale::system().name();
    QString ret;

    foreach (QString key, object.keys()) {
        if (locale.contains(key, Qt::CaseInsensitive)) {
            ret = object[key].toString();
            break;
        }
    }

    if (ret.isEmpty()) {
        ret = object["default"].toString();
    }

    return ret;
}

