#include "cctrlcassette.h"
#include "logic.h"

CCtrlCassette::CCtrlCassette(QWidget *parent)
    : CMWidget(parent)
{
    m_iCurSlotCount=-1;
    m_pLayOut=new QVBoxLayout(this);
    setLayout(m_pLayOut);
    m_pLayOut->setSpacing(0);
    m_pLayOut->setMargin(0);
	m_pLayOut->setContentsMargins(10, 10, 10, 0);

	m_pDlgCasslotStatuSet = new CDlgCasslotStatuSet;
	m_pDlgCasslotStatuSet->setModal(true);

	/*m_pCasslotStatusBtn = new QPushButton(this);
	m_pCasslotStatusBtn->setSizePolicy(QSizePolicy::Expanding,
		QSizePolicy::Fixed);
	m_pCasslotStatusBtn->setCheckable(true);
	m_pCasslotStatusBtn->setMinimumHeight(60);
	m_pCasslotStatusBtn->setMinimumWidth(200);
	m_pCasslotStatusBtn->setText(QObject::tr("Set Wafer Status"));
	connect(m_pCasslotStatusBtn, SIGNAL(clicked(bool)), this, SLOT(CasslotStatusBtn_Clicked(bool)));*/

    m_pCheckboxBtn=new QPushButton(this);
    m_pCheckboxBtn->setSizePolicy(QSizePolicy::Expanding,
                                    QSizePolicy::Fixed);
    m_pCheckboxBtn->setCheckable(true);
	m_pCheckboxBtn->setMinimumHeight(60);
    m_pCurCheckSlot=nullptr;
    connect(m_pCheckboxBtn,SIGNAL(clicked(bool)),this,SLOT(CheckBoxBtn_Clicked(bool)));
}

CCtrlCassette::~CCtrlCassette()
{
    ClearSlotCtrls();
	delete m_pDlgCasslotStatuSet;
}

void CCtrlCassette::MTimeFresh()
{
    CCassette* pCass=GET_CHDIRECT(CASSETTE,CCassette);
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    int iSlotCount= pCass->m_Variants.get_VData(Var_Cass_Slot_Count)
            ->toInt();

    if(m_vSlotCtrls.size()!=iSlotCount)
    {
		//m_pLayOut->addWidget(m_pCasslotStatusBtn);

        ClearSlotCtrls();
        m_vSlotCtrls.resize(iSlotCount);
        for(int i=0;i<iSlotCount;i++)
        {
            slotCtrl& ctrl=m_vSlotCtrls[iSlotCount-i-1];
            ctrl.pCheckBox=new CCtrlCheckBoxID(this,iSlotCount-i-1);
            connect(ctrl.pCheckBox,
                    SIGNAL(MClick_Signal(int,bool)),
                    this,
                    SLOT(SlotButton_Clicked(int,bool)));
            ctrl.pCheckBox->setText(pCass->ch_wp_status[NOWP] + QString(" Slot %1").arg(iSlotCount-i));
            ctrl.pCheckBox->setStyleSheet("background-color:gray;color:white");
            ctrl.pCheckBox->setChecked(false);
            m_pLayOut->addWidget(ctrl.pCheckBox);
        }

        m_pLayOut->addWidget(m_pCheckboxBtn);
    }
    if(m_pCurCheckSlot!=ptrans->m_pSlotCheckBox)
    {
       m_pCurCheckSlot=ptrans->m_pSlotCheckBox;
       if(m_pCurCheckSlot)
       {
            int istate=m_pCurCheckSlot->WPstatus;
			
       }
       else
       {
           m_pCheckboxBtn->setText(pCass->ch_wp_status[NOWP]);
       }
    }
	if(m_pCurCheckSlot == nullptr)
	{
		m_pCheckboxBtn->setText(pCass->ch_wp_status[NOWP]);
		m_pCheckboxBtn->setChecked(false);
	}else{
		int icheckstate = m_pCurCheckSlot->CheckStatus;
		QString info;
		if(icheckstate==CHECKCOATING)
			info = tr("CHECK COAT");
		else if(icheckstate == CHECKCOATFINISHED)
			info = tr("COAT PASS");
		else if(icheckstate == CHECKCUTING)
			info = tr("CHECK CUT");
		else if(icheckstate == CHECKCUTFINISHED)
			info = tr("CUT PASS");
		else
			info = tr("UNDEFINED");

		info += QString("(%1)").arg(m_pCurCheckSlot->index+1);
		m_pCheckboxBtn->setText(info);
	}

    for(int i=0;i<iSlotCount;i++)
    {
        slotCtrl& ctrl=m_vSlotCtrls[i];
        if(pCass->vslots[i].bEnableCut!=ctrl.pCheckBox->isChecked())
        {
            ctrl.pCheckBox->setChecked(pCass->vslots[i].bEnableCut);
        }
        if(pCass->vslots[i].WPstatus!=ctrl.Slotstatus)
        {
            ctrl.Slotstatus=pCass->vslots[i].WPstatus;
            ctrl.pCheckBox->setText(pCass->ch_wp_status[ctrl.Slotstatus] + QString(" Slot %1").arg(pCass->vslots[i].index+1));
            if(ctrl.Slotstatus==NOWP)
            {
               ctrl.pCheckBox->setStyleSheet("background-color:gray;color:white");
            }
            else if(ctrl.Slotstatus==WPUNCUT )
            {
                ctrl.pCheckBox->setStyleSheet("background-color:black;color:white");
            }
            else if(ctrl.Slotstatus==WPCUTTED)
            {
                ctrl.pCheckBox->setStyleSheet("background-color:green;color:black");
            }
            else if(ctrl.Slotstatus==WPABORT)
            {
                ctrl.pCheckBox->setStyleSheet("background-color:red;color:black");
            }else if(ctrl.Slotstatus==WPOVERLAPPED||ctrl.Slotstatus==WPMALPOSED||ctrl.Slotstatus==SCANFAILED){
				ctrl.pCheckBox->setStyleSheet("background-color:red;color:black");
			}
            else
            {
                ctrl.pCheckBox->setStyleSheet("background-color:yellow;color:black");
            }
        }
    }


}

