#include "cmwcalilaserpath.h"
#include "ui_cmwcalilaserpath.h"
#include "common_include.h"
#include "cctrllineedit.h"
CMWCaliLaserPath::CMWCaliLaserPath(QWidget* parent, int iLaserPathID) :
    CMWidget(parent),
    ui(new Ui::CMWCaliLaserPath)
{
    ui->setupUi(this);
    m_iLaserPathID = iLaserPathID;
    Q_ASSERT(m_iLaserPathID == LASERPATH_D_NARROW 
             || m_iLaserPathID == LASERPATH_D_WIDE 
             || m_iLaserPathID == LASERPATH_S_NARROW 
             || m_iLaserPathID == LASERPATH_L_NARROW);

}


static void hide_layout_child(QLayout* pLayout, bool bhide)
{
    if(!pLayout)return;
    int isize = pLayout->count();
    for(int i = 0; i < isize; i++) {
        QLayoutItem* pitem = pLayout->itemAt(i);
        if(pitem->widget()) {
            if(bhide) {
                pitem->widget()->hide();//setHidden(true);
            } else {
                pitem->widget()->show();//setHidden(false);
            }
            hide_layout_child(pitem->widget()->layout(), bhide);
        } else if(pitem->layout()) {
            hide_layout_child(pitem->layout(), bhide);
        }
    }

}

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

void CMWCaliLaserPath::MInit()
{
    if(m_iLaserPathID == LASERPATH_D_NARROW) {
        ui->lineEdit_SIndex->setEnabled(true);
    } else {
        ui->lineEdit_SIndex->setEnabled(false);
    }
    CLaserPath* pPath = GET_CHDIRECT(m_iLaserPathID,
                                     CLaserPath);
    Q_ASSERT(pPath);
#ifdef __USE_S_NARROW__
    Variant* pparam[21] = { 0 };
#else
	Variant* pparam[15] = { 0 };
#endif
    int ID[20] = { 0 };
    int index = 0;
    int iIndexG5 = -1;
    int iIndexG6 = -1;
    int iIndexFixPowerAngle = -1;
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_HairOffsetThres_ID);
    pparam[index++] = pPath->m_Variants.get_Variant(Var_Process_LaserOffset);

    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_PowerMaxAngle_ID);
    /*pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_UseFixGripper);
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_FixGripperPos);*/
    if(m_iLaserPathID == LASERPATH_D_WIDE || m_iLaserPathID == LASERPATH_L_NARROW) {
        pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_UseFixGripper);
        pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_FixGripperPos);
    } 
    if(m_iLaserPathID == LASERPATH_D_NARROW || m_iLaserPathID == LASERPATH_S_NARROW) {
        pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_UseFixSetting);
        iIndexG5 = index;
        pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_Angle1);
        iIndexG6 = index;
        pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_Angle2);
    }
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_UseFixPowerSetting);
    iIndexFixPowerAngle = index;
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_PowerSet);
    

    /*
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_PChangeAngle_ID);
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_DIndexZeroAngle_ID);
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_DIndexMax_ID);
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_5050ZeroAngle_ID);
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_5050CalcuStep_ID);
    */

    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_UseFixFreq);
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_FixFreqValue);
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_UseFixEpulse);
    pparam[index++] = pPath->m_Variants.get_Variant(Var_LaserPath_FixEpulseValue);

    CLaserPathDriver* pPathDriver = GET_CARD(LASERPATH_DRIVER, CLaserPathDriver);
    pparam[index++] = pPathDriver->m_Variants.get_Variant(Var_GateStartDelay);
    pparam[index++] = pPathDriver->m_Variants.get_Variant(Var_GateEndDelay);
    if (m_iLaserPathID == LASERPATH_D_WIDE || m_iLaserPathID == LASERPATH_L_NARROW) {
		ID[2] = MOTORG2_POWER | (CMotorDriver::ABSMOVE_TYPE << 16);
        if (iIndexFixPowerAngle != -1)
        {
            ID[iIndexFixPowerAngle] = MOTORG2_POWER | (CMotorDriver::ABSMOVE_TYPE << 16);
        }
	}
    if (m_iLaserPathID == LASERPATH_D_NARROW || m_iLaserPathID == LASERPATH_S_NARROW) {
        ID[2] = MOTORG2_POWER | (CMotorDriver::ABSMOVE_TYPE << 16);
        if (iIndexG5 != -1)
        {
            ID[iIndexG5] = MOTORG5_NARROWINDEX | (CMotorDriver::ABSMOVE_TYPE << 16);
        }
        if (iIndexG6 != -1)
        {
            ID[iIndexG6] = MOTORG6_NARROW5050 | (CMotorDriver::ABSMOVE_TYPE << 16);
        }
        if (iIndexFixPowerAngle != -1)
        {
            ID[iIndexFixPowerAngle] = MOTORG2_POWER | (CMotorDriver::ABSMOVE_TYPE << 16);
        }
    } 
    ui->widget->MParamInit_list(pparam, index, ID);
    mchildwidgets[(uintptr_t)ui->widget] = ui->widget;
}

