#include "cmwcomplaser.h"
#include "ui_cmwcomplaser.h"
#include "logic.h"
CMWCompLaser::CMWCompLaser(QWidget* parent) :
    CMWidget(parent),
    ui(new Ui::CMWCompLaser)
{
    ui->setupUi(this);
    picOn = QPixmap(CMakePath::make_bin_path("../theme/led_blue.png"));
    picOff = QPixmap(CMakePath::make_bin_path("../theme/led_red.png"));
    m_iTimerInterval = 50;
}

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

void CMWCompLaser::MInit()
{
#ifdef __USE_D_NARROW__
    ui->comboBox_LaserPath->addItem(m_gSystem.m_mapNameList[LASERPATH_D_NARROW], LASERPATH_D_NARROW);
#endif
#ifdef __USE_D_WIDE__
    ui->comboBox_LaserPath->addItem(m_gSystem.m_mapNameList[LASERPATH_D_WIDE], LASERPATH_D_WIDE);
#endif

#ifdef __USE_S_NARROW__
    ui->comboBox_LaserPath->addItem(m_gSystem.m_mapNameList[LASERPATH_S_NARROW], LASERPATH_S_NARROW);
#endif
#ifdef __USE_L_NARROW__
    ui->comboBox_LaserPath->addItem(m_gSystem.m_mapNameList[LASERPATH_L_NARROW], LASERPATH_L_NARROW);
#endif

    CLaser* pLaser = GET_CHDIRECT(LASER, CLaser);
    VariantMap::iterator ii = pLaser->m_Variants.m_mVariantMap.begin();
    //int iIndex=0;
    Variant* pparam[50] = { 0 };
    /*for(;ii!=pLaser->m_Variants.m_mVariantMap.end();ii++)
    {
    pparam[iIndex]=&(*ii);
    iIndex++;
    }*/
    pparam[0] = pLaser->m_Variants.get_Variant(Var_Laser_Freq_ID);
    //pparam[1]=pLaser->m_Variants.get_Variant(Var_Laser_Current_Ref_ID);
    pparam[1] = pLaser->m_Variants.get_Variant(Var_Laser_Current_Cur_ID);

    //pparam[2] = pLaser->m_Variants.get_Variant(Var_Laser_IntGate_ID);
    pparam[2] = pLaser->m_Variants.get_Variant(Var_Laser_LaserTime_ID);
    pparam[3] = pLaser->m_Variants.get_Variant(Var_Laser_LaserTime_Limit_ID);

    pparam[4] = pLaser->m_Variants.get_Variant(Var_Laser_Power_ID);
    pparam[5] = pLaser->m_Variants.get_Variant(Var_LaserUseTimeWarning);
    pparam[6] = pLaser->m_Variants.get_Variant(Var_LaserUseTimeErr);

    //pparam[7]=pLaser->m_Variants.get_Variant(Var_Laser_LaserTime_ID);

    ui->widget->MParamInit_list(pparam, 7);
    mchildwidgets[(uintptr_t)ui->widget] = ui->widget;
}
void CMWCompLaser::MFresh()
{
    CMWidget::MFresh();

    MTimeFresh();
    CWorker* pWorker = GET_CHDIRECT(M_WORKER, CWorker);
    if(pWorker->m_workLevel >= LEVEL_ENGINEER) {
        ui->btnLayserGate->setEnabled(true);
        ui->btnLayserExternGate->setEnabled(true);
    } else {
        ui->btnLayserGate->setEnabled(false);
        ui->btnLayserExternGate->setEnabled(false);
    }
}

