#include "cmwmanualstep.h"
#include "ui_cmwmanualstep.h"
#include "logic.h"
#include "qmymessagebox.h"
CMWManualStep::CMWManualStep(QWidget *parent) :
    CMWidget(parent),
    ui(new Ui::CMWManualStep)
{
    ui->setupUi(this);
}

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

void CMWManualStep::MFresh()
{
    if(m_gSystem.m_iCurChID!=CAM2X&&
            m_gSystem.m_iCurChID!=CAM6X)
    {
        showShieldWindow(true,
                    QObject::trUtf8("Switch to any cam."));
        return;
    }
    if(m_gSystem.m_iCurSideID!=SIDE_A&&
            m_gSystem.m_iCurSideID!=SIDE_B)
    {
        showShieldWindow(true,
                    QObject::trUtf8("Switch to side AB."));
        return;
    }
    showShieldWindow(false);
    m_iCurSideID=m_gSystem.m_iCurSideID;
    ui->label_Side->setText(m_gSystem.m_mapNameList[m_iCurSideID]);
    CProductSide* pSide=GET_CHDIRECT(m_iCurSideID,
                                     CProductSide);
    double dStep= pSide->m_Variants.get_VData(Var_ProductSide_SideStep)
            ->toDouble();
    dStep=MMTOUM(dStep);
    ui->lineEdit_Step->setText(QString::number(dStep,'f',3));
//    EnableButton(-1);

	//ui->pushButton_PtRotateSet->setVisible(false);
	//ui->pushButton_PtRotateSet->setVisible(false);


    /*ui->pushButton_PtRotateSet->setEnabled(false);
    ui->pushButton_Pt1Set->setEnabled(false);
    ui->pushButton_PtMidSet->setEnabled(false);
    ui->pushButton_Pt2Set->setEnabled(false);
    ui->pushButton_PtBaseSet->setEnabled(false);*/

	/*ui->pushButton_PtRotate->setVisible(false);
	ui->pushButton_PtRotateSet->setVisible(false)*/;
	CWorker* pWorker=GET_CHDIRECT(M_WORKER,CWorker);
	if(m_gSystem.IsSMAutoRun(SM_MAIN)
		&& pWorker->m_workLevel<LEVEL_ADMINISTRATOR)
	{
		setEnabled(false);
	}
	else
		setEnabled(true);

	if(pWorker->m_workLevel<LEVEL_FACTORAY){
		ui->btnRedo->setVisible(false);
	}else{
		ui->btnRedo->setVisible(true);
	}
}

void CMWManualStep::MApply()
{
    Q_ASSERT(!(m_iCurSideID!=SIDE_A&&m_iCurSideID!=SIDE_B));
    CProductSide* pSide=GET_CHDIRECT(m_iCurSideID,
                                     CProductSide);
    double dStep= ui->lineEdit_Step->text().toDouble();
    dStep=UMTOMM(dStep);
    pSide->m_Variants.get_VData(Var_ProductSide_SideStep)->setValue(dStep);
}

void CMWManualStep::MTimeFresh()
{
    if( strCurFile!=m_gSystem.GetCurFile()||
            m_iCurCamID!=m_gSystem.m_iCurChID||
            m_iCurSideID!=m_gSystem.m_iCurSideID)
    {
        strCurFile=m_gSystem.GetCurFile();
        m_iCurCamID=m_gSystem.m_iCurChID;
        m_iCurSideID=m_gSystem.m_iCurSideID;

        MFresh();
    }

    if(m_gSystem.m_iCurSideID == SIDE_A){
        ui->btnApplyCH2->setEnabled(true);
    }else{
        ui->btnApplyCH2->setEnabled(false);
    }

}
void CMWManualStep::StepMove(int iCount)
{
    double dStep=ui->lineEdit_Step->text().toDouble();

    dStep=UMTOMM(dStep)*iCount*-1;
    V_MsgQueue msgqueue;
    //m_gSystem.relmove_msg(dStep,MOTORX,msgqueue);
    m_gSystem.relmove_msg(dStep, MOTORY, msgqueue);
	m_gSystem.start_manual(SM_MAIN, msgqueue,false);
    int icountCur=ui->lineEdit_Count->text().toDouble();
    ui->lineEdit_Count->setText(QString::number(icountCur+iCount));

}
void CMWManualStep::on_pushButton_N10_clicked()
{
    StepMove(-10);
}

void CMWManualStep::on_pushButton_N1_clicked()
{
    StepMove(-1);
}

void CMWManualStep::on_pushButton_P1_clicked()
{
    StepMove(1);
}

void CMWManualStep::on_pushButton_P10_clicked()
{
    StepMove(10);
}

