#include "cmwauto.h"
#include "ui_cmwauto.h"
#include "logic.h"
CMWAuto::CMWAuto(QWidget* parent) :
    CMWidget(parent),
    ui(new Ui::CMWAuto)
{
    ui->setupUi(this);
    m_NA = tr("N/A");
    m_iTimerInterval = 50;


    m_dCurCoatTime = 0;
    m_dCurCleanTime = 0;
}

CMWAuto::~CMWAuto()
{
    delete ui;
}
void CMWAuto::MInit()
{
    mchildwidgets[(uintptr_t)ui->widget_Cass] = ui->widget_Cass;

    //QTimer* timer = new QTimer(this);
    //connect(timer,&QTimer::timeout,[this](){

    //	
    //	//if (++nAngle>360)
    //	//	nAngle = 0;
    //});
    //timer->start(200);
}

void CMWAuto::MTimeFresh()
{
    CMWidget::MTimeFresh();

    CMProduct* pProduct = GET_CHDIRECT(PRODUCT, CMProduct);
    if (int(m_dThickDiff_cur * 1000) != int(pProduct->m_dThickDiff * 1000))
    {
        m_dThickDiff_cur = pProduct->m_dThickDiff;
        ui->lineEdit_ThickDiff->setText(QString::number(m_dThickDiff_cur, 'f', 3));
    }
    if (m_gSystem.m_iCurSideID == SIDE_A ||
        m_gSystem.m_iCurSideID == SIDE_B)
    {
        m_iCurSideID = m_gSystem.m_iCurSideID;
        ui->label_Side->setText(m_gSystem.m_mapNameList[m_iCurSideID]);
        CProductSide* pSide = GET_CHDIRECT(m_iCurSideID, CProductSide);
        double dStep = pSide->m_Variants.get_VData(Var_ProductSide_SideStep)
            ->toDouble();
        ui->label_Step->setNum(dStep);
        int iCurLayer = m_gSystem.m_iCurLayer;
        SINGLECUTPARAM param;
        bool bflag = pSide->GetCutParam(iCurLayer, param);
        if (bflag)
        {
            ui->label_Depth->setNum(param.dCutDepth);
        }
        if (m_iCurLineID != m_gSystem.m_iCurLine)
        {
            m_iCurLineID = m_gSystem.m_iCurLine;
            int isizeLine = pSide->m_CutLineVector.size();
            ui->label_CutCount->setText(QString::fromUtf8("%1/%2")
                                        .arg(m_iCurLineID + 1)
                                        .arg(isizeLine));
            ui->label_LineTime->setText
            (QString::number(m_gSystem.m_lineTime.GetTime(),
                             'f', 3));
        }
        ui->label_CellTime->setText(
            m_gSystem.m_cellTime.FormatTime());
    }
    CProcess_Coat* pCoat = GET_PROCESS(PROCESS_COAT,
                                       SM_COAT, CProcess_Coat);
    Q_ASSERT(pCoat);
    if (pCoat->m_iActionType != m_iCurCoatActionType ||
        pCoat->m_iCurStep != m_iCurCoatStep)
    {
        m_iCurCoatActionType = pCoat->m_iActionType;
        m_iCurCoatStep = pCoat->m_iCurStep;
        COATCLEANSTEP coatstep;
        bool b = pCoat->GetCoatCleanStep(pCoat->m_iActionType, pCoat->m_iCurStep, coatstep);
        if (b)
        {
            int iSize = pCoat->m_vCoatCleanStep.size();
            if (pCoat->m_iActionType == CLEANTABLE_ACTION)
            {
                iSize = pCoat->m_vCleanTableStep.size();
            }
            else if (pCoat->m_iActionType == CLEANCOATFLUID_ACTION)
            {
                iSize = pCoat->m_vCleanTableRing.size();
            }
            QString str = QString::fromUtf8("%1(%3/%2)")
                .arg(pCoat->GetActionName(coatstep.iType))
                .arg(iSize).arg(pCoat->m_iCurStep + 1);
            ui->label_CoatStep->setText(str);
            ui->label_CoatSpeed->setNum(coatstep.iRPM);
            m_dCurCoatTime = coatstep.dTime;
        }
    }
    //CCoatVolumeSensor* pVolumeSensor=GET_CARD(COATVOLUME_SENSOR,CCoatVolumeSensor);
    //ui->lblCoatVolume->setText(QString::number(pCoat->m_Variants.get_VData(Var_Process_Coat_TotalVolume)->toDouble()));

    //CProcess_Coat* pCoat=GET_PROCESS(PROCESS_COAT,SM_COAT,CProcess_Coat);

    ui->lblCoatVolume->setText(QString("%1/%2").arg(QString::number(pCoat->m_dTotalBump, 'f', 1)).arg(QString::number(pCoat->m_dVolumeActual, 'f', 1)));

    ui->label_CoatTime->setText(QString::fromUtf8("%1/%2")
                                .arg(pCoat->m_COAT_CLEAN_TIME.GetTime(), 0, 'f', 1)
                                .arg(m_dCurCoatTime, 0, 'f', 1));
    ui->lblTotalCoatTime->setText(m_gSystem.m_totalCoatTime.FormatTime());

    CProcess_Clean* pClean = GET_PROCESS(PROCESS_CLEAN,
                                         SM_CLEAN, CProcess_Clean);
    Q_ASSERT(pClean);
    if (pClean->m_iActionType != m_iCurCleanActionType ||
        pClean->m_iCurStep != m_iCurCleanStep)
    {
        m_iCurCleanActionType = pClean->m_iActionType;
        m_iCurCleanStep = pClean->m_iCurStep;
        COATCLEANSTEP cleanstep;
        bool b = pClean->GetCoatCleanStep(m_iCurCleanActionType, m_iCurCleanStep, cleanstep);
        if (b)
        {
            int iSize = pClean->m_vCoatCleanStep.size();
            if (pClean->m_iActionType == CLEANTABLE_ACTION)
            {
                iSize = pClean->m_vCleanTableStep.size();
            }
            QString str = QString::fromUtf8("%1(%3/%2)")
                .arg(pClean->GetActionName(cleanstep.iType))
                .arg(iSize)
                .arg(pClean->m_iCurStep + 1);
            ui->label_CleanStep->setText(str);
            ui->label_CleanSpeed->setNum(cleanstep.iRPM);
            m_dCurCleanTime = cleanstep.dTime;
        }
    }
    ui->label_CleanTime->setText(QString::fromUtf8("%1/%2")
                                 .arg(pClean->m_COAT_CLEAN_TIME.GetTime(), 0, 'f', 1)
                                 .arg(m_dCurCleanTime, 0, 'f', 1));
    ui->lblTotalCleanTime->setText(m_gSystem.m_totalCleanTime.FormatTime());

    ui->widget_rtPower->m_dPowerMax = 10;
    //TODO:
    //CLaserPathDriver_Epulse* pLaserPathDriver =GET_CARD(LASERPATH_DRIVER,CLaserPathDriver_Epulse);

    //ui->widget_rtPower->m_dPowerCur = pLaserPathDriver->m_dCurPower;

    CLaserDriver_Base* pLaserDriver = GET_CARD(LASER_DRIVER, CLaserDriver_Base);
#ifdef __SIMULATOR__
    pLaserDriver->m_dCurThgUseHours = 3000.5;
#endif
    ui->lblUseTime->setText(QString("%3 H").arg(pLaserDriver->m_dCurThgUseHours, 3));
#ifdef __SIMULATOR__
    ui->widget_rtPower->m_dPowerCur = 1.5;
#endif // __SIMULATOR__
    ui->widget_rtPower->MTimeFresh();
    ui->lblPicese->setText(QString("%1").arg(m_gSystem.m_iTotalCuttedWafer));
    if (m_gSystem.m_iTotalCuttedWafer == 0 || (m_gSystem.m_totalWorkTime.GetTime() < 10 && m_gSystem.m_totalWorkHour < 0.001))
    {
        ui->lblUph->setText(QString("%1 P/H").arg("N/A"));
    }
    else
    {
        double dHour = m_gSystem.m_totalWorkTime.GetTime() / 3600 + m_gSystem.m_totalWorkHour;
        ui->lblUph->setText(QString("%1 P/H").arg(QString::number(m_gSystem.m_iTotalCuttedWafer / dHour, 'f', 1)));
    }

    ui->lblTotalCutTime->setText(m_gSystem.m_fullAutoTime.FormatTime());

    CCoatVolumeSensor* pVolumeSensor = GET_CARD(COATVOLUME_SENSOR, CCoatVolumeSensor);
    QString sVolumeInfo = QString("%1 ml").arg(pVolumeSensor->m_dGrams, 0, 'f', 1);

    double dAlarmVolume = pVolumeSensor->m_Variants.get_VData(Var_CoatVolume_AlarmVolume)->toDouble();
    double dWarningVolume = pVolumeSensor->m_Variants.get_VData(Var_CoatVolume_WarningVolume)->toDouble();

    ui->widget_rtHeightSensor->m_dCurVolume = pVolumeSensor->m_dGrams;
    ui->widget_rtHeightSensor->m_dVolumeAlarm = dAlarmVolume;
    ui->widget_rtHeightSensor->m_dVolumeWarning = dWarningVolume;
    ui->widget_rtHeightSensor->MTimeFresh();

    //Flush Spindle Status
    CMotor* motorE = GET_CHDIRECT(MOTORE_COAT_SPINDLE, CMotor);
    CMotor* motorF = GET_CHDIRECT(MOTORF_CLEAN_SPINDLE, CMotor);
    //实际回圆角度及状态
    CProcess_Clean* processClean = GET_PROCESS(PROCESS_CLEAN, SM_CLEAN, CProcess_Clean);
    CProcess_Coat* processCoat= GET_PROCESS(PROCESS_COAT, SM_COAT, CProcess_Coat);

    double iAngleInitE = processCoat->m_Variants.get_VData(Var_Coat_Spindle_InitPos_ID)->toDouble();
    double iAngleInitF = processClean->m_Variants.get_VData(Var_Clean_Spindle_InitPos_ID)->toDouble();

    int iAngleE = (long)motorE->m_Variants.get_VData(Var_Motor_Pos_ID)->toDouble() % 360;
    int iAngleF = (long)motorF->m_Variants.get_VData(Var_Motor_Pos_ID)->toDouble() % 360;
    iAngleE -= iAngleInitE;
    iAngleF -= iAngleInitF;
    ui->labCoat_Img->setAngle(iAngleE);
    ui->labClean_Img->setAngle(iAngleF);

    CProcess_Trans* transProcess = GET_PROCESS(PROCESS_TRANS,
                                               SM_TRANS, CProcess_Trans);
    if (transProcess->m_pSlotCoat) {
        ui->labCoat_Img->SetWafer(true);
    }
    else {
        ui->labCoat_Img->SetWafer(false);
    }
    if (transProcess->m_pSlotClean) {
        ui->labClean_Img->SetWafer(true);
    }
    else {
        ui->labClean_Img->SetWafer(false);
    }

    VS_Thread* m_pState = (VS_Thread*)m_gSystem.FindSM(SM_IO)->m_pCurState;
    if (m_pState->QueueSize() < 10)
    {
        m_pState->pushback_Msg(VarMsg_Driver_ID, QObject::trUtf8("Fresh Pos"),
                               QVariantList()
                               << MOTOR_DRIVER << CMotorDriver::FRESHPOS
                               << MOTORE_COAT_SPINDLE, 0);
        m_pState->pushback_Msg(VarMsg_Driver_ID, QObject::trUtf8("Fresh Pos"),
                               QVariantList()
                               << MOTOR_DRIVER << CMotorDriver::FRESHPOS
                               << MOTORF_CLEAN_SPINDLE, 0);

    }

    updateStatus(SM_TRANS, ui->lblTransState);
    updateStatus(SM_COAT, ui->labCoat_state);
    updateStatus(SM_CLEAN, ui->labClean_state);
    updateStatus(SM_MAIN, ui->lblWorkState);
}