static bool bset = false;
void CMWCompLaser::MTimeFresh()
{

    CMotorDriver* pMotorDriver = GET_CARD(MOTOR_DRIVER, CMotorDriver);
    Q_ASSERT(pMotorDriver);
    if(LASER_ON == pMotorDriver->get_iodata(IO_OUTPUT, OUT_LASERGATE, true)) {
        ui->pushButton_LaserOpen->setChecked(true);
        ui->pushButton_LaserClose->setChecked(false);
    } else {
        ui->pushButton_LaserOpen->setChecked(false);
        ui->pushButton_LaserClose->setChecked(true);
    }

    CLaserDriver_Base* pLaserDriver = GET_CARD(LASER_DRIVER, CLaserDriver_Base);
    if(pLaserDriver->m_bEmissionOn) {
        ui->lblIconPartsLayserEmissionOn->setPixmap(picOn);

        //ui->btnLayserEmission->setText("关闭");
    } else {
        ui->lblIconPartsLayserEmissionOn->setPixmap(picOff);
        //ui->btnLayserEmission->setText("打开");
    }

    if(pLaserDriver->m_bExternalGate) {
        ui->lblIconPartsLayserExteralGate->setPixmap(picOn);
    } else {
        ui->lblIconPartsLayserExteralGate->setPixmap(picOff);
    }

    if(pLaserDriver->m_bInternalGate) {
        ui->lblIconPartsLayserGate->setPixmap(picOn);
    } else {
        ui->lblIconPartsLayserGate->setPixmap(picOff);
    }

    ui->btnLayserEmission->setChecked(pLaserDriver->m_bEmissionOn);
    ui->btnLayserExternGate->setChecked(pLaserDriver->m_bExternalGate);
    ui->btnLayserGate->setChecked(pLaserDriver->m_bInternalGate);
    ui->lblPartsLayserEPRF->setText(QString::number(pLaserDriver->m_iEpulseFreq));
    ui->lblPartsLayserDiode->setText(QString::number(pLaserDriver->m_dDiode));
    ui->txtPartsLayserTemperature->display(pLaserDriver->m_dTemperature);
    ui->txtPartsLayserInnerPower->display(pLaserDriver->m_dInnerPower);

    CLaser* pLaser = (CLaser*)GET_CHDIRECT(LASER, CLaser);
    Q_ASSERT(pLaser);
    double electric = pLaser->m_Variants.get_VData(Var_Laser_Current_Cur_ID)->toDouble();
    ui->lblPartsLayserDiode->setText(QString::number(electric));
    double freq = pLaser->m_Variants.get_VData(Var_Laser_Freq_ID)->toDouble();
    ui->lblPartsLayserQSW->setText(QString::number(freq * 1000));

    ui->lblUserHour->setText(QString::number(pLaserDriver->m_dCurThgUseHours));

    CPowerMeterDriver* pPowerMeterDriver = GET_CARD(POWERMETERDRIVER, CPowerMeterDriver);
    CPowerMeter* pPowerMeter = GET_CHANNEL(POWERMETERHEAD, POWERMETERDRIVER,
                                           CPowerMeter);
    double dPower = pPowerMeter->m_Variants
        .get_VData(Var_PowerMeter_MeasResult_ID)
        ->toDouble();
    ui->label_Power->setText(QString::number(dPower, 'f', 3));

#ifndef __SIMULATOR__
    if(pPowerMeterDriver->m_Variants.get_VData(Var_LaserPower_Temperature)) {
        double dTemperature = pPowerMeterDriver->m_Variants.get_VData(Var_LaserPower_Temperature)->toDouble();
        ui->lblPowerHeadTemperature->setText(QString::number(dTemperature, 'f', 2));
    }
#endif

    if(pLaserDriver->m_bError) {
        ui->lblLayserStatus->setText("Error");
    } else if(pLaserDriver->m_bPreheat) {
        ui->lblLayserStatus->setText(QString("Preheat:%1 Min %2 S").arg(pLaserDriver->m_iPreheatTimeSecs / 60).arg(pLaserDriver->m_iPreheatTimeSecs % 60));
    } else {
        ui->lblLayserStatus->setText("Normal");
    }

    if(bset)return;
//    ui->lblPartsLayserQSW->setText();
    /*double diode = pLaser->
    ui->lblPartsLayserDiode->setText(QString::number());*/

    static int freshfreq = 20;
    static int freshcount = 0;
    freshcount++;
    if(freshcount % freshfreq) return;

    //if(ui->pushButton_direct_VaccCT->isChecked()!=bvaccumCoat)
    //    ui->pushButton_direct_VaccCT->setChecked(bvaccumCoat);

    //这里如果激光器没有打开，则会导致后面全部等待超时
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Current Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver_SP::GETSTATE
                          << LASER, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Current Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::GETCURRENT_CUR
                          << LASER, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Laser Time Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::GETLASERTIME
                          << LASER, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Diod Temp Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::GETDIODTEMP
                          << LASER, 0);


    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Laser Time Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver_SP::GETINNERPOWER
                          << LASER, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Laser Time Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::GETFREQ
                          << LASER, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Laser Time Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver_SP::GETEPULSEFREQ
                          << LASER, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Power fresh Sig"),
                          QVariantList()
                          << POWERMETERDRIVER
                          << CPowerMeterDriver::FRESHRESULT
                          << POWERMETERHEAD, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Power fresh Sig"),
                          QVariantList()
                          << POWERMETERDRIVER
                          << CPowerMeterDriver_lp::GET_TEMPERATURE
                          << POWERMETERHEAD, 0);




