#include "cmwparamcutside.h"
#include "ui_cmwparamcutside.h"
#include "logic.h"
#include <QTableWidgetItem>
#include "cctrllineedit.h"
#include "cctrlcombo.h"
CMWParamCutSide::CMWParamCutSide(QWidget* parent, int32_t iSideID, bool bhidebtn) :
    CMWidget(parent),
    ui(new Ui::CMWParamCutSide)
{
    ui->setupUi(this);
    m_iSideID = iSideID;
    m_iSelectRow = -1;
    ui->tableWidget->setEditTriggers(QTableWidget::NoEditTriggers);
    m_bHideBtn = bhidebtn;
    if(bhidebtn) {
        ui->gridLayout->removeWidget(ui->pushButton_add);
        ui->gridLayout->removeWidget(ui->pushButton_copy);
        ui->gridLayout->removeWidget(ui->pushButton_del);
        ui->gridLayout->removeWidget(ui->pushButton_test);
        delete ui->pushButton_add;
        delete ui->pushButton_copy;
        delete ui->pushButton_del;
        delete ui->pushButton_test;
        ui->pushButton_add = 0;
        ui->pushButton_copy = 0;
        ui->pushButton_del = 0;
        ui->pushButton_test = 0;
        /* ui->tableWidget->horizontalHeader()->
        setSectionResizeMode(QHeaderView::Stretch);*/

        ui->tableWidget->horizontalHeader()->
            setSectionResizeMode(QHeaderView::Interactive);
    }

    //ui->tableWidget->setColumnHidden(6, true);
    //ui->tableWidget->setColumnHidden(7, true);
    //ui->tableWidget->setColumnHidden(8, true);

    ui->tableWidget->setColumnWidth(0, 100);
    //ui->tableWidget->resizeColumnsToContents();
}

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

void CMWParamCutSide::MFresh()
{
    CProductSide* pSide = GET_CHDIRECT(m_iSideID, CProductSide);
    Q_ASSERT(pSide);
    int iSize = pSide->m_vCutParam.size();
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(iSize);
    for(int i = 0; i < iSize; i++) {
        SINGLECUTPARAM& CutParam = pSide->m_vCutParam[i];
        QTableWidgetItem* pitem =
            new QTableWidgetItem(QString::fromUtf8("Pass%1").arg(i + 1));

        ui->tableWidget->setItem(i, 0, pitem);
        pitem = new QTableWidgetItem(QString::number(CutParam.dCutDepth));

        ui->tableWidget->setItem(i, 1, pitem);
        pitem = new QTableWidgetItem(QString::number(CutParam.dOvercut));

        ui->tableWidget->setItem(i, 2, pitem);
        pitem = new QTableWidgetItem(QString::number(CutParam.dCutSpd));

        ui->tableWidget->setItem(i, 3, pitem);
        pitem = new QTableWidgetItem(QString::number(CutParam.dLaserPower));
        ui->tableWidget->setItem(i, 4, pitem);

        pitem = new QTableWidgetItem(QString::number(CutParam.iLaserFreq));
        ui->tableWidget->setItem(i, 5, pitem);

        pitem = new QTableWidgetItem(QString::number(CutParam.iLaserEpulse));
        ui->tableWidget->setItem(i, 6, pitem);

        pitem = new QTableWidgetItem(m_gSystem.m_mapNameList[CutParam.laserpathID]);
        pitem->setData(Qt::UserRole, CutParam.laserpathID);

        ui->tableWidget->setItem(i, 7, pitem);
        pitem = new QTableWidgetItem(QString::number(CutParam.dDIndex));
        ui->tableWidget->setItem(i, 8, pitem);
        if(CutParam.laserpathID == LASERPATH_S_NARROW || CutParam.laserpathID == LASERPATH_L_NARROW) {
            pitem = new QTableWidgetItem("-");
            ui->tableWidget->setItem(i, 9, pitem);
        } else {
            pitem = new QTableWidgetItem(QString::number(CutParam.dBeamWidth, 'f', 3));
            ui->tableWidget->setItem(i, 9, pitem);
        }
//#else
//        if (CutParam.laserpathID == LASERPATH_D_NARROW)
//        {
//            pitem = new QTableWidgetItem("-");
//            ui->tableWidget->setItem(i, 9, pitem);
//        }
//        else
//        {
//            pitem = new QTableWidgetItem(QString::number(CutParam.dBeamWidth, 'f', 3));
//            ui->tableWidget->setItem(i, 9, pitem);
//        }
//#endif

        pitem = new QTableWidgetItem(QString::number(CutParam.iCutCount));
        ui->tableWidget->setItem(i, 10, pitem);


        pitem = new QTableWidgetItem(QString::number(CutParam.dOffset));
        ui->tableWidget->setItem(i, 11, pitem);
    }


    ui->tableWidget->setColumnHidden(8, true);
    if(nullptr != ui->pushButton_test)
        ui->pushButton_test->setVisible(false);
}

