#include "claserpath_thorlab.h"
#include "logic.h"

CLaserPath_Thorlab::CLaserPath_Thorlab(const QString &name, int32_t id)
    : CLaserPath(name, id)
{
	m_Variants.add_Variant(Var_LaserPath_PowerHomeOffsetAngle,
		QObject::trUtf8("PowerHomeOffsetAngle"),int(0),false,
		Variant::TOXML);
	m_Variants.add_Variant(Var_LaserPath_Homed,
		QObject::trUtf8("LaserPathHomed"),int(0),false,
		Variant::TOXML);
	m_Variants.add_Variant(Var_LaserPath_InitAngle,
		QObject::trUtf8("LaserPathInitAngle"),int(0),false,
		Variant::TOXML);
	//m_Variants.add_Variant(Var_LaserPath_AdjsutRate,
	//	QObject::trUtf8("LaserPathAdjustRate"),double(1),false,
	//	Variant::TOXML);
}

int CLaserPath_Thorlab::ReadConfig(QDomElement &root)
{
	int iIndex=CChannel::ReadConfig(  root);
	return iIndex;
}


bool CLaserPath_Thorlab::FindPowerPosVector(int iFreq, double dSIndex,
	POWERPOSVECTOR &vector)
{
	Q_UNUSED(dSIndex);

	int iSize=m_PowerCali.size();
	if(iSize <= 0){
		return false;
	}
	for (int i=0;i<iSize;i++)
	{
		if (iFreq==m_PowerCali[i].first)
		{
			vector=m_PowerCali[i].second;
			return true;
		}
	}
	return false;
}

void CLaserPath_Thorlab::WritePowerPosMap()
{
	QString strPath=
		CMakePath::make_config_path1(m_sName+"_LaserCaliDataThorlab.ldf");
	QFile file(strPath);
	if(!file.open(QIODevice::WriteOnly))
	{
		MQT_THROW_STR(QObject::trUtf8("open path %1 failed")
			.arg(strPath));
	}
	QDataStream stream(&file);
	int iSize=m_PowerCali.size();
	stream<<iSize;
	CALIMAP_FREQ::iterator ii;
	for (ii=m_PowerCali.begin();ii!=m_PowerCali.end();ii++)
	{
		stream<<ii->first;
		POWERPOSVECTOR& ParamVector=ii->second;
		int iSizeReal=ParamVector.size();
		stream<<iSizeReal;
		for (int i=0;i<iSizeReal;i++)
		{
			stream<<ParamVector[i].dIndex;
			stream<<ParamVector[i].dPower;
			stream<<ParamVector[i].dPower_RT;
		}
	}
}

void CLaserPath_Thorlab::ReadPowerPosMap()
{
	QString strPath=
		CMakePath::make_config_path1(m_sName+"_LaserCaliDataThorlab.ldf");
	QFile file(strPath);
	if(!file.open(QIODevice::ReadOnly))
	{
		MQT_THROW_STR(QObject::trUtf8("open path %1 failed")
			.arg(strPath));
	}
	QDataStream stream(&file);
	m_PowerCali.clear();
	int iSize;
	stream>>iSize;
	for (int j=0;j<iSize;j++)
	{
		QPair<int,POWERPOSVECTOR> ParamPair;
		stream>>ParamPair.first;
		int iSizeVector;
		stream>>iSizeVector;
		POWERPOSVECTOR& ParamVector=ParamPair.second;
		ParamVector.resize(iSizeVector);
		for (int i=0;i<iSizeVector;i++)
		{
			stream>>ParamVector[i].dIndex;
			stream>>ParamVector[i].dPower;
			stream>>ParamVector[i].dPower_RT;
		}
		m_PowerCali.push_back(ParamPair);
	}
}


void CLaserPath_Thorlab::GenPower()
{
	m_PowerCali.clear();
	for(int j=0;j<5;j++){


		POWERPOSVECTOR vec;
		for(int i=0;i<10;i++)
		{
			PowerPosParam param;
			param.dIndex = i;
			param.dPower = i*0.1;
			param.dPower_RT = i*0.2;
			vec.push_back(param);
		}
		m_PowerCali.push_back(QPair<int, POWERPOSVECTOR>(j, vec));
	}

}

void CLaserPath_Thorlab::WritePowerPosMapText()
{
	QString strPath=
		CMakePath::make_config_path1(m_sName+"_LaserCaliDataThorlab.txt");
	QFile file(strPath);
	if(!file.open(QIODevice::WriteOnly))
	{
		MQT_THROW_STR(QObject::trUtf8("open path %1 failed")
			.arg(strPath));
	}

	QTextStream stream(&file);
	int iSize=m_PowerCali.size();
	stream<<"FreqCount:"<<iSize<<"\r\n";
	CALIMAP_FREQ::iterator ii;
	for (ii=m_PowerCali.begin();ii!=m_PowerCali.end();ii++)
	{
		stream<<"Freq:"<<ii->first<<"\r\n";
		POWERPOSVECTOR& ParamVector=ii->second;
		int iSizeReal=ParamVector.size();
		stream<<"Count"<<iSizeReal<<"\r\n";
		for (int i=0;i<iSizeReal;i++)
		{
			stream<<ParamVector[i].dIndex<<",";
			stream<<ParamVector[i].dPower<<",";
			stream<<ParamVector[i].dPower_RT<<"\r\n";
		}
	}
}