void CCtrlCassette::ClearSlotCtrls()
{
    while(m_pLayOut->takeAt(0)!=0);
    int isize=m_vSlotCtrls.size();
    for(int i=0;i<isize;i++)
    {
        slotCtrl& ctrl=m_vSlotCtrls[i];
        if(ctrl.pCheckBox)
        {
            delete ctrl.pCheckBox;
            ctrl.pCheckBox=0;
        }
    }
    m_vSlotCtrls.clear();
}

void CCtrlCassette::CheckBoxBtn_Clicked(bool bchecked)
{

    //if(!m_gSystem.IsAllSMIdle()) return;
	if(!m_gSystem.FindSM(SM_TRANS)->IsIdle()){
		if(bchecked)m_pCheckboxBtn->setChecked(false);
		if(!bchecked)m_pCheckboxBtn->setChecked(true);
		return;
	}
    if(m_pCurCheckSlot)
    {
        if(bchecked){
			//m_pCurCheckSlot->WPstatus=WPCHECKED;
			if(m_pCurCheckSlot->CheckStatus==CHECKCOATING){
				m_pCurCheckSlot->bCheckCoatFinished = true;
				m_pCurCheckSlot->CheckStatus=CHECKCOATFINISHED;
			}else if(m_pCurCheckSlot->CheckStatus==CHECKCUTING){
				m_pCurCheckSlot->bCheckCutFinished = true;
				m_pCurCheckSlot->CheckStatus = CHECKCUTFINISHED;
			}
		}
		/*else {
		m_pCurCheckSlot->WPstatus=WPCHECKING;
		}*/
        //m_pCurCheckSlot=0;
    }
}

void CCtrlCassette::SlotButton_Clicked(int iID,bool istate)
{
	if(!m_gSystem.IsAllSMStopped()) {
		return;
	}

    MqDebug()<<"CheckBox_Clicked";
    CCassette* pCass=GET_CHDIRECT(CASSETTE,CCassette);
    Q_ASSERT(iID<MAX_SLOT);
    pCass->vslots[iID].bEnableCut=istate;
}

void CCtrlCassette::CasslotStatusBtn_Clicked(bool bchecked)
{
    /*if (!m_gSystem.IsAllSMStopped())
    {
        return;
    }

    m_pDlgCasslotStatuSet->show();*/
}
