#include "grifosettingsform.h"
#include "ui_grifosettingsform.h"

#include "icd_g346.h"

#define DEG_LSB (ICD1553_SEMICIRCLE_RAD_LSB*GC_K_RAD_TO_DEG)

struct param_coding_info_t
{
    unsigned int id;
    const char* name;
    const char* units;
    float lsb;

    int itemIndex;
};

static param_coding_info_t param_coding_info[]=
{
    {1, "ANT_YAW", "deg", DEG_LSB},
    {2, "ANT_PITCH", "deg", DEG_LSB},
    {3, "ANT_ROLL", "deg", DEG_LSB},
};

static const param_coding_info_t* coding_find(unsigned int id)
{
    for(unsigned int i=0; i<((sizeof param_coding_info)/(sizeof param_coding_info[0])); ++i)
    {
        if (param_coding_info[i].id==id)
        {
            return &param_coding_info[i];
        }
    }
    return 0;
}

GrifoSettingsForm::GrifoSettingsForm(QWidget *parent) :
    QScrollArea(parent),
    ui(new Ui::GrifoSettingsForm)
{
    ui->setupUi(this);

    int itemIndex=0;
    for(unsigned int i=0; i<((sizeof param_coding_info)/(sizeof param_coding_info[0])); ++i)
    {
        if (param_coding_info[i].id)
        {
            ui->codedParamID->addItem(param_coding_info[i].name, param_coding_info[i].id);
            param_coding_info[i].itemIndex=itemIndex;
            ++itemIndex;
        }
    }

    connect(ui->codedParamID, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [this](int index)
    {
        int i=ui->codedParamID->itemData(index).toInt();
        if (i!=ui->PARAM_ID->value())
            ui->PARAM_ID->setValue(i);
    });

    connect(ui->PARAM_VALUE, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, [this](int v)
    {
        if (ui->doubleSpinBox->isEnabled())
        {
            int index=ui->PARAM_ID->value();
            const param_coding_info_t* dinfo=coding_find(index);
            if (!dinfo)
            {
                ui->doubleSpinBox->setEnabled(false);
            }
            else if (dinfo->lsb)
            {
                float x=(v)*dinfo->lsb;
                ui->doubleSpinBox->setValue(x);
            }
            else
            {
                ui->doubleSpinBox->setEnabled(false);
            }
        }
    });

    connect(ui->doubleSpinBox, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), this, [this](double v)
    {
        int index=ui->PARAM_ID->value();
        {
            const param_coding_info_t* dinfo=coding_find(index);
            if (!dinfo)
            {
                ui->doubleSpinBox->setEnabled(false);
            }
            else if (dinfo->lsb)
            {
                int x=v/dinfo->lsb;
                ui->PARAM_VALUE->setValue(x);
            }
            else
            {
                ui->doubleSpinBox->setEnabled(false);
            }
        }
    });

    connect(ui->PARAM_VALUE_TB, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, [this](int v)
    {
        int index=ui->PARAM_ID_TB->value();
        const param_coding_info_t* dinfo=coding_find(index);
        if (!dinfo)
        {
            //ui->doubleSpinBoxTB->setEnabled(false);
        }
        else if (dinfo->lsb)
        {
            double x=v*dinfo->lsb;
            ui->doubleSpinBoxTB->setValue(x);
        }
        else
        {
            //ui->doubleSpinBoxTB->setEnabled(false);
        }
    });
}

GrifoSettingsForm::~GrifoSettingsForm()
{
    delete ui;
}

void GrifoSettingsForm::on_PARAM_ID_valueChanged(int arg1)
{
    const param_coding_info_t* dinfo=coding_find(arg1);
    if (!dinfo)
        ui->doubleSpinBox->setEnabled(false);
    else
    {
        ui->codedParamID->setCurrentIndex(dinfo->itemIndex);
        if (dinfo->lsb)
        {
            ui->doubleSpinBox->setEnabled(true);
            float v=(ui->PARAM_VALUE->value())*dinfo->lsb;
            ui->doubleSpinBox->setValue(v);
        }
        else
            ui->doubleSpinBox->setEnabled(false);
    }
}
