#include "CMWFactoryIOAnalog.h"
#include "ui_CMWFactoryIOAnalog.h"
#include "logic.h"
#include "cctrlbuttonid.h"

CMWFactoryIOAnalog::CMWFactoryIOAnalog(E_MotorDriver_ID eMotorDriver, QWidget* parent)
    : CMWidget(parent)
{
    ui = new Ui::CMWFactoryIOAnalog();
    ui->setupUi(this);
    m_iCurLineCount = -1;
    m_eMotorDriver = eMotorDriver;

    ui->tableWidget_IO->setEditTriggers(QAbstractItemView::DoubleClicked);
    ui->tableWidget_IO->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
    
    ui->tableWidget_IO->setColumnWidth(1, 250);
    ui->tableWidget_IO->setColumnWidth(2, 250);
}

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


void CMWFactoryIOAnalog::MFresh()
{
    MFresh_IOList();
}

void CMWFactoryIOAnalog::MApply()
{
    MApply_IOList();
}

void CMWFactoryIOAnalog::MTimeFresh()
{
    MFresh_IOList();
}

void CMWFactoryIOAnalog::MFresh_IOList()
{
    CIO* pIO = nullptr;
    if (m_eMotorDriver == IO_INPUT_ANALOG)
    {
        pIO = GET_CHDIRECT(IO_INPUT_ANALOG, CIO);
    }
    else
    {
        pIO = GET_CHDIRECT(IO_OUTPUT_ANALOG, CIO);
    }

    int isize = pIO->m_iIOcnt;
    if (isize != m_iCurLineCount)
    {
        m_iCurLineCount = isize;
        ui->tableWidget_IO->clearContents();
        ui->tableWidget_IO->setRowCount(isize);
        for (int i = 0; i < isize; i++)
        {
            CIO::IOPARAM& ioParam = pIO->m_io[i];
            QString sName = QString("%1_%2").arg(m_eMotorDriver == IO_INPUT_ANALOG ? "AIN" : "AOUT").arg(i);

            QTableWidgetItem* pItem = new QTableWidgetItem(sName);
            pItem->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget_IO->setItem(i, 0, pItem);
            pItem = new QTableWidgetItem(ioParam.sDisplayName);
            ui->tableWidget_IO->setItem(i, 1, pItem);
            pItem = new QTableWidgetItem(ioParam.singlename);
            ui->tableWidget_IO->setItem(i, 2, pItem);
            pItem = new QTableWidgetItem(QString::number(ioParam.ioIndex));
            ui->tableWidget_IO->setItem(i, 3, pItem);
            pItem = new QTableWidgetItem(QString::number(ioParam.bReversed));
            ui->tableWidget_IO->setItem(i, 4, pItem);

			pItem = new QTableWidgetItem(QString::number(ioParam.fanalogOffset));
			ui->tableWidget_IO->setItem(i, 5, pItem);

			pItem = new QTableWidgetItem(QString::number(ioParam.fanalogUnit));
			ui->tableWidget_IO->setItem(i, 6, pItem);

			pItem = new QTableWidgetItem(QString::number(ioParam.fanalogThres));
			ui->tableWidget_IO->setItem(i, 7, pItem);
        }
    }
}

void CMWFactoryIOAnalog::MApply_IOList()
{
    CIO* pIO = nullptr;
    if (m_eMotorDriver == IO_INPUT_ANALOG)
    {
        pIO = GET_CHDIRECT(IO_INPUT_ANALOG, CIO);
    }
    else
    {
        pIO = GET_CHDIRECT(IO_OUTPUT_ANALOG, CIO);
    }

    int iSize = ui->tableWidget_IO->rowCount();
    for (int i = 0; i < iSize; i++)
    {
        CIO::IOPARAM& ioParam = pIO->m_io[i];
        QTableWidgetItem* pItem = ui->tableWidget_IO->item(i, 1); Q_ASSERT(pItem);
        ioParam.sDisplayName = pItem->text();
        
        pItem = ui->tableWidget_IO->item(i, 2); Q_ASSERT(pItem);
        ioParam.singlename = pItem->text();

        pItem = ui->tableWidget_IO->item(i, 3); Q_ASSERT(pItem);
        ioParam.ioIndex = pItem->text().toInt();
        pItem = ui->tableWidget_IO->item(i, 4); Q_ASSERT(pItem);
        ioParam.bReversed = pItem->text().toInt() >= 1;

        ioParam.fanalogOffset = ui->tableWidget_IO->item(i, 5)->text().toFloat();
		ioParam.fanalogUnit = ui->tableWidget_IO->item(i, 6)->text().toFloat();
		ioParam.fanalogThres = ui->tableWidget_IO->item(i, 7)->text().toFloat();
    }
}

void CMWFactoryIOAnalog::MInit()
{
    //ui->tableWidget_IO->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //ui->tableWidget_IO->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
}