#include "cmwparamcoatclean.h"
#include "ui_cmwparamcoatclean.h"
#include "logic.h"
#include "cctrllineedit.h"
#include "cctrlcombo.h"
CMWParamCoatClean::CMWParamCoatClean(QWidget *parent,int itype) :
    CMWidget(parent),
    ui(new Ui::CMWParamCoatClean)
{
    ui->setupUi(this);
	m_itype = itype;
     if(m_itype==COAT_WIDGET){
         int colSize = ui->tableWidget->columnCount();
         ui->tableWidget->insertColumn(colSize);
		 ui->tableWidget->setHorizontalHeaderItem(colSize, new QTableWidgetItem(tr("BumpVolumn")));
         ui->tableWidget->insertColumn(colSize+1);
		 ui->tableWidget->setHorizontalHeaderItem(colSize+1, new QTableWidgetItem(tr("PreSpay")));
     }
    m_iSelectRow=-1;
    ui->tableWidget->horizontalHeader()->
            setSectionResizeMode(QHeaderView::Interactive);

	ui->tableWidget->setColumnWidth(0, 150);

	ui->tableWidget->setEditTriggers(QTableWidget::NoEditTriggers);
}

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

void CMWParamCoatClean::MFresh()
{
    CProcess_CoatClean *pCoatClean=0;
    QMMutexVector<COATCLEANSTEP> coatcleansteps;
    if(m_itype==COAT_WIDGET)
    {
        pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
                               CProcess_Coat);

    }
    else
    {
        pCoatClean=GET_PROCESS(PROCESS_CLEAN,SM_CLEAN,
                               CProcess_Clean);
    }

    coatcleansteps=pCoatClean->m_vCoatCleanStep;
    int isize=coatcleansteps.size();
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(isize);
    for(int i=0;i<isize;i++)
    {
        const COATCLEANSTEP& step=coatcleansteps.at(i);
        QTableWidgetItem* pItem=0;
        if(m_itype==COAT_WIDGET)
        {
            pItem=new QTableWidgetItem(CoatName[step.iType]);
        }
        else
        {
            pItem=new QTableWidgetItem(CleanName[step.iType]);
        }
        pItem->setData(Qt::UserRole,step.iType);
        ui->tableWidget->setItem(i,0,pItem);
        pItem=new QTableWidgetItem(QString::number(step.dTime,'f',0));
        ui->tableWidget->setItem(i,1,pItem);
        pItem=new QTableWidgetItem(QString::number(step.iRPM));
        ui->tableWidget->setItem(i,2,pItem);
		if(m_itype==COAT_WIDGET)
		{
			pItem=new QTableWidgetItem(QString::number(step.dBumpNum));
			ui->tableWidget->setItem(i,3,pItem);
			pItem=new QTableWidgetItem(QString::number(step.dPreSpayNum));
			ui->tableWidget->setItem(i,4,pItem);
		}
    }

	CMWidget::MFresh();
	ui->widget->hide();
}

void CMWParamCoatClean::MApply()
{
    CMWidget::MApply();
    QMMutexVector<COATCLEANSTEP> coatcleansteps;
    int isize=ui->tableWidget->rowCount();
    coatcleansteps.resize(isize);
    on_tableWidget_currentItemChanged(0,ui->tableWidget->currentItem());
	ui->widget->MApply();

    CProcess_CoatClean *pCoatClean=0;
    if(m_itype==COAT_WIDGET)
    {
        pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
                               CProcess_Coat);

    }
    else
    {
        pCoatClean=GET_PROCESS(PROCESS_CLEAN,SM_CLEAN,
                               CProcess_Clean);
    }

	if (ui->tableWidget->currentItem() && m_itype==COAT_WIDGET)
	{
		COATCLEANSTEP& step = pCoatClean->m_vCoatCleanStep[ui->tableWidget->currentItem()->row()];
	    step.dSpayNum = pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Num_ID)->toDouble();
	    step.dSpaySpd = pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Spd_ID)->toDouble();
	    step.dCenterSpeed = pCoatClean->m_Variants.get_VData(Var_Coat_M_CenterSpeed_ID)->toDouble();
	    step.dEageSpeed = pCoatClean->m_Variants.get_VData(Var_Coat_M_EageSpeed_ID)->toDouble();
	    step.dCenterWaitTime = pCoatClean->m_Variants.get_VData(Var_Coat_M_CenterWaitTime_ID)->toDouble();
        step.dCoatPreSpyNum = pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Pre_Num_ID)->toDouble();
        step.iCoatPreSpyWaitTime = pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Pre_WaitTime)->toInt();
	}

	for(int i=0;i<isize&&pCoatClean->m_vCoatCleanStep.size();i++)
	{
		pCoatClean->m_vCoatCleanStep[i].iType=ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt();
		pCoatClean->m_vCoatCleanStep[i].dTime=ui->tableWidget->item(i,1)->text().toDouble();
		pCoatClean->m_vCoatCleanStep[i].iRPM=ui->tableWidget->item(i,2)->text().toInt();
		if(m_itype==COAT_WIDGET){
			pCoatClean->m_vCoatCleanStep[i].dBumpNum=ui->tableWidget->item(i,3)->text().toDouble();
			pCoatClean->m_vCoatCleanStep[i].dPreSpayNum=ui->tableWidget->item(i,4)->text().toDouble();
		}
	}
    //pCoatClean->m_vCoatCleanStep=coatcleansteps;
}

