#include "cmwcaliclean.h"
#include "ui_cmwcaliclean.h"
#include "logic.h"
CMWCaliClean::CMWCaliClean(QWidget *parent,bool bIsSetting) :
    CMWidget(parent),
    ui(new Ui::CMWCaliClean)
{
	m_bSetting = bIsSetting;
    ui->setupUi(this);
	if(!bIsSetting){
    m_pMWCleanTable=new CMWCaliTableCleaning(this,
                        CMWCaliTableCleaning::CLEAN_WIDGET);

    ui->verticalLayout->addWidget(m_pMWCleanTable);
	}
}

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

void CMWCaliClean::MInit()
{
	if(!m_bSetting){
    CProcess_Clean* pClean=GET_PROCESS(PROCESS_CLEAN,
                                          SM_CLEAN,
                                          CProcess_Clean);
    Q_ASSERT(pClean);

    Variant* pparam[21]={0};

    pparam[0]=pClean->m_Variants.get_Variant(Var_Clean_Arm_Center_Pos);
    pparam[1]=pClean->m_Variants.get_Variant(Var_Clean_Arm_Edge_Pos);
    pparam[2]=pClean->m_Variants.get_Variant(Var_Clean_Arm_PerparePos_ID);
    pparam[3]=pClean->m_Variants.get_Variant(Var_Clean_Arm_WaitPos_ID);
    pparam[4]=pClean->m_Variants.get_Variant(Var_Clean_Arm_Spd);
    //pparam[5]=pClean->m_Variants.get_Variant(Var_Clean_Protect_ClosePos_ID);
    // pparam[6] = pClean->m_Variants.get_Variant(Var_Clean_Protect_OpenPos_ID);
    pparam[5]=pClean->m_Variants.get_Variant(Var_Clean_Spindle_InitPos_ID);

    int configSpindle=MOTORF_CLEAN_SPINDLE|(CMotorDriver::ABSMOVE_TYPE<<16);
    int configCleanHand=MOTORF1_CLEAN_ARM|(CMotorDriver::ABSMOVE_TYPE<<16);
    //int configProtectAbs=MOTORF4_CLEAN_CURTAIN|(CMotorDriver::ABSMOVE_TYPE<<16);

    int iID[21]={0};
    for(int i=0;i<=3;i++)
    {
       iID[i]=configCleanHand;
    }
    //for(int i=5;i<=6;i++)
    //{
    //   iID[i]=configProtectAbs;
    //}
    for(int i=7;i<=7;i++)
    {
        iID[i]=configSpindle;
    }
    ui->widget->MParamInit_list(pparam,6,iID);
    mchildwidgets[0]=ui->widget;
	mchildwidgets[1]=m_pMWCleanTable;
	}else{
		CProcess_Clean* pClean=GET_PROCESS(PROCESS_CLEAN,
			SM_CLEAN,
			CProcess_Clean);
		Q_ASSERT(pClean);

		Variant* pparam[21]={0};

		pparam[0]=pClean->m_Variants.get_Variant(Var_Clean_Arm_PerparePos_ID);
		pparam[1]=pClean->m_Variants.get_Variant(Var_Clean_Arm_Edge_Pos);
		pparam[2]=pClean->m_Variants.get_Variant(Var_Clean_Arm_Center_Pos);
		
		pparam[3]=pClean->m_Variants.get_Variant(Var_Clean_Protect_ClosePos_ID);
		pparam[4]=pClean->m_Variants.get_Variant(Var_Clean_Protect_OpenPos_ID);

		pparam[5]=pClean->m_Variants.get_Variant(Var_Clean_Spindle_InitPos_ID);

		int configSpindle=MOTORF_CLEAN_SPINDLE|(CMotorDriver::ABSMOVE_TYPE<<16);
		int configCleanHand=MOTORF1_CLEAN_ARM|(CMotorDriver::ABSMOVE_TYPE<<16);
		//int configProtectAbs=MOTORF4_CLEAN_CURTAIN|(CMotorDriver::ABSMOVE_TYPE<<16);

		int iID[21]={0};
		for(int i=0;i<=2;i++)
		{
			iID[i]=configCleanHand;
		}
        /*for(int i=3;i<=4;i++)
        {
            iID[i]=configProtectAbs;
        }*/
		for(int i=5;i<=5;i++)
		{
			iID[i]=configSpindle;
		}
		ui->widget->MParamInit_list(pparam,6,iID);
		mchildwidgets[0]=ui->widget;
	}


    CMWidget::MInit();
}

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

     if ((bool_t)ui->pushButton_CurtainOpened->isChecked()!=
         pMotorDriver->get_iodata(IO_OUTPUT, OUT_CLEAN_CURTAIN_OPENED,false))
     {
         ui->pushButton_CurtainOpened->toggle( );
     }
     if ((bool_t)ui->pushButton_Dryopen->isChecked()!=
         pMotorDriver->get_iodata(IO_OUTPUT,OUT_CLEAN_AIR,false))
     {
         ui->pushButton_Dryopen->toggle( );
     }
     if ((bool_t)ui->pushButton_TwoFluidOpen->isChecked()!=
         pMotorDriver->get_iodata(IO_OUTPUT,OUT_CLEAN_TWOFLUID_AIR,false))
     {
         ui->pushButton_TwoFluidOpen->toggle( );
     }

     CMWidget::MTimeFresh();
}

void CMWCaliClean::on_pushButton_CurtainOpenedclicked(bool checked)
{

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Clean Curtain Open Close Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::SETIO
                          <<IO_OUTPUT<<OUT_CLEAN_CURTAIN_OPENED
                          <<checked,0);
    m_gSystem.start_test(SM_MAIN, msgqueue);
}

void CMWCaliClean::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_CLEAN_TWOFLUID_WATER
                          <<checked,0);
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("TwoFluid OpenClose Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::SETIO
                          <<IO_OUTPUT<<OUT_CLEAN_TWOFLUID_AIR
                          <<checked,0);
    m_gSystem.start_test(SM_MAIN, msgqueue);
}

void CMWCaliClean::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_CLEAN_AIR
                          <<checked,0);
    m_gSystem.start_test(SM_MAIN, msgqueue);
}

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