#include "cmwcalicoat.h"
#include "ui_cmwcalicoat.h"
#include "logic.h"
CMWCaliCoat::CMWCaliCoat(QWidget *parent,bool bIsSetting) :
    CMWidget(parent),
    ui(new Ui::CMWCaliCoat)
{
	m_bSetting = bIsSetting;
    ui->setupUi(this);
	if(!bIsSetting){
		m_pMWCleanTable=new CMWCaliTableCleaning(this,
							CMWCaliTableCleaning::COAT_WIDGET);
		ui->verticalLayout->addWidget(m_pMWCleanTable);

		m_pMWCleanCoatFluid=new CMWCaliCleanCoatFluid(this);
		ui->verticalLayout->addWidget(m_pMWCleanCoatFluid);
	}
}
CMWCaliCoat::~CMWCaliCoat()
{
    delete ui;
}

void CMWCaliCoat::MInit()
{
	if(!m_bSetting){
    CProcess_Coat* pCoat=GET_PROCESS(PROCESS_COAT,
                                          SM_COAT,
                                          CProcess_Coat);
    Q_ASSERT(pCoat);

    Variant* pparam[21]={0};

    pparam[0]=pCoat->m_Variants.get_Variant(Var_Coat_Arm_WaitPos_ID);
    pparam[1]=pCoat->m_Variants.get_Variant(Var_Coat_Arm_CenterPos_ID);
    pparam[2]=pCoat->m_Variants.get_Variant(Var_Coat_Arm_EagePos_ID);
    pparam[3]=pCoat->m_Variants.get_Variant(Var_Coat_Arm_PerparePos_ID);

    //pparam[4]=pCoat->m_Variants.get_Variant(Var_Coat_Arm_Speed_ID);
    //pparam[5]=pCoat->m_Variants.get_Variant(Var_Coat_M_CenterSpeed_ID);
    //pparam[6]=pCoat->m_Variants.get_Variant(Var_Coat_M_EageSpeed_ID);
    //pparam[7]=pCoat->m_Variants.get_Variant(Var_Coat_M_CenterWaitTime_ID);

    pparam[4]=pCoat->m_Variants.get_Variant(Var_Coat_Clean_Arm_WaitPos_ID);
    pparam[5]=pCoat->m_Variants.get_Variant(Var_Coat_Clean_Arm_PerparePos_ID);
	pparam[6]=pCoat->m_Variants.get_Variant(Var_Coat_Clean_ProtectionBlockPos);
    pparam[7]=pCoat->m_Variants.get_Variant(Var_Coat_Clean_Arm_EagePos_ID);
    pparam[8]=pCoat->m_Variants.get_Variant(Var_Coat_Clean_Arm_CenterPos_ID);
    pparam[9]=pCoat->m_Variants.get_Variant(Var_Coat_Clean_Arm_RingPos_ID);

    pparam[10]=pCoat->m_Variants.get_Variant(Var_Coat_Spindle_InitPos_ID);

    pparam[11]=pCoat->m_Variants.get_Variant(Var_Coat_Bump_Max_ID);
    pparam[12]=pCoat->m_Variants.get_Variant(Var_Coat_Spay_Pre_WaitTime);
    pparam[13]=pCoat->m_Variants.get_Variant(Var_Coat_Spay_Spd_ID);
    pparam[14]=pCoat->m_Variants.get_Variant(Var_Coat_Spay_ZeroOffset_ID);
	pparam[15]=pCoat->m_Variants.get_Variant(Var_Coat_ReplaceFluidVolume);
	

    //pparam[16]=pCoat->m_Variants.get_Variant(Var_Coat_Protect_ClosePos_ID);
    //pparam[17]=pCoat->m_Variants.get_Variant(Var_Coat_Protect_OpenPos_ID);

    pparam[16]=pCoat->m_Variants.get_Variant(Var_Coat_Nozzel_ShutSleep_ID);

    int configCoatHand=MOTORE2_COAT_COAT_ARM|(CMotorDriver::ABSMOVE_TYPE<<16);
    int configCleanHand=MOTORE1_COAT_CLEAN_ARM|(CMotorDriver::ABSMOVE_TYPE<<16);
    int configSpindle=MOTORE_COAT_SPINDLE|(CMotorDriver::ABSMOVE_TYPE<<16);
//    int configSpayRel=MOTORE3_COAT_BUMP|(CMotorDriver::RELMOVE_TYPE<<16);
//    int configSpayAbs=MOTORE3_COAT_BUMP|(CMotorDriver::ABSMOVE_TYPE<<16);
    //int configProtectAbs=MOTORE4_COAT_CURTAIN|(CMotorDriver::ABSMOVE_TYPE<<16);

    int iID[21]={0};
    for(int i=0;i<=3;i++)
    {
       iID[i]=configCoatHand;
    }
    for(int i=4;i<=9;i++)
    {
       iID[i]=configCleanHand;
    }
    for(int i=10;i<=10;i++)
    {
       iID[i]=configSpindle;
    }
    /*for(int i=16;i<=17;i++)
    {
       iID[i]=configProtectAbs;
    }*/

    ui->widget->MParamInit_list(pparam,17,iID);
    mchildwidgets[0]=ui->widget;

	mchildwidgets[1]=m_pMWCleanTable;
	mchildwidgets[2]=m_pMWCleanCoatFluid;
	}else{
		CProcess_Coat* pCoat=GET_PROCESS(PROCESS_COAT,
			SM_COAT,
			CProcess_Coat);
		Q_ASSERT(pCoat);

		Variant* pparam[21]={0};
		pparam[0]=pCoat->m_Variants.get_Variant(Var_Coat_Arm_PerparePos_ID);
		pparam[1]=pCoat->m_Variants.get_Variant(Var_Coat_Arm_EagePos_ID);
		pparam[2]=pCoat->m_Variants.get_Variant(Var_Coat_Arm_CenterPos_ID);

		pparam[3]=pCoat->m_Variants.get_Variant(Var_Coat_Clean_Arm_EagePos_ID);
		pparam[4]=pCoat->m_Variants.get_Variant(Var_Coat_Clean_Arm_CenterPos_ID);
		pparam[5]=pCoat->m_Variants.get_Variant(Var_Coat_Clean_Arm_RingPos_ID);

		pparam[6]=pCoat->m_Variants.get_Variant(Var_Coat_Spindle_InitPos_ID);
		

		pparam[7]=pCoat->m_Variants.get_Variant(Var_Coat_Protect_ClosePos_ID);
		pparam[8]=pCoat->m_Variants.get_Variant(Var_Coat_Protect_OpenPos_ID);

		pparam[9]=pCoat->m_Variants.get_Variant(Var_Coat_Nozzel_ShutSleep_ID);
		pparam[10]=pCoat->m_Variants.get_Variant(Var_Coat_ReplaceFluidVolume);

		CCoatVolumeSensor* pVolumeSensor=GET_CARD(COATVOLUME_SENSOR,CCoatVolumeSensor);
		pparam[11]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_AlarmDiffVolume);
		pparam[12]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_AlarmVolume);
		pparam[13]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_WarningVolume);
		pparam[14]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_ReplaceDate);
		pparam[15]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_InvalidDays);

		int configCoatHand=MOTORE2_COAT_COAT_ARM|(CMotorDriver::ABSMOVE_TYPE<<16);
		int configCleanHand=MOTORE1_COAT_CLEAN_ARM|(CMotorDriver::ABSMOVE_TYPE<<16);
		int configSpindle=MOTORE_COAT_SPINDLE|(CMotorDriver::ABSMOVE_TYPE<<16);
		//    int configSpayRel=MOTORE3_COAT_BUMP|(CMotorDriver::RELMOVE_TYPE<<16);
		//    int configSpayAbs=MOTORE3_COAT_BUMP|(CMotorDriver::ABSMOVE_TYPE<<16);
		//int configProtectAbs=MOTORE4_COAT_CURTAIN|(CMotorDriver::ABSMOVE_TYPE<<16);

		int iID[21]={0};
		for(int i=0;i<=2;i++)
		{
			iID[i]=configCoatHand;
		}
		for(int i=3;i<=5;i++)
		{
			iID[i]=configCleanHand;
		}
		for(int i=6;i<=6;i++)
		{
			iID[i]=configSpindle;
		}
		//for(int i=7;i<=8;i++)
		//{
		//	iID[i]=configProtectAbs;
		//}

		ui->widget->MParamInit_list(pparam,16,iID);
		mchildwidgets[0]=ui->widget;
	}
    CMWidget::MInit();

}

