#include "cmwmanualresettrans.h"
#include "ui_cmwmanualresettrans.h"
#include "logic.h"
#include <QMessageBox>
CMWManualResetTrans::CMWManualResetTrans(QWidget *parent) :
    CMWidget(parent),
    ui(new Ui::CMWManualResetTrans)
{
    ui->setupUi(this);
}

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

void CMWManualResetTrans::MTimeFresh()
{
	CWorker* pWorker=GET_CHDIRECT(M_WORKER,CWorker);
	if(m_gSystem.IsSMAutoRun(SM_TRANS)
		&& pWorker->m_workLevel<LEVEL_ADMINISTRATOR)
	{
		setEnabled(false);
	}
	else 
	{
		setEnabled(true);
	}

	if(pWorker->m_workLevel<LEVEL_ADMINISTRATOR){
		if(m_gSystem.IsSMAutoRun(SM_MAIN)){
			ui->pushButton_WorkTableReset->setEnabled(false);
		}else{
			ui->pushButton_WorkTableReset->setEnabled(true);
		}

		if(m_gSystem.IsSMAutoRun(SM_CLEAN)){
			ui->pushButton_CleanTableReset->setEnabled(false);
		}else{
			ui->pushButton_CleanTableReset->setEnabled(true);
		}

		if(m_gSystem.IsSMAutoRun(SM_COAT)){
			ui->pushButton_CoatTableReset->setEnabled(false);
		}else{
			ui->pushButton_CoatTableReset->setEnabled(true);
		}
	}
}

void CMWManualResetTrans::ResetMsg(CASSSLOT** ppSlot, int iSMID, bool bAsk/*=true*/)
{
	if(bAsk){
		QMessageBox msgBoxAsk(QMessageBox::Question,  tr("Confirm information"),tr("Are you sure reset work status?"));
		msgBoxAsk.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
		msgBoxAsk.setDefaultButton(QMessageBox::No);
		msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Yes"));
		msgBoxAsk.setButtonText(QMessageBox::No, tr("No"));
		int ret = msgBoxAsk.exec();
		if(ret == QMessageBox::No){
			QTimer::singleShot(500, this, [this](){
				MFresh();
			});
			return;
		}
	}
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Reset Slot Sig"),
                          QVariantList()
                          <<PROCESS_TRANS<<CProcess_Trans::RESETSLOT
                          <<(uint64_t)ppSlot,0);
    m_gSystem.start_reset(iSMID, msgqueue);

	QTimer::singleShot(500, this, [this](){
			MFresh();
		});
}

void CMWManualResetTrans::on_pushButton_AllReset_clicked()
{

    if(m_gSystem.IsAllSMStopped()){

		QMessageBox msgBoxAsk(QMessageBox::Question,  tr("Confirm information"),tr("Are you sure reset all status?"));
		msgBoxAsk.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
		msgBoxAsk.setDefaultButton(QMessageBox::No);
		msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Yes"));
		msgBoxAsk.setButtonText(QMessageBox::No, tr("No"));
		int ret = msgBoxAsk.exec();
		if(ret == QMessageBox::Yes){
			CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
				CProcess_Trans);
			try
			{
				ptrans->ResetAllSlot();
			}
			catch (QString& ex)
			{
                m_gSystem.m_SysMsg.SetMsg(ex, WARN_MSG_TYPE);
			}
		}
    }else{
        m_gSystem.m_SysMsg.SetMsg
                (QObject::trUtf8("Stop action before test move"));
    }
    //ResetMsg(0);
	QTimer::singleShot(500, this, [this](){
		MFresh();
	});


}

void CMWManualResetTrans::on_pushButton_GrabReset_clicked()
{
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    ResetMsg(&ptrans->m_pSlotGrab, SM_TRANS);
}

void CMWManualResetTrans::on_pushButton_ArrangeReset_clicked()
{
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    ResetMsg(&ptrans->m_pSlotRailNear, SM_TRANS);
}

void CMWManualResetTrans::on_pushButton_TransferReset_clicked()
{
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    ResetMsg(&ptrans->m_pSlotTrans, SM_TRANS);
}

void CMWManualResetTrans::on_pushButton_CoatTableReset_clicked()
{
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    ResetMsg(&ptrans->m_pSlotCoat, SM_COAT);
}

void CMWManualResetTrans::on_pushButton_LoadReset_clicked()
{
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    ResetMsg(&ptrans->m_pSlotLoad, SM_TRANS);
}

void CMWManualResetTrans::on_pushButton_WorkTableReset_clicked()
{
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    ResetMsg(&ptrans->m_pSlotWT, SM_MAIN);
}