void CMWManualStep::on_pushButton_Zero_clicked()
{
    CMWorkTableDriver* pWorkTableDriver=GET_CARD(
                WORKTABLE_DRIVER,CMWorkTableDriver);
    pWorkTableDriver->CurAxisToWt(m_gSystem.m_iCurChID,m_WPDPZERO,true);

    ui->lineEdit_Count->setText("0");

}

void CMWManualStep::on_pushButton_Calcu_clicked()
{
    int iStepCount=ui->lineEdit_Count->text().toDouble();
    if(!iStepCount)
    {
        m_gSystem.m_SysMsg.SetMsg(tr("Step Count equal 0"));
        return;
    }
    CMPointd WPDPCur;
    CMWorkTableDriver* pWorkTableDriver=GET_CARD(
                WORKTABLE_DRIVER,CMWorkTableDriver);
    pWorkTableDriver->CurAxisToWt(m_gSystem.m_iCurChID,WPDPCur,true);
    //double dstep= MMTOUM(fabs((WPDPCur.tX-m_WPDPZERO.tX)/iStepCount));
    double dstep = MMTOUM(fabs((WPDPCur.tY - m_WPDPZERO.tY) / iStepCount));
    ui->lineEdit_Step->setText(QString::number(dstep,'f',3));
}

void CMWManualStep::MovePt(int iPtStyle)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Move Align Pt Sig"),
                          QVariantList()
                          <<PROCESS_LEVELING<<CProcess_Level::MOVE_PT
                          <<m_iCurSideID<<m_iCurCamID<<iPtStyle
                          <<false<<0<<0,0);
    m_gSystem.start_manual(SM_MAIN, msgqueue);

    EnableButton(iPtStyle);
}
bool CMWManualStep::SetPt(int iPtStyle)
{
    V_MsgQueue msgqueue;
	if( iPtStyle != CProcess_Level::TYPE_PTBASE){
		msgqueue.pushback_Msg(VarMsg_Process_ID,
			QObject::trUtf8("Set Pt Manual Sig"),
			QVariantList()
			<<PROCESS_LEVELING<<CProcess_Level::SETPT_MANUAL
			<<m_iCurSideID<<m_iCurCamID<<iPtStyle
			<<m_gSystem.m_iCurLine,0);
	}else{
		CProcess_Level* pLevel=GET_PROCESS(PROCESS_LEVELING,SM_MAIN,CProcess_Level);
		msgqueue.pushback_Msg(VarMsg_Process_ID,
			QObject::trUtf8("Set Pt Manual Sig"),
			QVariantList()
			<<PROCESS_LEVELING<<CProcess_Level::SETPT_MANUAL
			<<m_iCurSideID<<m_iCurCamID<<iPtStyle
			<<pLevel->m_iCurLineId,0);
	}
    return m_gSystem.start_manual(SM_MAIN, msgqueue,true);

}

void CMWManualStep::EnableButton(int iIndex)
{
    //ui->pushButton_PtRotateSet->setEnabled(false);
    //ui->pushButton_Pt1Set->setEnabled(false);
    //ui->pushButton_PtMidSet->setEnabled(false);
    //ui->pushButton_Pt2Set->setEnabled(false);
    //ui->pushButton_PtBaseSet->setEnabled(false);
    switch(iIndex)
    {
    case CProcess_Level::TYPE_PTROTATE: ui->pushButton_PtRotateSet->setEnabled(true); break;
    case CProcess_Level::TYPE_PT1:
        ui->pushButton_Pt1Set->setEnabled(true);
        ui->pushButton_PtMidSet->setEnabled(true);
		break;
    case CProcess_Level::TYPE_PTMID: ui->pushButton_PtMidSet->setEnabled(true); break;
    case CProcess_Level::TYPE_PT2: ui->pushButton_Pt2Set->setEnabled(true); break;
    case CProcess_Level::TYPE_PTBASE: ui->pushButton_PtBaseSet->setEnabled(true); break;
    }
}
void CMWManualStep::on_pushButton_Pt1_clicked()
{
    MovePt(CProcess_Level::TYPE_PT1);
}

void CMWManualStep::on_pushButton_Pt1Set_clicked()
{
    if(true == SetPt(CProcess_Level::TYPE_PT1)){
		m_gSystem.abort_state(SM_MAIN,
			VarState_Align_Pt1_MoveSearch_ID,
			QVariantList()<<m_gSystem.m_iCurSideID, true);
	}

}

void CMWManualStep::on_pushButton_PtMid_clicked()
{
    MovePt(CProcess_Level::TYPE_PTMID);

}