void CMWCaliLaserPath::MFresh()
{
    CMWidget::MFresh();
    if(LASERPATH_D_WIDE == m_iLaserPathID || LASERPATH_L_NARROW == m_iLaserPathID
       || LASERPATH_S_NARROW == m_iLaserPathID)
    {
        hide_layout_child(ui->gridLayout, true);
        hide_layout_child(ui->gridLayout_2, true);
    }
    else
    {
#ifdef __LOWK_SOLUTION__
        MFresh5050table();
        MFreshOffsetTable();
#else
        hide_layout_child(ui->gridLayout, true);
        hide_layout_child(ui->gridLayout_2, true);
#endif
    }

    ui->pushButton_Calculate->setHidden(true);
}

void CMWCaliLaserPath::MApply()
{
#ifdef __USE_S_NARROW__
    MApply5050table();
    MApplyOffsetTable();
#endif
    CMWidget::MApply();
}


void CMWCaliLaserPath::MFresh5050table()
{
    CLaserPath* pPath = GET_CHDIRECT(m_iLaserPathID, CLaserPath);
    Q_ASSERT(pPath);
    int iSize = pPath->m_vDIndexParam.size();
    ui->tableWidget_5050->clearContents();
    ui->tableWidget_5050->setRowCount(iSize);
    for(int i = 0; i < iSize; i++) {
        QTableWidgetItem* pItem = 0;
        QPair<int, DIndexParam>& pairAngle = pPath->m_vDIndexParam[i];
        pItem = new QTableWidgetItem(QString::number(pairAngle.first / 1000.0, 'f', 3));
        ui->tableWidget_5050->setItem(i, 0, pItem);


        pItem = new QTableWidgetItem(
            QString::number(pairAngle.second.dAngle1,
                            'f', 3));
        //pItem = new QTableWidgetItem(QString::number(0,'f', 3));
        ui->tableWidget_5050->setItem(i, 1, pItem);

        pItem = new QTableWidgetItem(
            QString::number(pairAngle.second.dAngle2,
                            'f', 3));
        ui->tableWidget_5050->setItem(i, 2, pItem);
    }
}

void CMWCaliLaserPath::MFreshOffsetTable()
{
    CLaserPath* pPath = GET_CHDIRECT(m_iLaserPathID,
                                     CLaserPath);
    Q_ASSERT(pPath);
    int iSize = pPath->m_vOffset.size();
    ui->tableWidget_Offset->clearContents();
    ui->tableWidget_Offset->setRowCount(iSize);
    for(int i = 0; i < iSize; i++) {
        QTableWidgetItem* pItem = 0;
        QPair<CMPathIndex, double>& pairOffset = pPath->m_vOffset[i];
        pItem = new QTableWidgetItem(
            QString::number(double(pairOffset.first.iFirstIndex) / 1000.0, 'f', 3));
        ui->tableWidget_Offset->setItem(i, 0, pItem);
        pItem = new QTableWidgetItem(
            QString::number(double(pairOffset.first.iSecondIndx / 1000.0), 'f', 3));
        ui->tableWidget_Offset->setItem(i, 1, pItem);
        pItem = new QTableWidgetItem(
            QString::number(pairOffset.second,
                            'f', 3));
        ui->tableWidget_Offset->setItem(i, 2, pItem);
    }
}

void CMWCaliLaserPath::MApplyParam()
{
    CMWidget::MApply();
}

void CMWCaliLaserPath::MApply5050table()
{
    CLaserPath* pPath = GET_CHDIRECT(m_iLaserPathID,
                                     CLaserPath);
    Q_ASSERT(pPath);
    int iSize = ui->tableWidget_5050->rowCount();
    pPath->m_vDIndexParam.resize(iSize);
    on_tableWidget_5050_currentItemChanged(0, ui->tableWidget_5050->currentItem());
    for(int i = 0; i < iSize; i++) {
        QPair<int, DIndexParam>& pairAngle = pPath->m_vDIndexParam[i];
        QTableWidgetItem* pItem = ui->tableWidget_5050->item(i, 0);
        pairAngle.first = pItem->text().toDouble() * 1000;

        pItem = ui->tableWidget_5050->item(i, 1);
        pairAngle.second.dAngle1 = pItem->text().toDouble();

        pItem = ui->tableWidget_5050->item(i, 2);
        pairAngle.second.dAngle2 = pItem->text().toDouble();
    }
    ui->tableWidget_5050->setCurrentCell(0, 0);
}