void CMWParamCoatClean::MInit()
{
    Variant* pparams[7];
    if(m_itype==COAT_WIDGET)
    {
        CProcess_Coat* pCoat=GET_PROCESS(PROCESS_COAT,
                                         SM_COAT,
                                         CProcess_Coat);
        Q_ASSERT(pCoat);
        pparams[0]=pCoat->m_Variants.get_Variant(Var_Coat_Spay_Num_ID);
        pparams[1]=pCoat->m_Variants.get_Variant(Var_Coat_Spay_Spd_ID);
        pparams[2]=pCoat->m_Variants.get_Variant(Var_Coat_M_CenterSpeed_ID);
        pparams[3]=pCoat->m_Variants.get_Variant(Var_Coat_M_EageSpeed_ID);
        pparams[4]=pCoat->m_Variants.get_Variant(Var_Coat_M_CenterWaitTime_ID);
		pparams[5]=pCoat->m_Variants.get_Variant(Var_Coat_Spay_Pre_Num_ID);
		pparams[6]=pCoat->m_Variants.get_Variant(Var_Coat_Spay_Pre_WaitTime);

        ui->widget->MParamInit_list(pparams,7);
    }
    else
    {
        CProcess_Clean* pClean=GET_PROCESS(PROCESS_CLEAN,
                                           SM_CLEAN,
                                           CProcess_Clean);
        Q_ASSERT(pClean);
        pparams[0]=pClean->m_Variants.get_Variant(Var_Clean_Arm_Spd);
        ui->widget->MParamInit_list(pparams,1);
    }
    mchildwidgets[intptr_t(ui->widget)]=ui->widget;
	ui->widget->hide();
}

void CMWParamCoatClean::MTimeFresh()
{
    CWorker* pWork=GET_CHDIRECT(M_WORKER,CWorker);
    if(m_iCurLevel!=pWork->m_workLevel)
    {
        m_iCurLevel=pWork->m_workLevel;
        ui->pushButton_del->setEnabled(m_iCurLevel>=LEVEL_ENGINEER);
        ui->pushButton_test->setEnabled(m_iCurLevel>=LEVEL_ENGINEER);
        ui->pushButton_Insert->setEnabled(m_iCurLevel>=LEVEL_ENGINEER);
    }
}

