#include "cmwparamside.h"
#include "ui_cmwparamside.h"
#include "logic.h"
#include "cctrllineedit.h"
#include "cmwparammodelinfo.h"
CMWParamSide::CMWParamSide(QWidget *parent, int iSideID) :
	CMWidget(parent),
	ui(new Ui::CMWParamSide)
{
	ui->setupUi(this);
	m_iSideID=iSideID;
	m_iSelectRow=-1;
}

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

void CMWParamSide::MInit()
{
	CProductSide* pSide=GET_CHDIRECT(m_iSideID,CProductSide);
	Q_ASSERT(pSide);
	Variant* pparams[10];
    pparams[0]=pSide->m_Variants.get_Variant(Var_ProductSide_SideStep);
    pparams[1]=pSide->m_Variants.get_Variant(Var_ProductSide_CutStreet);
//    pparams[2]=pSide->m_Variants.get_Variant(Var_ProductSide_StepDotLine);
//    pparams[3]=pSide->m_Variants.get_Variant(Var_ProductSide_SolidDotLine);
    pparams[2]=pSide->m_Variants.get_Variant(Var_ProductSide_InitAngle);
    pparams[3]=pSide->m_Variants.get_Variant(Var_ProductSide_TemplateOffsetX);
    pparams[4]=pSide->m_Variants.get_Variant(Var_ProductSide_TemplateOffsetY);

	if(pSide->m_iID == SIDE_A){
		CProcess_Level* pLevel=GET_PROCESS(PROCESS_LEVELING, SM_MAIN, CProcess_Level);
		pparams[5]=pLevel->m_Variants.get_Variant(Var_ProductSide_TemplateOffsetSideALowX);
		pparams[6]=pLevel->m_Variants.get_Variant(Var_ProductSide_TemplateOffsetSideALowY);
	}else{
		CProcess_Level* pLevel=GET_PROCESS(PROCESS_LEVELING, SM_MAIN, CProcess_Level);
		pparams[5]=pLevel->m_Variants.get_Variant(Var_ProductSide_TemplateOffsetSideBLowX);
		pparams[6]=pLevel->m_Variants.get_Variant(Var_ProductSide_TemplateOffsetSideBLowY);
	}

    ui->widget_Side->MParamInit_list(pparams,7);
	mchildwidgets[intptr_t(ui->widget_Side)]=ui->widget_Side;

	CMWParamModelInfo* pModelInfoA=new CMWParamModelInfo(this,CAM2X,m_iSideID);
	ui->horizontalLayout_2X->addWidget(pModelInfoA);
	mchildwidgets[intptr_t(pModelInfoA)]=pModelInfoA;

	CMWParamModelInfo* pModelInfoB=new CMWParamModelInfo(this,CAM6X,m_iSideID);
	ui->horizontalLayout_2X->addWidget(pModelInfoB);
	mchildwidgets[intptr_t(pModelInfoB)]=pModelInfoB;
}

void CMWParamSide::FreshSubIndex()
{
	CProductSide *pSide= GET_CHDIRECT(m_iSideID,CProductSide);
	Q_ASSERT(pSide);
	int iSize=pSide->m_vSubIndex.size();
	ui->tableWidget->clearContents();
	ui->tableWidget->setRowCount(iSize);
	for(int i=0;i<iSize;i++)
	{
		SUBINDEX&  SubIndex=pSide->m_vSubIndex[i];
		QTableWidgetItem* pitem=
                new QTableWidgetItem(QString::number(SubIndex.dSolidOffset*1000));
		ui->tableWidget->setItem(i,0,pitem);
        pitem=new QTableWidgetItem(QString::number(SubIndex.dSubIndex*1000));
		ui->tableWidget->setItem(i,1,pitem);
		pitem=new QTableWidgetItem(QString::number(SubIndex.iCount));
		ui->tableWidget->setItem(i,2,pitem);
        pitem=new QTableWidgetItem(QString::number(SubIndex.dSubIndexX*1000));
		ui->tableWidget->setItem(i,3,pitem);
        pitem=new QTableWidgetItem(QString::number(SubIndex.dSolidLine*1000));
		ui->tableWidget->setItem(i,4,pitem);
        pitem=new QTableWidgetItem(QString::number(SubIndex.dLocationOffset*1000));
        ui->tableWidget->setItem(i,3,pitem);

	}
}

