#include "cmwcalicass.h"
#include "ui_cmwcalicass.h"
#include "logic.h"
CMWCaliCass::CMWCaliCass(QWidget *parent, bool bSetting) :
    CMWidget(parent),
    ui(new Ui::CMWCaliCass)
{
    ui->setupUi(this);
	m_bSetting = bSetting;
}

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

void CMWCaliCass::MInit()
{
	if(!m_bSetting){
    CProcess_Scan* pScan=GET_PROCESS(PROCESS_SCAN,
                                     SM_TRANS,
                                     CProcess_Scan);
    Q_ASSERT(pScan);
    Variant* pparam[20]={0};
    int ID[20]={0};
	pparam[0]=pScan->m_Variants.get_Variant(Var_Process_WaferCheckType);

    pparam[1]=pScan->m_Variants.get_Variant(Var_Cass_FirstClawPos_ID);
    pparam[2]=pScan->m_Variants.get_Variant(Var_ClawPos_CheckBox_ID );
	pparam[3]=pScan->m_Variants.get_Variant(Var_Cass_CassUpPos_ID);
    pparam[4]=pScan->m_Variants.get_Variant(Var_Cass_UnloadOffset_ID);
    //pparam[4]=pScan->m_Variants.get_Variant(Var_CheckBox_EnableCheck);
    

    CCassette* pCass=GET_CHDIRECT(CASSETTE,CCassette);
    pparam[5]=pCass->m_Variants.get_Variant(Var_Cass_Slot_Count);
    pparam[6]=pScan->m_Variants.get_Variant(Var_Cass_Slot_Step);

	pparam[7]=pScan->m_Variants.get_Variant(Var_Cass_FirstScanPos_ID);
	pparam[8]=pScan->m_Variants.get_Variant(Var_Cass_ScanPosStart);
	pparam[9]=pScan->m_Variants.get_Variant(Var_Cass_ScanPosEnd);
	pparam[10]=pScan->m_Variants.get_Variant(Var_Cass_ScanSensorOffset);
	pparam[11]=pScan->m_Variants.get_Variant(Var_Cass_WaferThickness);
	
	pparam[12]=pScan->m_Variants.get_Variant(Var_Process_UserTwoSensor);
	pparam[13]=pScan->m_Variants.get_Variant(Var_Process_ScanSpeed);
	pparam[14]=pScan->m_Variants.get_Variant(Var_Process_DelayCompensation);
	pparam[15]=pScan->m_Variants.get_Variant(Var_Process_WPOverrlapped_ThickLimit1);
	pparam[16]=pScan->m_Variants.get_Variant(Var_Process_WPOverrlapped_ThickLimit2);
	pparam[17]=pScan->m_Variants.get_Variant(Var_Process_WPMalposed_ThickLimit);
	pparam[18]=pScan->m_Variants.get_Variant(Var_Process_WaferExistsRange);

	pparam[19] = pScan->m_Variants.get_Variant(Var_Rail_Scan_Pos_ID);
	

    int configAbs=MOTORC_CASS|(CMotorDriver::ABSMOVE_TYPE<<16);
    int configRel=MOTORC_CASS|(CMotorDriver::RELMOVE_TYPE<<16);
    for(int i=1;i<=3;i++)
    {
        ID[i]=configAbs;
    }
	ID[4]=configRel;
    ID[7]=configAbs;
	ID[8]=configAbs;
	ID[9]=configAbs;
	ID[19]= MOTORB1_LOCK | (CMotorDriver::ABSMOVE_TYPE << 16);
    ui->widget->MParamInit_list(pparam,20,ID);

    connect(ui->widget,
            SIGNAL(ListNameClicked_Singal(int32_t)),
            this,
            SLOT(ListButtonClicked_Slot(int32_t)));

    mchildwidgets[intptr_t(ui->widget)]=ui->widget;
	}else{
		CProcess_Scan* pScan=GET_PROCESS(PROCESS_SCAN,
			SM_TRANS,CProcess_Scan);
		Q_ASSERT(pScan);
		Variant* pparam[20]={0};
		int ID[20]={0};
		pparam[0]=pScan->m_Variants.get_Variant(Var_Cass_FirstClawPos_ID);
		pparam[1]=pScan->m_Variants.get_Variant(Var_Cass_FirstScanPos_ID);
		pparam[2]=pScan->m_Variants.get_Variant(Var_ClawPos_CheckBox_ID );

		pparam[3]=pScan->m_Variants.get_Variant(Var_Cass_UnloadOffset_ID);
		pparam[4]=pScan->m_Variants.get_Variant(Var_Cass_CassUpPos_ID);

		CCassette* pCass=GET_CHDIRECT(CASSETTE,CCassette);
		//pparam[5]=pScan->m_Variants.get_Variant(Var_Cass_Slot_Step);
		pparam[5]=pScan->m_Variants.get_Variant(Var_Cass_ScanPosStart);
		pparam[6]=pScan->m_Variants.get_Variant(Var_Cass_ScanPosEnd);

		//pparam[8]=pScan->m_Variants.get_Variant(Var_Process_WaferCheckType);

		int configAbs=MOTORC_CASS|(CMotorDriver::ABSMOVE_TYPE<<16);
		int configRel=MOTORC_CASS|(CMotorDriver::RELMOVE_TYPE<<16);
		for(int i=0;i<=2;i++)
		{
			ID[i]=configAbs;
		}
		ID[3]=configRel;
		ID[4]=configAbs;

		ID[5]=configAbs;
		ID[6]=configAbs;
		ui->widget->MParamInit_list(pparam,7,ID);

		connect(ui->widget,
			SIGNAL(ListNameClicked_Singal(int32_t)),
			this,
			SLOT(ListButtonClicked_Slot(int32_t)));

		mchildwidgets[intptr_t(ui->widget)]=ui->widget;
	}
}