void CMWParamCoatClean::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
	if(m_bReadOnly){
		if(current&&m_itype==COAT_WIDGET){
			QString text = ui->tableWidget->item(current->row(), 0)->text();
			if (text == "COAT(Spiral)" || text == "COAT")
			{
				CProcess_Coat* pCoat=GET_PROCESS(PROCESS_COAT,
					SM_COAT, CProcess_Coat);
				COATCLEANSTEP& step = pCoat->m_vCoatCleanStep[current->row()];
				pCoat->m_Variants.get_VData(Var_Coat_Spay_Num_ID)->setValue(step.dSpayNum);
				pCoat->m_Variants.get_VData(Var_Coat_Spay_Spd_ID)->setValue(step.dSpaySpd);
				pCoat->m_Variants.get_VData(Var_Coat_M_CenterSpeed_ID)->setValue(step.dCenterSpeed);
				pCoat->m_Variants.get_VData(Var_Coat_M_EageSpeed_ID)->setValue(step.dEageSpeed);
				pCoat->m_Variants.get_VData(Var_Coat_M_CenterWaitTime_ID)->setValue(step.dCenterWaitTime);
				pCoat->m_Variants.get_VData(Var_Coat_Spay_Pre_Num_ID)->setValue(step.dCoatPreSpyNum);
				pCoat->m_Variants.get_VData(Var_Coat_Spay_Pre_WaitTime)->setValue(step.iCoatPreSpyWaitTime);

				ui->widget->MFresh();
				ui->widget->show();
			}else{
				ui->widget->hide();
			}
		}
		return;
	}
    if(previous && current)
    {
		if (m_itype == COAT_WIDGET)
		{
			CProcess_CoatClean* pCoatClean = GET_PROCESS(PROCESS_COAT,SM_COAT,CProcess_Coat);
			if (previous->row() < pCoatClean->m_vCoatCleanStep.size())
			{
				//应用涂覆的参数
				ui->widget->MApply();
				COATCLEANSTEP& step = pCoatClean->m_vCoatCleanStep[previous->row()];
				step.dSpayNum = pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Num_ID)->toDouble();
				step.dSpaySpd = pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Spd_ID)->toDouble();
				step.dCenterSpeed = pCoatClean->m_Variants.get_VData(Var_Coat_M_CenterSpeed_ID)->toDouble();
				step.dEageSpeed = pCoatClean->m_Variants.get_VData(Var_Coat_M_EageSpeed_ID)->toDouble();
				step.dCenterWaitTime = pCoatClean->m_Variants.get_VData(Var_Coat_M_CenterWaitTime_ID)->toDouble();
                step.dCoatPreSpyNum = pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Pre_Num_ID)->toDouble();
                step.iCoatPreSpyWaitTime = pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Pre_WaitTime)->toInt();
			}
		}
        if(previous->column()!=0)
        {
            CCtrlLineEdit* pLineEdit=
                    (CCtrlLineEdit*)ui->tableWidget->cellWidget(previous->row(),
                                                                previous->column());
            if(pLineEdit)
            {
                previous->setText(pLineEdit->text());
            }
        }
        else
        {
            CCtrlCombo* pCombo=
                    (CCtrlCombo*)ui->tableWidget->cellWidget(previous->row(),
                                                             previous->column());
            if(pCombo)
            {
                previous->setText(pCombo->currentText());
                previous->setData(Qt::UserRole,pCombo->currentData());
            }
        }
        ui->tableWidget->removeCellWidget(previous->row(),
                                          previous->column());
    }

    if(current&&!m_bReadOnly)
    {
        m_iSelectRow=current->row();

		if (m_itype == COAT_WIDGET)
		{
			CProcess_CoatClean* pCoatClean = GET_PROCESS(PROCESS_COAT,SM_COAT,CProcess_Coat);
			if (current->row() < pCoatClean->m_vCoatCleanStep.size())
			{
				COATCLEANSTEP& step = pCoatClean->m_vCoatCleanStep[current->row()];
				pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Num_ID)->setValue(step.dSpayNum);
				pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Spd_ID)->setValue(step.dSpaySpd);
				pCoatClean->m_Variants.get_VData(Var_Coat_M_CenterSpeed_ID)->setValue(step.dCenterSpeed);
				pCoatClean->m_Variants.get_VData(Var_Coat_M_EageSpeed_ID)->setValue(step.dEageSpeed);
				pCoatClean->m_Variants.get_VData(Var_Coat_M_CenterWaitTime_ID)->setValue(step.dCenterWaitTime);
				pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Pre_Num_ID)->setValue(step.dCoatPreSpyNum);
				pCoatClean->m_Variants.get_VData(Var_Coat_Spay_Pre_WaitTime)->setValue(step.iCoatPreSpyWaitTime);


				ui->widget->MFresh();
			}
		}

        if(current->column()!=0)
        {
            CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(current->text(),this);
            pLineEdit->SetRange(0,10000,false);
            ui->tableWidget->setCellWidget(current->row(),
                                           current->column(),
                                           pLineEdit);

			QString text = ui->tableWidget->item(current->row(), 0)->text();
			if (text != "COAT(Spiral)"
				&& text != "COAT")
			{
				ui->widget->hide();
			}else{
				ui->widget->show();
			}
        }
        else
        {
            CCtrlCombo* pCombo=new CCtrlCombo(this);
			connect(pCombo,&CCtrlCombo::currentTextChanged,[this](const QString & text){
				if (text != "COAT(Spiral)"
					&& text != "COAT")
				{
					if(m_iCurLevel<LEVEL_ADMINISTRATOR)
						ui->widget->hide();
					else
						ui->widget->show();
				}
				else
				{
					ui->widget->show();
				}
			});

            if(m_itype==COAT_WIDGET)
            {
                for(int i=1;i<MAXCOATTYPE;i++)
                {
                    pCombo->addItem(CoatName[i],i);
                }
            }
            else
            {
                for(int i=1;i<MAXCLEANTYPE;i++)
                {
                    pCombo->addItem(CleanName[i],i);
                }
            }
            pCombo->setCurrentText(current->text());
            ui->tableWidget->setCellWidget(current->row(),
                                           current->column(),
                                           pCombo);
			//TODO:根据状态设置后续几列是否能修改
        }
    }
}