void CMWCaliCoat::MTimeFresh()
{
   CMotorDriver* pMotorDriver=GET_CARD(MOTOR_DRIVER,
                                        CMotorDriver);

    if ((bool_t)ui->pushButton_CurtainOpened->isChecked()!=
        (bool)pMotorDriver->get_iodata(IO_OUTPUT,OUT_COAT_CURTAIN_OPENED, false))
    {
        ui->pushButton_CurtainOpened->toggle( );
    }
    if ((bool_t)ui->pushButton_CoatHandUp->isChecked()!=
        pMotorDriver->get_iodata(IO_OUTPUT,OUT_COAT_ARM_UP,false))
    {
        ui->pushButton_CoatHandUp->toggle();
    }
    if ((bool_t)ui->pushButton_Dryopen->isChecked()!=
        pMotorDriver->get_iodata(IO_OUTPUT,OUT_COAT_ELECT_AIR,false))
    {
        ui->pushButton_Dryopen->toggle( );
    }
    if ((bool_t)ui->pushButton_TwoFluidOpen->isChecked()!=
        pMotorDriver->get_iodata(IO_OUTPUT,OUT_COAT_TWOFLUID_AIR,false))
    {
        ui->pushButton_TwoFluidOpen->toggle( );
    }

	freshButtonStatus(SM_COAT, VarState_Spay_ID, ui->pushButton_Spray);
	freshButtonStatus(SM_COAT, VarState_Bump_ID, ui->pushButton_Bump);
	freshButtonStatus(SM_COAT, VarState_ReplaceFluid_ID, ui->btnReplaceCoatFluid);
    CMWidget::MTimeFresh();
}
void CMWCaliCoat::on_pushButton_CurtainOpened_clicked(bool checked)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Coat Curtain OpenClose Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::SETIO
                          <<IO_OUTPUT<<OUT_COAT_CURTAIN_OPENED
                          <<checked,0);
    m_gSystem.start_test(SM_COAT, msgqueue, true);

}

