#include "stable.h"
#include "cmwviewtrans.h"

CMWViewTrans::CMWViewTrans(QWidget *parent)
	: CMWidget(parent)
{
    ui = new Ui::CMWViewTrans();
    ui->setupUi(this);
	m_iTimerInterval = 100;
}


CMWViewTrans::~CMWViewTrans()
{

}

void CMWViewTrans::MTimeFresh()
{
	MFresh();

    //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->lblStatusTrans);
    updateStatus(SM_COAT, ui->lblStatusCoat);
    updateStatus(SM_CLEAN, ui->lblStatusClean);
    updateStatus(SM_MAIN, ui->lblStatusCutting);
    updateStatus(SM_WTLoad, ui->lblStatusLoad);
    updateStatus(SM_WTUnload, ui->lblStatusUnload);
}

void CMWViewTrans::MFresh()
{
    FreshWaferStatus();
}

void CMWViewTrans::FreshWaferStatus()
{
    CProcess_Trans* pTrans = GET_PROCESS(PROCESS_TRANS, SM_TRANS, CProcess_Trans);

    if (pTrans->m_pSlotCheckBox) {
        ui->lblCupboardWafer->setVisible(true);
    } else {
        ui->lblCupboardWafer->setVisible(false);
    }

    if (pTrans->m_pSlotRailNear) {
        ui->lblRailNearWafer->setVisible(true);
        ui->lblRailNear->setVisible(false);
    } else {
        ui->lblRailNearWafer->setVisible(false);
        ui->lblRailNear->setVisible(true);
    }

    if (pTrans->m_pSlotRailFar) {
        ui->lblRailFarWafer->setVisible(true);
        ui->lblRailFar->setVisible(false);
    } else {
        ui->lblRailFarWafer->setVisible(false);
        ui->lblRailFar->setVisible(true);
    }

    if (pTrans->m_pSlotUnLoad) {
        ui->lblLoad->setVisible(true);
    } else {
        ui->lblLoad->setVisible(false);
    }

    if (pTrans->m_pSlotTrans) {
        ui->lblTransWafer->setVisible(true);
    } else {
        ui->lblTransWafer->setVisible(false);
    }

    if (pTrans->m_pSlotLoad) {
        ui->lblUnload->setVisible(true);
    } else {
        ui->lblUnload->setVisible(false);
    }

    if (pTrans->m_pSlotWT) {
        ui->lblCutWafer->setVisible(true);
    } else {
        ui->lblCutWafer->setVisible(false);
    }

    if (pTrans->m_pSlotCoat) {
        ui->lblCoatWafer->setVisible(true);
    } else {
        ui->lblCoatWafer->setVisible(false);
    }

    if (pTrans->m_pSlotClean) {
        ui->lblCleanWafer->setVisible(true);
    } else {
        ui->lblCleanWafer->setVisible(false);
    }
}

void CMWViewTrans::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");
    }

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

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

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