void CMWCaliLaserPath::MApplyOffsetTable()
{
    CLaserPath* pPath = GET_CHDIRECT(m_iLaserPathID,
                                     CLaserPath);
    Q_ASSERT(pPath);

    int iSize = ui->tableWidget_Offset->rowCount();
    pPath->m_vOffset.resize(iSize);

    for(int i = 0; i < iSize; i++) {
        QPair<CMPathIndex, double>  pairOffset = pPath->m_vOffset[i];
        QTableWidgetItem* pItem = ui->tableWidget_Offset->item(i, 0);
        pairOffset.first.iFirstIndex = int(pItem->text().toDouble() * 1000);

        pItem = ui->tableWidget_Offset->item(i, 1);
        pairOffset.first.iSecondIndx = int(pItem->text().toDouble() * 1000);

        pItem = ui->tableWidget_Offset->item(i, 2);
        pairOffset.second = pItem->text().toDouble();
    }
}

void CMWCaliLaserPath::on_tableWidget_5050_currentItemChanged(QTableWidgetItem* current,
                                                              QTableWidgetItem* previous)
{
    if(current) {
        //if(current->column()==1)
        {
            CCtrlLineEdit* pLineEdit =
                new CCtrlLineEdit(current->text(), ui->tableWidget_5050);
            pLineEdit->SetRange(-10000, 10000, true);
            ui->tableWidget_5050->setCellWidget(
                current->row(),
                current->column(),
                pLineEdit);
        }
    }
    if(previous) {
        //if(previous->column()==1)
        {
            CCtrlLineEdit* pLineEdit =
                (CCtrlLineEdit*)ui->tableWidget_5050->cellWidget(
                    previous->row(), previous->column());
            if(pLineEdit) {
                previous->setText(pLineEdit->text());
            }
        }
        ui->tableWidget_5050->removeCellWidget(
            previous->row(), previous->column());
    }
}

void CMWCaliLaserPath::on_tableWidget_Offset_cellClicked(int row,
                                                         int column)
{
    UNUSED(column);
    QTableWidgetItem* pItem = ui->tableWidget_Offset->item(row, 0);
    if(pItem) {
        ui->lineEdit_DIndex->setText(pItem->text());
    }
    pItem = ui->tableWidget_Offset->item(row, 1);
    if(pItem) {
        if(m_iLaserPathID == LASERPATH_D_WIDE) {
            ui->lineEdit_SIndex->setText(pItem->text());
        } else {
            ui->lineEdit_SIndex->setText("0");
        }

    }
    pItem = ui->tableWidget_Offset->item(row, 2);
    if(pItem) {
        ui->lineEdit_Offset->setText(pItem->text());
    }
    m_iCurSelOffset = row;
}
#include <QMessageBox>
void CMWCaliLaserPath::on_pushButton_ADD_clicked()
{
    MApplyParam();
    CLaserPath* pPath = GET_CHDIRECT(m_iLaserPathID,
                                     CLaserPath);
    Q_ASSERT(pPath);
    double dOffset = ui->lineEdit_Offset->text().toDouble();
    double dThres = pPath->m_Variants.get_VData(Var_LaserPath_HairOffsetThres_ID)
        ->toDouble();
    if(fabs(dOffset) > fabs(dThres)) {
        QMessageBox::warning(this, tr("Set Offset"),
                             tr("Must not set offset bigger then hairline threshold"),
                             QMessageBox::Cancel);
        return;
    }

#ifdef __LOWK_SOLUTION__
    if (pPath->m_iID == LASERPATH_D_NARROW)
    {
        pPath->SetOffset(ui->lineEdit_DIndex->text().toDouble(),
                         ui->lineEdit_SIndex->text().toDouble(),
                         dOffset);
        MFreshOffsetTable();
    }
    /*else
    {
        pPath->SetOffset(dOffset);
    }*/
#endif
}

void CMWCaliLaserPath::on_pushButton_Delete_clicked()
{
    CLaserPath* pPath = GET_CHDIRECT(m_iLaserPathID,
                                     CLaserPath);
    Q_ASSERT(pPath);
    int iIndex = m_iCurSelOffset;
    int iSize = pPath->m_vOffset.size();
    if(OUTOFRANGE(iIndex, 0, iSize)) {
        return;
    }
    pPath->m_vOffset.erase(pPath->m_vOffset.begin() + iIndex);
    MFreshOffsetTable();
}