void CMWParamCutSide::MApply()
{
    CProductSide* pSide = GET_CHDIRECT(m_iSideID, CProductSide);
    Q_ASSERT(pSide);
    int iSize = ui->tableWidget->rowCount();
    pSide->m_vCutParam.resize(iSize);
    on_tableWidget_currentItemChanged(0, ui->tableWidget->currentItem());

    for(int i = 0; i < iSize; i++) {
        SINGLECUTPARAM& CutParam = pSide->m_vCutParam[i];
        CutParam.dCutDepth = ui->tableWidget->item(i, 1)->text()
            .toDouble();
        CutParam.dOvercut = ui->tableWidget->item(i, 2)->text()
            .toDouble();
        CutParam.dCutSpd = ui->tableWidget->item(i, 3)->text()
            .toDouble();
        CutParam.dLaserPower = ui->tableWidget->item(i, 4)->text()
            .toDouble();
        CutParam.iLaserFreq = ui->tableWidget->item(i, 5)->text()
            .toDouble();
        CutParam.iLaserEpulse = ui->tableWidget->item(i, 6)->text()
            .toDouble();
        CutParam.laserpathID = ui->tableWidget->item(i, 7)->data(Qt::UserRole).toInt();
        CutParam.dDIndex = ui->tableWidget->item(i, 8)->text()
            .toDouble();
        if(CutParam.laserpathID == LASERPATH_S_NARROW) {
            CutParam.dBeamWidth = 0;
        }
        else if (CutParam.laserpathID == LASERPATH_L_NARROW)
        {
            CutParam.dBeamWidth = 0;
        }
        else {
            CutParam.dBeamWidth = ui->tableWidget->item(i, 9)->text()
                .toDouble();
        }
        CutParam.iCutCount = ui->tableWidget->item(i, 10)->text()
            .toDouble();

        CutParam.dOffset = ui->tableWidget->item(i, 11)->text()
            .toDouble();

    }
}

void CMWParamCutSide::MTimeFresh()
{
    CWorker* pWork = GET_CHDIRECT(M_WORKER, CWorker);
    if(m_iCurLevel != pWork->m_workLevel && !m_bHideBtn) {
        m_iCurLevel = pWork->m_workLevel;
        ui->pushButton_del->setEnabled(m_iCurLevel >= 2);
        ui->pushButton_test->setEnabled(m_iCurLevel >= 2);
        ui->pushButton_copy->setEnabled(m_iCurLevel >= 2);
        ui->pushButton_add->setEnabled(m_iCurLevel >= 2);
    }
}


void CMWParamCutSide::on_pushButton_add_clicked()
{
    MApply();
    CProductSide* pSide = GET_CHDIRECT(m_iSideID, CProductSide);
    Q_ASSERT(pSide);
    int iSize = pSide->m_vCutParam.size();
    SINGLECUTPARAM  CutParam;
    if(iSize) {
        CutParam = pSide->m_vCutParam[iSize - 1];
    }
    pSide->m_vCutParam.push_back(CutParam);
    MFresh();
}

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

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