void CMWCaliCoat::on_pushButton_CoatHandUp_clicked(bool checked)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("CoatHand UpDown Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::SETIO
                          <<IO_OUTPUT<<OUT_COAT_ARM_UP
                          <<checked,0);
    m_gSystem.start_test(SM_COAT, msgqueue, true);
}

void CMWCaliCoat::on_pushButton_TwoFluidOpen_clicked(bool checked)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("TwoFluid OpenClose Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::SETIO
                          <<IO_OUTPUT<<OUT_COAT_TWOFLUID_WATER
                          <<checked,0);
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("TwoFluid OpenClose Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::SETIO
                          <<IO_OUTPUT<<OUT_COAT_TWOFLUID_AIR
                          <<checked,0);
    m_gSystem.start_test(SM_COAT, msgqueue, true);
}

void CMWCaliCoat::on_pushButton_Dryopen_clicked(bool checked)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("DryBlow OpenClose Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::SETIO
                          <<IO_OUTPUT<<OUT_COAT_ELECT_AIR
                          <<checked,0);
    m_gSystem.start_test(SM_COAT, msgqueue, true);
}


void CMWCaliCoat::on_pushButton_Bump_clicked()
{
    //吸胶
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Coat_BumpTest,
                          QObject::trUtf8("Bump test Sig"));
    m_gSystem.start_manual(SM_COAT, msgqueue,true);
}

void CMWCaliCoat::on_pushButton_Spray_clicked()
{
    //唝胶
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Coat_SpayTest,
                          QObject::trUtf8("Spay test Sig"));
    m_gSystem.start_manual(SM_COAT, msgqueue,true);
}


void CMWCaliCoat::on_btnReplaceCoatFluid_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Coat_ReplaceFluid,
                          QObject::trUtf8("Replace coat fluid"));
    m_gSystem.start_manual(SM_COAT, msgqueue,true);
}

void CMWCaliCoat::on_pushButton_WaterOpen_clicked(bool checked)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Water Flow OpenClose Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::SETIO
                          <<IO_OUTPUT<<OUT_COAT_TWOFLUID_WATER
                          <<checked,0);
    m_gSystem.start_test(SM_MAIN, msgqueue, true);
}

void CMWCaliCoat::on_pushButton_Export_clicked()
{
	ui->widget->ExportParas();
}


void CMWCaliCoat::freshButtonStatus(int iSMID, int iStateId, QPushButton* pBtn)
{
	QMutexLocker l(&m_gSystem.FindSM(iSMID)->m_SMMutex);
	int iType = m_gSystem.FindSM(iSMID)->m_pCurState->m_iStateType;
	int iCurStateId = m_gSystem.FindSM(iSMID)->m_pCurState->m_iID;
	bool bSemiRun = false;
	if(iType == VS_Thread::State_Work_Test)
	{
		//手动指令运行中
		bSemiRun = true;
	}
	l.unlock();
	if(bSemiRun){
		if(iCurStateId == iStateId){
			pBtn->setChecked(true);
			pBtn->setEnabled(true);
		}else{
			pBtn->setChecked(false);
			pBtn->setEnabled(false);
		}
	}else{
		pBtn->setChecked(false);
		pBtn->setEnabled(true);
	}
}