//    msgqueue.pushback_Msg(VarMsg_Driver_ID,
//                          QObject::trUtf8("Laser Get Laser Time Sig"),
//                          QVariantList()
//                          <<LASER_DRIVER<<CLaserDriver::GETFREQ
//                          <<LASER,0);

    /*msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get UV Power Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::GETUVPOWER
                          <<LASER,0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Diod Temp Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::GETDIODTEMP
                          <<LASER,0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Diod Temp ref Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::GETDIODTEMPREF
                          <<LASER,0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Thg Temp Adjust ref Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::GETTHGTEMPADJUSTREF
                          <<LASER,0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Thg Temp Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::GETTHGTEMP
                          <<LASER,0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Get Thg Temp ref Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::GETTHGTEMPREF
                          <<LASER,0);*/

    //m_gSystem.start_test(SM_MAIN, msgqueue,false);

    int iSMID = SM_IO;
    VS_Thread* m_pState = (VS_Thread*)m_gSystem.FindSM(iSMID)->m_pCurState;
    if(m_pState->QueueSize() < 150) {
        m_pState->appandMsgqueue(msgqueue);
    }
}

void CMWCompLaser::on_pushButton_Set_clicked()
{
    MApply();
    CLaser* pLaser = GET_CHDIRECT(LASER, CLaser);
    bset = true;

    double dFreq = pLaser->m_Variants.get_VData(Var_Laser_Freq_ID)->toDouble();
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set Freq Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::SETFREQ
                          << LASER << dFreq, 0);

    /*msgqueue.pushback_Msg(VarMsg_Driver_ID,
                            QObject::trUtf8("Laser Set Freq Sig"),
                            QVariantList()
                            <<LASER_DRIVER<<CLaserDriver::SETCURRENT_REF
                            <<LASER<<dFreq,0);*/

    /*double dPower=pLaser->m_Variants.get_VData(Var_Laser_Power_ID)->toDouble();
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set Power Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::SETPOWERRATE
                          <<LASER<<dPower,0);

    double dFpk=pLaser->m_Variants.get_VData(Var_Laser_FPKTime_ID)->toDouble();
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set Fpk Time Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::SETFPKTIME
                          <<LASER<<dFpk<<true,0);

    double dLDDTime=pLaser->m_Variants.get_VData(Var_Laser_LaserTime_ID)->toDouble();
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set Laser Time Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::SETLASERTIME
                          <<LASER<<dLDDTime,0);

    double dDiodTemp=pLaser->m_Variants.get_VData(Var_Laser_Diodtemp_ID)->toDouble();
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set Laser Time Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::SETDIODTEMP
                          <<LASER<<dDiodTemp,0);

    int iTHGTempAdjust=pLaser->m_Variants.get_VData(Var_Laser_thgtempadjust_ID)->toInt();
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set THG Temp Adjust Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::SETTHGTEMPADJUST
                          <<LASER<<iTHGTempAdjust,0);

    int iWorkMode=pLaser->m_Variants.get_VData(Var_Laser_workmode_ID)->toUInt();
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set Work Mode Sig"),
                          QVariantList()
                          <<LASER_DRIVER<<CLaserDriver::SETWORKMODE
                          <<LASER<<iWorkMode,0);*/

    m_gSystem.start_manual(SM_MAIN, msgqueue);

    bset = false;
}

void CMWCompLaser::on_pushButton_LaserOpen_clicked()
{
    int iLaserPath = ui->comboBox_LaserPath->currentData().toInt();

    V_MsgQueue msgqueue;
    if(iLaserPath == LASERPATH_D_WIDE) {
        msgqueue.pushback_Msg(VarMsg_Process_ID,
                              QObject::trUtf8("Move Power Cali Pos Sig"),
                              QVariantList()
                              << PROCESS_POWERCALI_D_WIDE
                              << CProcess_LaserPowerCali::MOVECALIPOS, 0);
    } else {
        msgqueue.pushback_Msg(VarMsg_Process_ID,
                              QObject::trUtf8("Move Power Cali Pos Sig"),
                              QVariantList()
                              << PROCESS_POWERCALI_D_NARROW
                              << CProcess_LaserPowerCali::MOVECALIPOS, 0);
    }

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Open Laser Gate Sig"),
                          QVariantList()
                          << LASERPATH_DRIVER << CLaserPathDriver::OPENSHUTTER
                          << iLaserPath << true, 0);

    //开光
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Open Laser Gate Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::OPENLASERGATE
                          << iLaserPath << true, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                            QObject::tr("Close Lens Protect Air"),
                            QVariantList() << LASERPATH_DRIVER << CLaserPathDriver::OPENLENSPROTECT 
                            << iLaserPath << false, 0);
    //关闭激光内外吹气
    /*msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Close Laser lens blow inner"),
                          QVariantList()
                          << MOTOR_DRIVER << CMotorDriver::SETIO
                          << IO_OUTPUT << OUT_LENS_Blow_Inner
                          << false, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Close Laser lens blow outer"),
                          QVariantList()
                          << MOTOR_DRIVER << CMotorDriver::SETIO
                          << IO_OUTPUT << OUT_LENS_Blow_Outter
                          << false, 0);*/

    m_gSystem.start_manual(SM_MAIN, msgqueue);

}

