#include "cmwfactoryio.h"
#include "ui_cmwfactoryio.h"
#include "logic.h"
#include "cctrlbuttonid.h"

CMWFactoryIO::CMWFactoryIO(E_MotorDriver_ID eMotorDriver, QWidget* parent)
    : CMWidget(parent)
{
    ui = new Ui::CMWFactoryIO();
    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);
}

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


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

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

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

void CMWFactoryIO::MFresh_IOList()
{
    CIO* pIO = nullptr;
    if (m_eMotorDriver == IO_INPUT)
    {
        pIO = GET_CHDIRECT(IO_INPUT, CIO);
    }
    else
    {
        pIO = GET_CHDIRECT(IO_OUTPUT, 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 ? "INPUT" : "OUTPUT").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);
        }
    }
}

void CMWFactoryIO::MApply_IOList()
{
    CIO* pIO = nullptr;
    if (m_eMotorDriver == IO_INPUT)
    {
        pIO = GET_CHDIRECT(IO_INPUT, CIO);
    }
    else
    {
        pIO = GET_CHDIRECT(IO_OUTPUT, 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;
    }
}

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