void CMWParamCoatClean::on_pushButton_Insert_clicked()
{
    if(!m_gSystem.IsAllSMStopped())
    {
        return;
    }
    MApply();
    CProcess_CoatClean *pCoatClean=0;
    if(m_itype==COAT_WIDGET)
    {
        pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
                               CProcess_Coat);

    }
    else
    {
        pCoatClean=GET_PROCESS(PROCESS_CLEAN,SM_CLEAN,
                               CProcess_Clean);
    }
    QMMutexVector<COATCLEANSTEP> coatcleansteps=pCoatClean->m_vCoatCleanStep;

    COATCLEANSTEP  Step;
    int iSize=coatcleansteps.size();
    if (!OUTOFRANGE(m_iSelectRow,0,iSize))
    {
        coatcleansteps.insert(coatcleansteps.begin()+m_iSelectRow+1,Step);
    }
    else
    {
        coatcleansteps.push_back(Step);
    }
    pCoatClean->m_vCoatCleanStep=coatcleansteps;
    MFresh();
}

void CMWParamCoatClean::on_pushButton_del_clicked()
{
    if(!m_gSystem.IsAllSMStopped())
    {
        return;
    }
    CProcess_CoatClean *pCoatClean=0;
    if(m_itype==COAT_WIDGET)
    {
        pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
                               CProcess_Coat);

    }
    else
    {
        pCoatClean=GET_PROCESS(PROCESS_CLEAN,SM_CLEAN,
                               CProcess_Clean);
    }
    QMMutexVector<COATCLEANSTEP> coatcleansteps=pCoatClean->m_vCoatCleanStep;

    int iSize=coatcleansteps.size();
    int iIndex=m_iSelectRow;

    if (!OUTOFRANGE(iIndex,0,iSize))
    {
        coatcleansteps.erase(coatcleansteps.begin()+iIndex);
        pCoatClean->m_vCoatCleanStep=coatcleansteps;
        MFresh();
		ui->tableWidget->setCurrentItem(nullptr);
    }
}

void CMWParamCoatClean::on_pushButton_test_clicked()
{
    if(!m_gSystem.IsAllSMStopped())
    {
        return;
    }
    int iStart=ui->lineEdit_start->text().toInt();
    int iEnd=ui->lineEdit_end->text().toInt();
    QString str=QObject::trUtf8("Test Coat sig");
    int iSMID=SM_COAT;
    if(m_itype==CLEAN_WIDGET)
    {
        str=QObject::trUtf8("Test Clean sig");
        iSMID=SM_CLEAN;
    }
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, str,
                          QVariantList()<<NORMAL_ACTION<<iStart-1
                          <<iEnd-1,0);
    m_gSystem.start_test(iSMID, msgqueue,false);

}

void CMWParamCoatClean::showEvent(QShowEvent* event)
{
	CMWidget::showEvent(event);

	ui->widget->hide();
}