void CMWCaliCass::MTimeFresh()
{
    if(m_gSystem.m_bNeedFreshUI)
    {
        MFresh();
        m_gSystem.m_bNeedFreshUI=false;
    }
}

void CMWCaliCass::ListButtonClicked_Slot(int32_t iID)
{
    double data= ui->widget->m_pTable->item(iID>>24,1)->text()
            .toDouble();
    V_MsgQueue msgqueue;
    if(((iID>>16)&0xFF)==CMotorDriver::ABSMOVE_TYPE)
    {
        m_gSystem.absmove_msg(data,iID&0xFFFF,msgqueue);
    }
    else if(((iID>>16)&0xFF)==CMotorDriver::RELMOVE_TYPE)
    {
        m_gSystem.relmove_msg(data,iID&0xFFFF,msgqueue);
    }
    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}

void CMWCaliCass::on_pushButton_clicked()
{
    int iSlotIndex=ui->lineEdit->text().toDouble()-1;

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Scan Slot Sig"),
                          QVariantList()
                          <<PROCESS_SCAN<<CProcess_Scan::MOVESLOTSCANPOS
                          <<CASSETTE<<iSlotIndex,0);
    m_gSystem.start_test(SM_TRANS, msgqueue);

}

void CMWCaliCass::on_pushButton_LoadPos_clicked()
{
    int iSlotIndex=ui->lineEdit->text().toDouble()-1;

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Scan Slot Sig"),
                          QVariantList()
                          <<PROCESS_SCAN<<CProcess_Scan::MOVESLOTLOADPOS
                          <<CASSETTE<<iSlotIndex<<false,0);
    m_gSystem.start_test(SM_TRANS, msgqueue);
}

void CMWCaliCass::on_pushButton_UnLoadPos_clicked()
{

    int iSlotIndex=ui->lineEdit->text().toDouble()-1;

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Scan Slot Sig"),
                          QVariantList()
                          <<PROCESS_SCAN<<CProcess_Scan::MOVESLOTUNLOADPOS
                          <<CASSETTE<<iSlotIndex<<false,0);
    m_gSystem.start_test(SM_TRANS, msgqueue);
}

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