#include "cmwcompioin.h"
#include "logic.h"
#include <QHBoxLayout>
#include "gui.h"
CMWCompIOIn::CMWCompIOIn(QWidget *parent, int iStartIO, int iEndIO)
    : CMWidget(parent)
{
    m_pGridLayOut=new QGridLayout();
    setLayout(m_pGridLayOut);

    icon_off.load(CMakePath::make_bin_path("../theme/led_off.png"));
    icon_blue.load(CMakePath::make_bin_path("../theme/led_blue.png"));

    icon_off=icon_off.scaledToWidth(30);
    icon_off=icon_off.scaledToHeight(30);
    icon_blue=icon_blue.scaledToWidth(30);
    icon_blue=icon_blue.scaledToHeight(30);

    Q_ASSERT(iEndIO>=iStartIO);
    m_vIO.resize(iEndIO-iStartIO+1);
    int iIndex=0;
    for(int i=iStartIO;i<=iEndIO;i++)
    {
        IOCTRL& ctrl=m_vIO[iIndex];
        ctrl.iIOIndex=i;
        QLabel* plabel_icon=new QLabel();
        plabel_icon->setMinimumSize(30,30);
        plabel_icon->setMaximumSize(30,30);
        ctrl.pLabel_icon=plabel_icon;
        QLabel* plabel_text=new QLabel();
        plabel_text->setMidLineWidth(50);
        ctrl.pLabel_text=plabel_text;
        iIndex++;
    }
}

void CMWCompIOIn::MInit()
{
    CIO* pIO=GET_CHDIRECT(IO_INPUT,CIO);
    int isize=m_vIO.size();
    for(int i=0;i<isize;i++)
    {
        IOCTRL& ctrl=m_vIO[i];
        QHBoxLayout* hlayout=new QHBoxLayout();
        ctrl.pLabel_icon->setPixmap(icon_off);
        hlayout->addWidget(ctrl.pLabel_icon);
        ctrl.pLabel_text->setText(QString::number(ctrl.iIOIndex)
                                  +":"
                                  +pIO->m_io[ctrl.iIOIndex].singlename
                                    + " " +  QString::number(pIO->m_io[ctrl.iIOIndex].ioIndex));
        hlayout->addWidget(ctrl.pLabel_text);
        m_pGridLayOut->addLayout(hlayout,i%(isize/2),i/(isize/2));
    }
}

void CMWCompIOIn::MTimeFresh()
{
    CIO* pIO=GET_CHDIRECT(IO_INPUT,CIO);
    int isize=m_vIO.size();
    for(int i=0;i<isize;i++)
    {
        IOCTRL& ctrl=m_vIO[i];
        int iIndex=ctrl.iIOIndex;
        if(iIndex>=pIO->m_iIOcnt) return;
        int icurData=int(pIO->m_io[iIndex].fdata);
        if(icurData!=ctrl.set)
        {
            ctrl.set=icurData;
            if(icurData)
            {ctrl.pLabel_icon->setPixmap(icon_blue);}
            else
            {ctrl.pLabel_icon->setPixmap(icon_off);}
        }
    }
    //TODO:
     //if(m_gMainWindow->m_bEnableSendFreshMsg)
    {
        int iSize= m_vIO.size();

        int iSMID=SM_IO;
        VS_Thread* m_pState=(VS_Thread*)m_gSystem.FindSM(iSMID)->GetIdleState();
        if(m_pState->QueueSize()<1000)
        {
            for(int i=0;i<iSize;i++)
            {
                IOCTRL& ctrl=m_vIO[i];
                int IOIndex= ctrl.iIOIndex;
                m_pState-> pushback_Msg(VarMsg_Driver_ID,QObject::tr("Fresh Input IO Sig"),
                                     QVariantList()<<MOTOR_DRIVER<<CMotorDriver::FRESHIO
                                     <<IO_INPUT<<IOIndex);
            }
        }
    }
}

void CMWCompIOIn::showEvent(QShowEvent *pEvent)
{
    CMWidget::showEvent(pEvent);
}

void CMWCompIOIn::hideEvent(QHideEvent *event)
{
    CMWidget::hideEvent(event);
}