int const g_iColRowIndex = 0;
int const g_iColIndexLaserPath = 7;
int const g_iColIndexBeamWidth = 9;
void CMWParamCutSide::on_tableWidget_currentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous)
{
    if(previous) {
        if(previous->column() != g_iColIndexLaserPath &&
           previous->column() != g_iColRowIndex &&
           previous->column() != g_iColIndexBeamWidth) {
            CCtrlLineEdit* pLineEdit =
                (CCtrlLineEdit*)ui->tableWidget->cellWidget(previous->row(),
                                                            previous->column());
            if(pLineEdit) {
                previous->setText(pLineEdit->text());
            }

        } else if(previous->column() == g_iColIndexLaserPath) {
            CCtrlCombo* pCombo =
                (CCtrlCombo*)ui->tableWidget->cellWidget(previous->row(),
                                                         previous->column());
            if(pCombo) {
                previous->setText(pCombo->currentText());
                previous->setData(Qt::UserRole, pCombo->currentData());
            }
        } else if(previous->column() == g_iColIndexBeamWidth) {
            CCtrlCombo* pCombo =
                (CCtrlCombo*)ui->tableWidget->cellWidget(previous->row(),
                                                         previous->column());
            if(pCombo) {
                previous->setText(pCombo->currentText());
            }
        }
        ui->tableWidget->removeCellWidget(previous->row(),
                                          previous->column());
    }

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

        if(current->column() != g_iColIndexLaserPath &&
           current->column() != g_iColRowIndex &&
           current->column() != g_iColIndexBeamWidth) {
            CCtrlLineEdit* pLineEdit = new CCtrlLineEdit(current->text(), this);
            pLineEdit->SetRange(-10000, 10000, true);
            ui->tableWidget->setCellWidget(current->row(),
                                           current->column(),
                                           pLineEdit);
        } else if(current->column() == g_iColIndexLaserPath) {
            //光路切换
            CCtrlCombo* pCombo = new CCtrlCombo(this);
            int iComboIndex = 0;
            std::map<int, int> pathIndex;
#ifdef __USE_D_NARROW__
            pathIndex[LASERPATH_D_NARROW] = iComboIndex++;
            pCombo->addItem(m_gSystem.m_mapNameList[LASERPATH_D_NARROW], LASERPATH_D_NARROW);
#endif

#ifdef __USE_S_NARROW__
            pathIndex[LASERPATH_S_NARROW] = iComboIndex++;
            pCombo->addItem(m_gSystem.m_mapNameList[LASERPATH_S_NARROW], LASERPATH_S_NARROW);
#endif
#ifdef __USE_L_NARROW__
            pathIndex[LASERPATH_L_NARROW] = iComboIndex++;
            pCombo->addItem(m_gSystem.m_mapNameList[LASERPATH_L_NARROW], LASERPATH_L_NARROW);
#endif
#ifdef __USE_D_WIDE__
            //TODO:光路切换
            pathIndex[LASERPATH_D_WIDE] = iComboIndex++;
            pCombo->addItem(m_gSystem.m_mapNameList[LASERPATH_D_WIDE], LASERPATH_D_WIDE);
#endif
            int idata = current->data(Qt::UserRole).toInt();
            int iColIndex = 0;
            if (pathIndex.find(idata) != pathIndex.end())
            {
                iColIndex = pathIndex[idata];
            }
            /*switch(idata) {
                case LASERPATH_D_NARROW:
                    iColIndex = 0;
                    break;
                case LASERPATH_S_NARROW:
                    iColIndex = 1;
                    break;
                case LASERPATH_L_NARROW:
                    iColIndex = 2;
                    break;
                case LASERPATH_D_WIDE:
                    iColIndex = 3;
                    break;
                default:
                    break;
            }*/
            pCombo->setCurrentIndex(iColIndex);
            ui->tableWidget->setCellWidget(current->row(),
                                           current->column(),
                                           pCombo);
        } else if(current->column() == g_iColIndexBeamWidth) {
            CProductSide* pSide = GET_CHDIRECT(m_iSideID, CProductSide);
            Q_ASSERT(pSide);
            int iSize = pSide->m_vCutParam.size();
            int iIndex = current->row();

            if(!OUTOFRANGE(iIndex, 0, iSize)) {
                
                //int iLaserPath = pSide->m_vCutParam[iIndex].laserpathID;
                int iLaserPath = ui->tableWidget->item(iIndex, g_iColIndexLaserPath)->data(Qt::UserRole).toInt();
                if(iLaserPath == LASERPATH_D_WIDE) {
                    CLaserPath* pLaserPath = GET_CHDIRECT(LASERPATH_D_WIDE, CLaserPath);
                    LPATHBEAMPOSMAP::iterator ii = pLaserPath->m_mBeamPos.begin();
                    CCtrlCombo* pCombo = new CCtrlCombo(this);
                    for(; ii != pLaserPath->m_mBeamPos.end(); ii++) {
                        pCombo->addItem(QString::number(ii.key() / 1000.0, 'f', 3));
                    }
                    ui->tableWidget->setCellWidget(current->row(),
                                                   current->column(),
                                                   pCombo);
                    pCombo->setCurrentText(current->text());
                } else if(iLaserPath == LASERPATH_D_NARROW) {
//#ifdef __LOWK_SOLUTION__
                    CLaserPath* pLaserPath = GET_CHDIRECT(LASERPATH_D_NARROW, CLaserPath);
                    auto ii = pLaserPath->m_vDIndexParam.begin();
                    CCtrlCombo* pCombo = new CCtrlCombo(this);
                    for(; ii != pLaserPath->m_vDIndexParam.end(); ii++) {
                        pCombo->addItem(QString::number(ii->first / 1000.0, 'f', 3));
                    }
                    ui->tableWidget->setCellWidget(current->row(), current->column(), pCombo);
                    pCombo->setCurrentText(current->text());
//#else
/*                    ui->tableWidget->item(iIndex, g_iColIndexBeamWidth)->setText("-");
#endif     */
                } else if(iLaserPath == LASERPATH_S_NARROW) {
                    ui->tableWidget->item(iIndex, g_iColIndexBeamWidth)->setText("-");
                }
                else if (iLaserPath == LASERPATH_L_NARROW)
                {
                    ui->tableWidget->item(iIndex, g_iColIndexBeamWidth)->setText("-");
                }
            }
        }
    }
}

void CMWParamCutSide::on_pushButton_test_clicked()
{

}

void CMWParamCutSide::on_pushButton_copy_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_vCutParam = pSide->m_vCutParam;

    CMWidget* pParent = (CMWidget*)parent();
    if(pParent) {
        pParent->MFresh();
    }
}
