#include "stable.h"
#include "cmwcompeap.h"
#include "secsmanager.h"
#include "cgemdriver.h"

CMWCompEap::CMWCompEap(QWidget *parent)
	: CMWidget(parent),
	ui(new Ui::CMWCompEap)
{
	ui->setupUi(this);
}

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

void CMWCompEap::on_pushButton_clicked()
{

}

void CMWCompEap::MInit()
{
	CGEMDriver* pDriverGEM=GET_CARD(GEM_DRIVER_JNG, CGEMDriver);
    int iIndex=0;
    Variant* pparam[50]={0};
	pparam[0] = pDriverGEM->m_Variants.get_Variant(Var_MES_ClientIP);
	pparam[1]=pDriverGEM->m_Variants.get_Variant(Var_MES_ClientPort);
	pparam[2]=pDriverGEM->m_Variants.get_Variant(Var_MES_DeviceID);

	pparam[3] = pDriverGEM->m_Variants.get_Variant(Var_MES_MDLN);
	pparam[4]=pDriverGEM->m_Variants.get_Variant(Var_MES_EnableComm);
	pparam[5]=pDriverGEM->m_Variants.get_Variant(Var_MES_ControlMode);

	pparam[6] = pDriverGEM->m_Variants.get_Variant(Var_MES_SoftVersion);
	pparam[7]=pDriverGEM->m_Variants.get_Variant(Var_MES_IsPassive);
	pparam[8]=pDriverGEM->m_Variants.get_Variant(Var_MES_EnableLog);

	pparam[9] = pDriverGEM->m_Variants.get_Variant(Var_MES_AllowHostDownload);
	pparam[10]=pDriverGEM->m_Variants.get_Variant(Var_MES_RecipeRoot);

	pparam[11]=pDriverGEM->m_Variants.get_Variant(Var_MES_Timeout1);
	pparam[12]=pDriverGEM->m_Variants.get_Variant(Var_MES_Timeout2);
	pparam[13]=pDriverGEM->m_Variants.get_Variant(Var_MES_Timeout3);
	pparam[14]=pDriverGEM->m_Variants.get_Variant(Var_MES_Timeout4);
	pparam[15]=pDriverGEM->m_Variants.get_Variant(Var_MES_Timeout5);
	pparam[16]=pDriverGEM->m_Variants.get_Variant(Var_MES_Timeout6);
	pparam[17]=pDriverGEM->m_Variants.get_Variant(Var_MES_Timeout7);

    ui->widget->MParamInit_list(pparam,18);
    mchildwidgets[(intptr_t)ui->widget]=ui->widget;
}

void CMWCompEap::MTimeFresh()
{

}

void CMWCompEap::MFresh()
{
	CMWidget::MFresh();
    On_RecvEAPChanged();

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

void CMWCompEap::MApply()
{
	CMWidget::MApply();
}

void CMWCompEap::on_btnEapInit_clicked()
{
    g_SecsManager.SwitchCommEnable(ui->chkEapEnableComm->isChecked());

	CONTROL_MODE ctrlMode =  eCONTROL_MODE_OFFLINE;
	if(!ui->rdOffline->isChecked()){
		if(ui->rdOnlineLocal->isChecked() == true){
			ctrlMode = eCONTROL_MODE_ONLINE_LOCAL;
		}else if(ui->rdOnlineRemote->isChecked()){
			ctrlMode = eCONTROL_MODE_ONLINE_REMOTE;
		}
	}

	g_SecsManager.SwitchCtrlMode(ctrlMode);

}

void CMWCompEap::on_btnSettingEap_clicked()
{
    g_SecsManager.SwitchCommEnable(ui->chkEapEnableComm->isChecked());

   CONTROL_MODE ctrlMode =  eCONTROL_MODE_OFFLINE;
    if(!ui->rdOffline->isChecked()){
        if(ui->rdOnlineLocal->isChecked() == true){
            ctrlMode = eCONTROL_MODE_ONLINE_LOCAL;
        }else if(ui->rdOnlineRemote->isChecked()){
            ctrlMode = eCONTROL_MODE_ONLINE_REMOTE;
        }
    }

    g_SecsManager.SwitchCtrlMode(ctrlMode);
}


void CMWCompEap::On_RecvEAPChanged()
{
    ui->chkEapEnableComm->blockSignals(true);
    ui->chkEapEnableComm->setChecked(g_SecsManager.getCommEnable());
    ui->chkEapEnableComm->blockSignals(false);

    ui->rdOnlineLocal->blockSignals(true);
    ui->rdOnlineRemote->blockSignals(true);
    ui->rdOnlineLocal->blockSignals(true);
    CONTROL_MODE contrlMode= (CONTROL_MODE)g_SecsManager.getControlMode();
    switch (contrlMode)
    {
    case eCONTROL_MODE_OFFLINE:
        ui->rdOffline->setChecked(true);
        break;
    case eCONTROL_MODE_ONLINE_LOCAL:
        ui->rdOnlineLocal->setChecked(true);
        break;
    case eCONTROL_MODE_ONLINE_REMOTE:
        ui->rdOnlineRemote->setChecked(true);
        break;
    default:
        ui->rdOffline->setChecked(true);
        break;
    }
    ui->rdOnlineRemote->blockSignals(false);
    ui->rdOnlineLocal->blockSignals(false);
    ui->rdOnlineRemote->blockSignals(false);
}

void CMWCompEap::on_btnEapAlarmReset_clicked()
{
    g_SecsManager.ClearAllAlarm();
}

void CMWCompEap::on_btnEapAlarm_clicked()
{
	g_SecsManager.SendAlarm(ALARM_CoatAreaWaterLeakage);
}

void CMWCompEap::on_btnEapSend_clicked()
{
	g_SecsManager.SendEvent(EVENT_BarcodeScanComplete);
}
