#include "configparamsgetter.h"

//bool BmsSettingsWidget::addParamRow(QLineEdit* edit, ConfigParams *params, QString paramName)


bool getSerialNumber(QLabel* lab, ConfigParams *params, QString paramName)
{
    // (*) serial number is inside a DOUBLE value inside the BMS
    // (**) But we need only the INT part of that double value
    bool res = false;
    QWidget *editor = params->getEditor(paramName);
    QString name = params->getLongName(paramName);
    if (editor && !name.isEmpty())
    {
        if(editor->property("type").toInt() == CFG_T_DOUBLE) // (*)
        {
            auto val = params->getParamDouble(paramName);
            quint64 valUint64 = quint64(val); // (**)
            lab->setText( QString::number(valUint64) );
            res = true;
        }
    }
    return res;
}

bool getSerialNumber(QLineEdit* edit, ConfigParams *params, QString paramName)
{
    // (*) serial number is inside a DOUBLE value inside the BMS
    // (**) But we need only the INT part of that double value
    bool res = false;
    QWidget *editor = params->getEditor(paramName);
    QString name = params->getLongName(paramName);
    if (editor && !name.isEmpty())
    {
        if(editor->property("type").toInt() == CFG_T_DOUBLE) // (*)
        {
            auto val = params->getParamDouble(paramName);
            quint64 valUint64 = quint64(val); // (**)
            edit->setText( QString::number(valUint64) );
            res = true;
        }
    }
    return res;
}

bool addParamRow(QLineEdit* edit, ConfigParams *params, QString paramName)
{
    bool res = false;
    QWidget *editor = params->getEditor(paramName);
    //ConfigParam &p = params[paramName];
    //ConfigParam &p = mParams[name];

    QString name = params->getLongName(paramName);

    if (editor && !name.isEmpty())
    {
        //int row = rowCount();
        //setRowCount(row + 1);
        //QTableWidgetItem *item = new QTableWidgetItem(name);
        //item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        //setItem(row, 0, item);
        //setCellWidget(row, 1, editor);
        //edit->setText(editor->);

        auto vtype = editor->property("type");
        auto type = vtype.toInt();
        //CFG_T_UNDEFINED = 0,

        switch (type)
        {
            case CFG_T_INT:
            case CFG_T_ENUM:
            {
                auto val = params->getParamInt(paramName);
                edit->setText(QString::number(val));
                break;
            }
            case CFG_T_DOUBLE:
            {
                auto val = params->getParamDouble(paramName);
                edit->setText(QString::number(val));
                break;
            }
            case CFG_T_QSTRING:
            {
                auto val = params->getParamQString(paramName);
                edit->setText(val);
                break;
            }
            case CFG_T_BOOL:
            {
                auto val = params->getParamBool(paramName);
                edit->setText(val ? "true" : "false");
                break;
            }
            default:
            {
                auto val = editor->property("value");
                auto valString = val.toString();
                edit->setText(valString.isEmpty() ? "Erro Read Value" : valString);
                break;
            }
        }

        auto vsuffix = editor->property("suffix");
        auto suffix = vsuffix.toString();
        auto veditorDecimalsDouble = editor->property("editorDecimalsDouble");
        auto editorDecimalsDouble = veditorDecimalsDouble.toDouble();
        auto veditAsPercentage = editor->property("editAsPercentage");
        auto editAsPercentage = veditAsPercentage.toBool();

        res = true;
        //resizeColumnToContents(0);
        //resizeRowsToContents();
    }

    return res;
}



bool addParamRowEx(QLineEdit* edit, ConfigParams *params, QString paramName)
{
    bool res = false;
    QWidget *editor = params->getEditor(paramName);
    QString name = params->getLongName(paramName);

    if (editor && !name.isEmpty()) {
        //int row = rowCount();
        //setRowCount(row + 1);
        //QTableWidgetItem *item = new QTableWidgetItem(name);
        //item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        //setItem(row, 0, item);
        //setCellWidget(row, 1, editor);
        //edit->setText(editor->);

        auto vtype = editor->property("type");
        auto type = vtype.toInt();
        auto vval = editor->property("value");
        //CFG_T_UNDEFINED = 0,

        switch(type)
        {
        case(CFG_T_INT):
        case(CFG_T_ENUM):
        {
            auto val = vval.toInt();
            edit->setText(QString::number(val));
            break;
        }
        case(CFG_T_DOUBLE):
        {
            auto val = vval.toDouble();
            edit->setText(QString::number(val));
            break;
        }
        case(CFG_T_QSTRING):
        {
            auto val = vval.toString();
            edit->setText(val);
            break;
        }
        case(CFG_T_BOOL):
        {
            auto val = vval.toBool();
            edit->setText(val ? "true" : "false");
            break;
        }
        default:
            auto valString = vval.toString();
            edit->setText(valString.isEmpty() ? "Erro Read Value" : valString);
            break;
        }

        auto vsuffix = editor->property("suffix");
        auto suffix = vsuffix.toString();
        auto veditorDecimalsDouble = editor->property("editorDecimalsDouble");
        auto editorDecimalsDouble = veditorDecimalsDouble.toDouble();
        auto veditAsPercentage = editor->property("editAsPercentage");
        auto editAsPercentage = veditAsPercentage.toBool();

        res = true;
        //resizeColumnToContents(0);
        //resizeRowsToContents();
    }

    return res;
}

