#include "cmwcamlight.h"
#include "ui_cmwcamlight.h"
#include "logic.h"
CMWCamLight::CMWCamLight(QWidget *parent, int iCamID, bool bSetDefaultProperty) :
    CMWidget(parent),
    ui(new Ui::CMWCamLight)
{
    ui->setupUi(this);
    m_iCamID=iCamID;
    m_bSetDefaultProperty=false;
    ui->horizontalScrollBar_DLight->setRange(0,255);
    ui->horizontalScrollBar_SLight->setRange(0,255);
    connect(ui->horizontalScrollBar_DLight,SIGNAL(MouseRelease()),this,
            SLOT(on_horizontalScrollBar_DLight_MouseRelease()));
    connect(ui->horizontalScrollBar_SLight,SIGNAL(MouseRelease()),this,
            SLOT(on_horizontalScrollBar_SLight_MouseRelease()));


    connect(ui->scrollBarExposure,SIGNAL(MouseRelease(int)),this,
            SLOT(on_scrollBar_Exposure_MouseRelease(int)));
    connect(ui->scrollBarGain,SIGNAL(MouseRelease(int)),this,
            SLOT(on_scrollBar_Gain_MouseRelease(int)));

}

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

void CMWCamLight::SetLight(int dlight, int slight)
{
    SetDlight(dlight);
    SetSlight(slight);

    SetLightData(dlight,slight);
}

void CMWCamLight::SetCamInfo(int exposure, int gain)
{
	V_MsgQueue msgqueue;
    m_gSystem.SetCamParam_sig(m_iCamID,exposure,gain,msgqueue);
	m_gSystem.start_manual(SM_MAIN, msgqueue,false);

    SetCamData(exposure, gain);
}

void CMWCamLight::GetCamInfo(int &exposure, int &gain)
{
    exposure=ui->scrollBarExposure->value();
    gain=ui->scrollBarGain->value();
}

void CMWCamLight::SetCamData(int exposure, int gain)
{
	ui->scrollBarExposure->setValue(exposure);
	ui->scrollBarGain->setValue(gain);
}

void CMWCamLight::MTimeFresh()
{
    CWorker* pWorker=GET_CHDIRECT(M_WORKER,CWorker);
    if(m_iCurLevel!=pWorker->m_workLevel)
    {
        m_iCurLevel=pWorker->m_workLevel;
        ui->pushButton_SaveAsDefault->setEnabled(m_iCurLevel>1);
    }


}

void CMWCamLight::SetDefaultLight()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    int DLight= pCam->m_Variants.get_VData(Var_Cam_LightDirect_default_ID)
            ->toInt();
    int SLight= pCam->m_Variants.get_VData(Var_Cam_LightSide_default_ID )
            ->toInt();

    SetLight(DLight,SLight);
}

void CMWCamLight::SetCamDefault()
{
	CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
	Q_ASSERT(pCam);
	int exposure = pCam->m_Variants.get_VData(Var_Cam_Exposure_default_ID)
		->toInt();
	int gain= pCam->m_Variants.get_VData(Var_Cam_Gain_default_ID )
		->toInt();

	ui->scrollBarExposure->setValue(exposure);
	ui->scrollBarGain->setValue(gain);
}

void CMWCamLight::MFresh()
{
	//设置光源
	CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
	Q_ASSERT(pCam);
	int DLight= pCam->m_Variants.get_VData(Var_Cam_LightDirect_default_ID)
		->toInt();
	int SLight= pCam->m_Variants.get_VData(Var_Cam_LightSide_default_ID )
		->toInt();
	SetLightData(DLight, SLight);

	//设置曝光
	int exposure = pCam->m_Variants.get_VData(Var_Cam_Exposure_ID)
		->toInt();
	int gain= pCam->m_Variants.get_VData(Var_Cam_Gain_ID )
		->toInt();
	SetCamData(exposure, gain);
}

void CMWCamLight::SetLightData(int dlight, int slight)
{
    ui->horizontalScrollBar_DLight->setValue(dlight);
    ui->horizontalScrollBar_SLight->setValue(slight);
}

