#include "cmwcompioinout.h"
#include "logic.h"
#include "gui.h"

CMWCompIOInOut::CMWCompIOInOut(QWidget *parent, QVector<int> vIOIn, QVector<int> vIOOut)
    : CMWidget(parent)
{
    m_pGridLayOut=new QGridLayout(this);
    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);

    for(int i= 0;i< vIOIn.size();i++)
    {
        IOCTRL ctrl;
        ctrl.iIOIndex= vIOIn[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;

		m_mapIO[IO_INPUT].push_back(ctrl);
    }

	for (int i = 0; i < vIOOut.size(); i++)
	{
		IOCTRL ctrl;
		ctrl.iIOIndex = vIOOut[i];

		CCtrlButtonID* pButton = new CCtrlButtonID(this, ctrl.iIOIndex);

		pButton->setMinimumHeight(50);
		pButton->setMaximumWidth(220);
		pButton->setSizePolicy(QSizePolicy::Fixed,
			QSizePolicy::Fixed);
		pButton->setFixedSize(QSize(230, 50));
		pButton->setCheckable(true);
		ctrl.pButton = pButton;

		connect(pButton,
			SIGNAL(MClick_Signal(int32_t)),
			this, SLOT(ButtonOutClicked(int32_t)));

		m_mapIO[IO_OUTPUT].push_back(ctrl);
	}
}

void CMWCompIOInOut::MInit()
{
    CIO* pInIO=GET_CHDIRECT(IO_INPUT,CIO);
    Q_ASSERT(pInIO);

	CIO* pOutIO = GET_CHDIRECT(IO_OUTPUT, CIO);
	Q_ASSERT(pInIO);

	QGridLayout* pGridLayOut = nullptr;
	int nRowIN = 0;
	if (m_mapIO.find(IO_INPUT) != m_mapIO.end())
	{
		pGridLayOut = new QGridLayout(this);
		nRowIN = m_mapIO[IO_INPUT].size();
	}

	for (auto it = m_mapIO.begin(); it != m_mapIO.end(); ++it)
	{
		int i = 0;
		for (IOCTRL& io : it.value())
		{
			IOCTRL& ctrl = io;
			if (it.key() == IO_INPUT)
			{
				QHBoxLayout* hlayout = new QHBoxLayout();
				ctrl.pLabel_icon->setPixmap(icon_off);
				hlayout->addSpacerItem(new QSpacerItem(20, 220, QSizePolicy::Expanding, QSizePolicy::Fixed));
				hlayout->addWidget(ctrl.pLabel_icon);
				ctrl.pLabel_text->setText(QString::number(ctrl.iIOIndex)
					+ ":"
					+ pInIO->m_io[ctrl.iIOIndex].singlename
					+ " " + QString::number(pInIO->m_io[ctrl.iIOIndex].ioIndex));
				hlayout->addWidget(ctrl.pLabel_text);
				hlayout->addSpacerItem(new QSpacerItem(20, 220, QSizePolicy::Expanding, QSizePolicy::Fixed));
				m_pGridLayOut->addLayout(hlayout, i % (it.value().size() / 2), i / (it.value().size() / 2));
				++i;
			}
			else
			{
				ctrl.pButton->setText(QString::number(ctrl.iIOIndex) +
					" "
					+ pOutIO->m_io[ctrl.iIOIndex].singlename + " " + QString::number(pOutIO->m_io[ctrl.iIOIndex].ioIndex));
				if (pGridLayOut)
				{
					pGridLayOut->addWidget(ctrl.pButton, i % (it.value().size() / 2), i / (it.value().size() / 2));
				}
				else
				{
					m_pGridLayOut->addWidget(ctrl.pButton, i % (it.value().size() / 2), i / (it.value().size() / 2));
				}
				++i;
			}
		}

		if (pGridLayOut)
		{
			m_pGridLayOut->addLayout(pGridLayOut, nRowIN / 2, 0, nRowIN * 2, nRowIN * 2);
		}
	}
}

void CMWCompIOInOut::MTimeFresh()
{
	CIO* pInIO = GET_CHDIRECT(IO_INPUT, CIO);
	Q_ASSERT(pInIO);

	CIO* pOutIO = GET_CHDIRECT(IO_OUTPUT, CIO);
	Q_ASSERT(pOutIO);

	for (auto it = m_mapIO.begin(); it != m_mapIO.end(); ++it)
    {
		for (IOCTRL& io : it.value())
		{
			IOCTRL& ctrl = io;
			int iIndex = ctrl.iIOIndex;
			if (it.key() == IO_INPUT)
			{
				if (iIndex >= pInIO->m_iIOcnt) return;
				int icurData = int(pInIO->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);
					}
				}
			}
			else
			{
				if (iIndex >= pOutIO->m_iIOcnt) return;
				bool bCheck = int(pOutIO->m_io[iIndex].fdata) ? true : false;
				if (bCheck != ctrl.pButton->isChecked())
				{
					ctrl.pButton->setChecked(bCheck);
				}
			}
		}
    }

	int iSize = m_mapIO.value(IO_INPUT).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++)
		{
			const IOCTRL& ctrl = m_mapIO.value(IO_INPUT)[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 CMWCompIOInOut::showEvent(QShowEvent *pEvent)
{
    CMWidget::showEvent(pEvent);
}

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

void CMWCompIOInOut::ButtonInClicked(int32_t iID)
{
	if (m_mapIO.value(IO_INPUT).isEmpty())
	{
		return;
	}

	QVector<IOCTRL>& vIO = m_mapIO[IO_INPUT];
    //IOCTRL& ctrl= vIO[iID];
    CIO* pIO=GET_CHDIRECT(IO_INPUT,CIO);
    Q_ASSERT(pIO);
    int iCheck=int(pIO->m_io[iID].fdata)?0:1;

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,QObject::trUtf8("Set Output IO Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::SETIO
                          <<IO_OUTPUT<< iID <<iCheck,0);
    m_gSystem.start_test(SM_MAIN, msgqueue);

}

void CMWCompIOInOut::ButtonOutClicked(int32_t iID)
{
	if (m_mapIO.value(IO_OUTPUT).isEmpty())
	{
		return;
	}

	QVector<IOCTRL>& vIO = m_mapIO[IO_OUTPUT];
	//IOCTRL& ctrl = vIO[iID];
	CIO* pIO = GET_CHDIRECT(IO_OUTPUT, CIO);
	Q_ASSERT(pIO);
	int iCheck = int(pIO->m_io[iID].fdata) ? 0 : 1;

	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Driver_ID, QObject::trUtf8("Set Output IO Sig"),
		QVariantList()
		<< MOTOR_DRIVER << CMotorDriver::SETIO
		<< IO_OUTPUT << iID << iCheck, 0);
	m_gSystem.start_test(SM_MAIN, msgqueue);

}