void CMWManualResetTrans::on_pushButton_UnloadReset_clicked()
{
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    ResetMsg(&ptrans->m_pSlotUnLoad, SM_TRANS);
}

void CMWManualResetTrans::on_pushButton_CleanTableReset_clicked()
{
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    ResetMsg(&ptrans->m_pSlotClean, SM_CLEAN);
}

void CMWManualResetTrans::on_pushButton_CheckBoxReset_clicked()
{
	QMessageBox msgBoxAsk(QMessageBox::Question,  tr("Confirm information"),tr("Are you sure reset checkbox status?"));
	msgBoxAsk.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
	msgBoxAsk.setDefaultButton(QMessageBox::No);
	msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Yes"));
	msgBoxAsk.setButtonText(QMessageBox::No, tr("No"));
	int ret = msgBoxAsk.exec();
	if(ret == QMessageBox::Yes){
		CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
			CProcess_Trans);
		ResetMsg(&ptrans->m_pSlotCheckBox, SM_TRANS, false);
	}

	QTimer::singleShot(500, this, [this](){
		MFresh();
	});
}

void CMWManualResetTrans::on_btnLoadRecover_clicked()
{
}

void CMWManualResetTrans::MFresh()
{
	CMWidget::MFresh();

	CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
		CProcess_Trans);

	if(ptrans->m_pSlotGrab)
		ui->pushButton_GrabReset->setChecked(true);
	else
		ui->pushButton_GrabReset->setChecked(false);
	if(ptrans->m_pSlotTrans)
		ui->pushButton_TransferReset->setChecked(true);
	else
		ui->pushButton_TransferReset->setChecked(false);

	if(ptrans->m_pSlotLoad)
		ui->pushButton_LoadReset->setChecked(true);
	else
		ui->pushButton_LoadReset->setChecked(false);
	if(ptrans->m_pSlotCoat)
		ui->pushButton_CoatTableReset->setChecked(true);
	else
		ui->pushButton_CoatTableReset->setChecked(false);

	if(ptrans->m_pSlotRailNear)
		ui->pushButton_ArrangeReset->setChecked(true);
	else
		ui->pushButton_ArrangeReset->setChecked(false);
	if(ptrans->m_pSlotWT)
		ui->pushButton_WorkTableReset->setChecked(true);
	else
		ui->pushButton_WorkTableReset->setChecked(false);
	if(ptrans->m_pSlotUnLoad)
		ui->pushButton_UnloadReset->setChecked(true);
	else
		ui->pushButton_UnloadReset->setChecked(false);
	if(ptrans->m_pSlotClean)
		ui->pushButton_CleanTableReset->setChecked(true);
	else
		ui->pushButton_CleanTableReset->setChecked(false);

	if(ptrans->m_pSlotCheckBox)
		ui->pushButton_CheckBoxReset->setChecked(true);
	else
		ui->pushButton_CheckBoxReset->setChecked(false);

    if(ptrans->m_pSlotRailFar){
        ui->btnRailFarReset->setChecked(true);
    }else{
        ui->btnRailFarReset->setChecked(false);
    }

    CWorker* pWorker = GET_CHDIRECT(M_WORKER, CWorker);
	if (pWorker->m_workLevel >= LEVEL_FACTORAY)
	{
		ui->btnResetRes->setVisible(true);
	}
	else
	{
		ui->btnResetRes->setVisible(false);
	}
}

void CMWManualResetTrans::on_btnRailFarReset_clicked()
{
    CProcess_Trans* ptrans=GET_PROCESS(PROCESS_TRANS,SM_TRANS,
                                   CProcess_Trans);
    ResetMsg(&ptrans->m_pSlotRailFar, SM_TRANS);
}

void CMWManualResetTrans::on_btnResetRes_clicked()
{
    QMessageBox msgBoxAsk(QMessageBox::Question, tr("Confirm information"), tr("Reset schedular resource must be stop status\nAre you sure reset?"));
    msgBoxAsk.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBoxAsk.setDefaultButton(QMessageBox::No);
    msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Yes"));
    msgBoxAsk.setButtonText(QMessageBox::No, tr("No"));
    int ret = msgBoxAsk.exec();
    if (ret != QMessageBox::Yes) {
		return;
    }

    ResManager().Release(SM_TRANS);
    ResManager().Release(SM_COAT);
    ResManager().Release(SM_CLEAN);
    ResManager().Release(SM_Schedular);
    ResManager().Release(SM_WTLoad);
    ResManager().Release(SM_WTUnload);
}