void CMWCamLight::SetDlight(int ilight)
{
    ilight=max(0,ilight);
    ilight=min(255,ilight);
//    ui->horizontalScrollBar_DLight->setValue(ilight);
    V_MsgQueue msgqueue;
    m_gSystem.SetCamLight_sig(m_iCamID,ilight,true,m_bSetDefaultProperty,msgqueue);
    m_gSystem.start_manual(SM_MAIN, msgqueue,false);
//    MqDebug()<<"CMWCamLight::SetDlight";
}

void CMWCamLight::SetSlight(int ilight)
{
    ilight=max(0,ilight);
    ilight=min(255,ilight);
//    ui->horizontalScrollBar_SLight->setValue(ilight);
    V_MsgQueue msgqueue;
    m_gSystem.SetCamLight_sig(m_iCamID,ilight,false,m_bSetDefaultProperty,msgqueue);
    m_gSystem.start_manual(SM_MAIN, msgqueue,false);
}

void CMWCamLight::on_horizontalScrollBar_DLight_MouseRelease( )
{
    int dlight=ui->horizontalScrollBar_DLight->value();
    SetDlight(dlight);
}

void CMWCamLight::on_horizontalScrollBar_SLight_MouseRelease( )
{
    int slight=ui->horizontalScrollBar_SLight->value();

    SetSlight(slight);
}



void CMWCamLight::GetLightData(int &dlight, int &slight)
{
    dlight=ui->horizontalScrollBar_DLight->value();
    slight=ui->horizontalScrollBar_SLight->value();
}


void CMWCamLight::on_pushButton_SaveAsDefault_clicked()
{
    int dlight, slight;
    GetLightData(dlight,slight);

    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    pCam->m_Variants.get_VData(Var_Cam_LightDirect_default_ID)->setValue(dlight);
    pCam->m_Variants.get_VData(Var_Cam_LightSide_default_ID)->setValue(slight);

    int exposure,gain;
    GetCamInfo(exposure, gain);
    pCam->m_Variants.get_VData(Var_Cam_Exposure_default_ID)->setValue(exposure);
    pCam->m_Variants.get_VData(Var_Cam_Gain_default_ID)->setValue(gain);

    m_gSystem.write_config(true);

    QString path=CMakePath::make_data_path(m_gSystem.GetCurFile());

    QFile file(path);
    if(!file.open(QIODevice::WriteOnly))
    {
        MqDebug()<<QObject::trUtf8("Can not open file."
                                      "file path:%1").arg(path);
        return;
    }
    /*QDataStream stream(&file);
    m_gSystem.Serialize(stream,true);
    ConfigStreamWriter stream(&file);
    m_gSystem.WriteRecipe(stream, 0);
    file.close();
	*/

}

void CMWCamLight::on_pushButton_RestoreDefault_clicked()
{
	m_bSetDefaultProperty = true;
    SetDefaultLight();
	m_bSetDefaultProperty = false;
	SetCamDefault();
}

void CMWCamLight::on_scrollBar_Exposure_MouseRelease(int value )
{
    int exposure =ui->scrollBarExposure->value();
	int gain =ui->scrollBarGain->value();

	V_MsgQueue msgqueue;
	m_gSystem.SetCamParam_sig(m_iCamID, exposure, gain,msgqueue);
	m_gSystem.start_manual(SM_MAIN, msgqueue,true);
}

void CMWCamLight::on_scrollBar_Gain_MouseRelease(int value)
{
	int exposure =ui->scrollBarExposure->value();
	int gain =ui->scrollBarGain->value();
	V_MsgQueue msgqueue;
	m_gSystem.SetCamParam_sig(m_iCamID,exposure,gain ,msgqueue);
	m_gSystem.start_manual(SM_MAIN, msgqueue,true);
}



void CMWCamLight::on_btnHightLight_clicked()
{
    SetLight(30, 250);
    SetCamInfo(15000, 0);
}