void CMWParamSide::ApplySubIndex()
{
	CProductSide *pSide= GET_CHDIRECT(m_iSideID,CProductSide);
	Q_ASSERT(pSide);
	int iSize=ui->tableWidget->rowCount();
    pSide->m_vSubIndex.resize(iSize);
    on_tableWidget_currentItemChanged(0,ui->tableWidget->currentItem());
	for(int i=0;i<iSize;i++)
    {
		SUBINDEX&  SubIndex=pSide->m_vSubIndex[i];
		SubIndex.dSolidOffset=ui->tableWidget->item(i,0)->text()
                .toDouble()/1000.0;
		SubIndex.dSubIndex=ui->tableWidget->item(i,1)->text()
                .toDouble()/1000.0;
		SubIndex.iCount=ui->tableWidget->item(i,2)->text()
				.toDouble();
		SubIndex.dSubIndexX=ui->tableWidget->item(i,3)->text()
                .toDouble()/1000.0;
		SubIndex.dSolidLine=ui->tableWidget->item(i,4)->text()
                .toDouble()/1000.0;
        SubIndex.dLocationOffset=ui->tableWidget->item(i,3)->text()
                .toDouble()/1000.0;
    }
}

void CMWParamSide::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_add->setEnabled(m_iCurLevel>=LEVEL_ENGINEER);
        ui->pushButton_ABSwitch->setEnabled(m_iCurLevel>=LEVEL_ENGINEER);
    }
}

void CMWParamSide::MFresh()
{
	CMWidget::MFresh();

	bool bVisible = false;
    ui->pushButton_ABSwitch->setVisible(bVisible);
    ui->tableWidget->setVisible(bVisible);
    ui->pushButton_add->setVisible(bVisible);
    ui->pushButton_del->setVisible(bVisible);
    ui->pushButton_ABSwitch->setVisible(bVisible);

	if (!bVisible)
	{
		FreshSubIndex();
	}
}

void CMWParamSide::MApply()
{
	CMWidget::MApply();
	ApplySubIndex();
}

void CMWParamSide::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
	if(previous)
	{
		CCtrlLineEdit* pLineEdit=
				(CCtrlLineEdit*)ui->tableWidget->cellWidget(previous->row(),
															previous->column());
		if(pLineEdit)
		{
			previous->setText(pLineEdit->text());
		}

		ui->tableWidget->removeCellWidget(previous->row(),
										  previous->column());
	}

    if(current&&!m_bReadOnly)
	{
		m_iSelectRow=current->row();
        CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(current->text(),this);
        pLineEdit->SetRange(-10000,10000,true);
		ui->tableWidget->setCellWidget(current->row(),
									   current->column(),
									   pLineEdit);
	}
}

void CMWParamSide::on_pushButton_add_clicked()
{
	MApply();
	CProductSide *pSide= GET_CHDIRECT(m_iSideID,CProductSide);
	Q_ASSERT(pSide);
	int iSize=pSide->m_vSubIndex.size();
	SUBINDEX  SubIndex;
	if (iSize)
	{
		SubIndex=pSide->m_vSubIndex[iSize-1];
	}
	pSide->m_vSubIndex.push_back(SubIndex);
	MFresh();
}

void CMWParamSide::on_pushButton_del_clicked()
{
	CProductSide *pSide= GET_CHDIRECT(m_iSideID,CProductSide);
	Q_ASSERT(pSide);
	int iSize=pSide->m_vSubIndex.size();
	int iIndex=m_iSelectRow;

	if (!OUTOFRANGE(iIndex,0,iSize))
	{
		pSide->m_vSubIndex.erase(pSide->m_vSubIndex.begin()+iIndex);
		MFresh();
	}
}

void CMWParamSide::on_pushButton_ABSwitch_clicked()
{
	CProductSide *pSide= GET_CHDIRECT(m_iSideID,CProductSide);
	Q_ASSERT(pSide);
	int32_t iSideother=m_iSideID==SIDE_A?SIDE_B:SIDE_A;
	CProductSide *pSideother= GET_CHDIRECT(iSideother,CProductSide);
	Q_ASSERT(pSideother);
	pSideother->m_vSubIndex.swap(pSide->m_vSubIndex);
	MFresh();
}