void CMWCompLaser::on_pushButton_LaserClose_clicked()
{
    int iLaserPath = ui->comboBox_LaserPath->currentData().toInt();

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Open Laser Gate Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::OPENLASERGATE
                          << iLaserPath << false, 0);

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Open Laser Gate Sig"),
                          QVariantList()
                          << LASERPATH_DRIVER << CLaserPathDriver::OPENSHUTTER
                          << iLaserPath << false, 0);

    m_gSystem.start_manual(SM_MAIN, msgqueue);
}

void CMWCompLaser::on_btnLayserEmission_clicked(bool checked)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Open Laser Gate Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::SETLDD
                          << LASER << checked, 0);
    m_gSystem.start_manual(SM_MAIN, msgqueue);

    if(false == checked) {
        CLaserDriver_SP* pLaser = GET_CARD(LASER_DRIVER, CLaserDriver_SP);
        pLaser->m_bEmissionOn = false;
    }
}

void CMWCompLaser::on_btnLayserGate_clicked(bool checked)
{
    CLaserDriver_Base* pLaser = GET_CARD(LASER_DRIVER, CLaserDriver_Base);
    pLaser->SetInnerGate(LASER, checked);
}

void CMWCompLaser::on_btnLayserExternGate_clicked(bool checked)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set Gate Source Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::SETGATESOURCE
                          << LASER << checked, 0);
    m_gSystem.start_manual(SM_MAIN, msgqueue, true);
}

void CMWCompLaser::on_txtPartsLayserDiode_returnPressed()
{
    bool ok = false;
    double diode = ui->txtPartsLayserDiode->text().toDouble(&ok);
    if(!ok)return;
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set REF Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::SETCURRENT_REF
                          << LASER << diode, 0);
    m_gSystem.start_manual(SM_MAIN, msgqueue, true);

}

void CMWCompLaser::on_txtPartsLayserQSW_returnPressed()
{
    bool ok = false;
    double freq = ui->txtPartsLayserQSW->text().toDouble(&ok);
    if(!ok)return;
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set Freq Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver::SETFREQ
                          << LASER << freq / 1000, 0);
    m_gSystem.start_manual(SM_MAIN, msgqueue, true);
}

void CMWCompLaser::on_txtPartsLayserEPRF_returnPressed()
{
    bool ok = false;
    double freq = ui->txtPartsLayserEPRF->text().toInt(&ok);
    if(!ok)return;
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Laser Set EPRF Sig"),
                          QVariantList()
                          << LASER_DRIVER << CLaserDriver_SP::SETEPULSEFREQ
                          << LASER << freq, 0);
    m_gSystem.start_manual(SM_MAIN, msgqueue, true);
}

void CMWCompLaser::on_btnPartsLayserToMeasure_clicked()
{
	
    V_MsgQueue msgqueue;
	int iLaserPath = ui->comboBox_LaserPath->currentData().toInt();
	int iProcessCaliPath = -1;
	if (iLaserPath == LASERPATH_D_WIDE || iLaserPath == LASERPATH_L_NARROW) {
		iProcessCaliPath = PROCESS_POWERCALI_D_WIDE;
	}
	else if(iLaserPath == LASERPATH_D_NARROW || iLaserPath == LASERPATH_S_NARROW) {
		iProcessCaliPath = PROCESS_POWERCALI_D_NARROW;
	}

	if (iLaserPath != -1) {

		msgqueue.pushback_Msg(VarMsg_Process_ID,
			QObject::trUtf8("Move Power Cali Pos Sig"),
			QVariantList()
			<< iProcessCaliPath
			<< CProcess_LaserPowerCali::MOVECALIPOS, 0);
		m_gSystem.start_manual(SM_MAIN, msgqueue, true);
	}
}

void CMWCompLaser::on_btnPowerMeterZero_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Set power meter ZERO"),
                          QVariantList()
                          << POWERMETERDRIVER
                          << CPowerMeterDriver_lp::ZERO_POWERMETER
                          << POWERMETERHEAD, 0);
    m_gSystem.start_manual(SM_MAIN, msgqueue);
}
