#include "cmwcalitablecleaning.h"
#include "ui_cmwcalitablecleaning.h"
#include "logic.h"
#include "cctrlcombo.h"
CMWCaliTableCleaning::CMWCaliTableCleaning(QWidget *parent, int itype) :
    CMWidget(parent),
    ui(new Ui::CMWCaliTableCleaning)
{
    ui->setupUi(this);
    m_itype=itype;
    m_iSelectRow=-1;
}

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

void CMWCaliTableCleaning::MInit()
{

    CMWidget::MInit();
}

void CMWCaliTableCleaning::MFresh()
{
    CMWidget::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_vCleanTableStep;
    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);
    }
}

void CMWCaliTableCleaning::MApply()
{
    CMWidget::MApply();
    QMMutexVector<COATCLEANSTEP> coatcleansteps;
    int isize=ui->tableWidget->rowCount();
    coatcleansteps.resize(isize);
    on_tableWidget_currentItemChanged(0,ui->tableWidget->currentItem());
    for(int i=0;i<isize;i++)
    {
        COATCLEANSTEP& step=coatcleansteps[i];
        step.iType=ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt();
        step.dTime=ui->tableWidget->item(i,1)->text().toDouble();
        step.iRPM=ui->tableWidget->item(i,2)->text().toInt();
    }
    CProcess_CoatClean *pCoatClean=0;
    QString filePath;
    if(m_itype==COAT_WIDGET)
    {
        pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
                               CProcess_Coat);


        filePath=CMakePath::make_config_path(PATH_CLEANTABLE_COAT);
    }
    else
    {
        pCoatClean=GET_PROCESS(PROCESS_CLEAN,SM_CLEAN,
                               CProcess_Clean);

        filePath = CMakePath::make_config_path(PATH_CLEANTABLE_CLEAN);
    }
    pCoatClean->m_vCleanTableStep=coatcleansteps;
    try{
        pCoatClean->ReadWriteCleanTable(true);
    }catch(QString& ex){
        m_gSystem.m_SysMsg.SetMsg(ex,NOTIFY_MSG_TYPE);
    }

}

void CMWCaliTableCleaning::MTimeFresh()
{

}

void CMWCaliTableCleaning::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
    if(previous)
    {
        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(current->column()!=0)
        {
            CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(current->text(),this);
            pLineEdit->SetRange(0,1500,false);
            ui->tableWidget->setCellWidget(current->row(),
                                           current->column(),
                                           pLineEdit);
        }
        else
        {
            CCtrlCombo* pCombo=new CCtrlCombo(this);
            if(m_itype==COAT_WIDGET)
            {
                for(int i=1;i<MAXCOATTYPE;i++)
                {
                    QString s=CoatName[i];
                    if(s!="AIR"&&s!="SPIN")
                    {
                        continue;
                    }
                    pCombo->addItem(CoatName[i],i);
                }
            }
            else
            {
                for(int i=1;i<MAXCLEANTYPE;i++)
                {
                    QString s=CleanName[i];
                    if(s!="AIR"&&s!="SPIN" )
                    {
                        continue;
                    }
                    pCombo->addItem(CleanName[i],i);
                }
            }
            pCombo->setCurrentText(current->text());
            ui->tableWidget->setCellWidget(current->row(),
                                           current->column(),
                                           pCombo);
        }
    }
}

void CMWCaliTableCleaning::on_pushButton_Insert_clicked()
{
    if(!m_gSystem.IsAllSMStopped())
    {
        return;
    }
    MApply();
    CProcess_CoatClean *pCoatClean=0;
    COATCLEANSTEP  Step;
    if(m_itype==COAT_WIDGET)
    {
        pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
                               CProcess_Coat);
        Step.iType=DRY;
    }
    else
    {
        pCoatClean=GET_PROCESS(PROCESS_CLEAN,SM_CLEAN,
                               CProcess_Clean);
        Step.iType=AIR;
    }
    QMMutexVector<COATCLEANSTEP> coatcleansteps=pCoatClean->m_vCleanTableStep;

    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_vCleanTableStep=coatcleansteps;
    MFresh();
}

void CMWCaliTableCleaning::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_vCleanTableStep;

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

    if (!OUTOFRANGE(iIndex,0,iSize))
    {
        coatcleansteps.erase(coatcleansteps.begin()+iIndex);
        pCoatClean->m_vCleanTableStep=coatcleansteps;
        MFresh();
    }
}

void CMWCaliTableCleaning::on_pushButton_Clean_clicked()
{
    if(!m_gSystem.IsAllSMStopped())
    {
        m_gSystem.m_SysMsg.SetMsg
                (QObject::trUtf8("Stop action before test move"));
        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()<<CLEANTABLE_ACTION<<iStart-1
                          <<iEnd-1,0);
    m_gSystem.start_test(iSMID, msgqueue);
}