void CMWAuto::MFresh()
{
    CMWidget::MFresh();
    ui->pushButton_SetThickDiff->setVisible(false);
    ui->label_11->setVisible(false);
    ui->lineEdit_ThickDiff->setVisible(false);
    ui->label_27->setVisible(false);
    update();
    CMProduct* pProduct = GET_CHDIRECT(PRODUCT, CMProduct);
    m_dThickDiff_cur = pProduct->m_dThickDiff;
    ui->lineEdit_ThickDiff->setText(QString::number(m_dThickDiff_cur, 'f', 3));
}
#include <QMessageBox>
void CMWAuto::on_pushButton_SetThickDiff_clicked()
{
    CMProduct* pProduct = GET_CHDIRECT(PRODUCT, CMProduct);
    double dDiff = ui->lineEdit_ThickDiff->text().toDouble();
    if (dDiff > -0.1 && dDiff < 0.1)
    {
        pProduct->m_dThickDiff = dDiff;
    }
    else
    {
        QMessageBox::warning(this,
                             QObject::trUtf8("SetThickDiff"),
                             QObject::trUtf8("Set Number between -0.1mm and 0.1mm"),
                             QMessageBox::Cancel);
    }
}

void CMWAuto::updateStatus(int iVsmId, QLabel* pLabel)
{
    VSM_Thread* pSMMain = m_gSystem.FindSM(iVsmId);
    uint32_t iStatusMain = 0;
    pSMMain->GetStatus(iStatusMain);
    if (iStatusMain & EStatus::kIDLE)
    {
        pLabel->setStyleSheet("border:1px;background-color:rgb(2,2,2);color:white");
        pLabel->setText("Ready");
    }

    if (iStatusMain & EStatus::kSemi_Paused || iStatusMain & EStatus::kAuto_Paused)
    {
        pLabel->setStyleSheet("border:1px;background-color:yellow;color:black");
        if (iStatusMain & EStatus::kSemi_Paused && iStatusMain & EStatus::kAuto_Paused)
        {
            pLabel->setText("Auto|Semi Pause");
        }
        else if (iStatusMain & EStatus::kSemi_Paused)
        {
            pLabel->setText("Semi Pause");
        }
        else
        {
            pLabel->setText("Auto Pause");
        }
    }

    if (iStatusMain & EStatus::kSemi_Running || iStatusMain & EStatus::kAuto_Runing)
    {
        pLabel->setStyleSheet("border:1px;background-color:rgb(0,255,0);color:black");
        if (iStatusMain & EStatus::kAuto_Runing)
        {
            pLabel->setText("Auto Work");
        }
        else if (iStatusMain & EStatus::kSemi_Running)
        {
            pLabel->setText("Semi Work");
        }

    }

    if (iStatusMain & EStatus::kAutoWaitTask)
    {
        pLabel->setStyleSheet("border:1px;background-color:rgb(255,255,197);color:black");
        pLabel->setText("WaitTask");
    }
}