void CMWManualStep::on_pushButton_PtMidSet_clicked()
{

    if(true == SetPt(CProcess_Level::TYPE_PTMID)){
		m_gSystem.abort_state(SM_MAIN,
							  VarState_Align_Near_ID,
							  QVariantList()<<m_gSystem.m_iCurSideID, true);


		m_gSystem.abort_state(SM_MAIN,
							  VarState_Align_Rotate_ID,
							  QVariantList()<<m_gSystem.m_iCurSideID, true);
	}


}

void CMWManualStep::on_pushButton_Pt2_clicked()
{
    MovePt(CProcess_Level::TYPE_PT2);
}

void CMWManualStep::on_pushButton_Pt2Set_clicked()
{
	if(true == SetPt(CProcess_Level::TYPE_PT2)){

		m_gSystem.abort_state(SM_MAIN,
							  VarState_Align_Near_ID,
							  QVariantList()<<m_gSystem.m_iCurSideID, true);


		m_gSystem.abort_state(SM_MAIN,
							  VarState_Align_Far_ID,
							  QVariantList()<<m_gSystem.m_iCurSideID, true);
	}

//    m_gSystem.abort_state(SM_MAIN,
//                          VarState_Align_Process_ID,
//                          QVariantList()<<m_gSystem.m_iCurSideID);
}

void CMWManualStep::on_pushButton_PtBase_clicked()
{
    MovePt(CProcess_Level::TYPE_PTBASE);
}

void CMWManualStep::on_pushButton_PtBaseSet_clicked()
{
    if(true == SetPt(CProcess_Level::TYPE_PTBASE)){
		m_gSystem.abort_state(SM_MAIN,
							  VarState_Align_PtBase_MoveSearch_ID,
							  QVariantList()<<m_gSystem.m_iCurSideID, true);
	}

}

void CMWManualStep::on_pushButton_PtRotate_clicked()
{
    MovePt(CProcess_Level::TYPE_PTROTATE);
}

void CMWManualStep::on_pushButton_PtRotateSet_clicked()
{
    if(true == SetPt(CProcess_Level::TYPE_PTROTATE)){
		m_gSystem.abort_state(SM_MAIN,
			VarState_Align_R_MoveSearch_ID,
			QVariantList()<<m_gSystem.m_iCurSideID, true);

		m_gSystem.abort_state(SM_MAIN, VarState_Align_Rotate_ID,  
			QVariantList()<<m_gSystem.m_iCurSideID, true);
	}
}

void CMWManualStep::on_btnCCW_clicked()
{
    V_MsgQueue msgqueue;
    m_gSystem.relmove_msg(-0.2,MOTORW,msgqueue);
	m_gSystem.relmove_msg(0.1,MOTORW,msgqueue);
    m_gSystem.start_manual(SM_MAIN, msgqueue);
}

void CMWManualStep::on_btnCW_clicked()
{
    V_MsgQueue msgqueue;
    m_gSystem.relmove_msg(0.1,MOTORW,msgqueue);
    m_gSystem.start_manual(SM_MAIN, msgqueue);
}

void CMWManualStep::on_btnApplyCH2_clicked()
{
    CProductSide* pSideA=GET_CHDIRECT(SIDE_A,CProductSide);
    CProductSide* pSideB=GET_CHDIRECT(SIDE_B,CProductSide);

    double dOffsetAngle =  m_gSystem.m_Variants.get_VData(Var_XY_AngleOffset)->toDouble();
    pSideB->m_dLevelAngle = pSideA->m_dLevelAngle + dOffsetAngle;
}

void CMWManualStep::on_btnRedo_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_MAIN);
	if(m_gSystem.FindSM(SM_MAIN)->IsStop())
		return ;


	QMyMessageBox msgBoxAsk(QMessageBox::Question, tr("Confirm information"),tr("Are you sure to redo cutting? Notice:The cuted street will be repeatedly cut"),
		QMessageBox::Yes | QMessageBox::No);
	//msgBoxAsk.setStandardButtons();
	msgBoxAsk.setDefaultButton(QMessageBox::No);
	msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Yes"));
	msgBoxAsk.setButtonText(QMessageBox::No, tr("No"));
	int ret = msgBoxAsk.exec();
	if(ret == QMessageBox::Yes){
		pSM->pushfront_Msg(VarMsg_RestartAutoCut,
			QObject::trUtf8("Resume Auto cut sig")); 
	}

	//pSM->pushfront_Msg(VarMsg_StartAuto_ID,
	//	QObject::trUtf8("Start Auto Sig")); 

	//VSMT_Base* pSMTrans= (VSMT_Base*)m_gSystem.FindSM(SM_TRANS);
	//if(pSMTrans->m_pCurState->m_iStateType == VS_Thread::State_Wait){
	//	//开始切割
	//	QString strStart=QObject::trUtf8("Auto Cut Start");
	//	pSMTrans->m_pCurState->pushfront_Msg(VarMsg_Auto_StartCut, strStart);
	//}
	//
}