#include "claserpath_epulse.h"
#include "logic.h"

CLaserPath_Epulse::CLaserPath_Epulse(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);

}

bool CLaserPath_Epulse::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;
}

double CLaserPath_Epulse::PowerToAngle(int iFreq, double dSIndex, double dPower)
{
	POWERPOSVECTOR Param;
	bool b= FindPowerPosVector(iFreq,dSIndex,Param);
	if (dPower<0 || DEQUAL0(dPower))
	{
		MQT_THROW_CODESTR(ALARM_POWER_SETDATA, QObject::trUtf8("Power map err.source power<=0"));
	}
	if (!b)
	{
		MQT_THROW_CODESTR(ALARM_POWER_SETDATA, QObject::trUtf8("Power map err."
			"Freq: %1KHz,Width: %2mm has not cali.")
			.arg(iFreq).arg(dSIndex));
	}
	int iSize=Param.size();
	if (iSize<=2)
	{
		MQT_THROW_CODESTR(ALARM_POWER_SETDATA, QObject::trUtf8("Power map err."
			"Freq: %1KHz,Width: %2mm has not cali.")
			.arg(iFreq).arg(dSIndex));
	}

	if(dPower>Param.at(0).dPower)
	{
		MQT_THROW_CODESTR(ALARM_POWER_SETDATA, QObject::trUtf8("Power map err."
			"%1KHz source Power:%2 <Min Power:%3")
			.arg(iFreq).arg(dPower)
			.arg(Param.at(0).dPower));
	}
	else if (dPower<Param.at(iSize-1).dPower)
	{
		MQT_THROW_CODESTR(ALARM_POWER_SETDATA, QObject::trUtf8("Power map err."
			"%1KHz source Power:%2 >Max Power:%3")
			.arg(iFreq).arg(dPower)
			.arg(Param.at(iSize-1).dPower));
	}

	//EPULSE点检从大到小,查找时从小到大查找
	double powerPer = dPower/iFreq;
	for (int i=iSize-1;i>0;i--)
	{
		//dPower1  < dPower
		double dPowerLow=Param.at(i).dPower_RT;
		double dPowerHigh=Param.at(i-1).dPower_RT;
		int eprfHigh = (int)Param.at(i-1).dIndex;
		int eprfLow = (int)Param.at(i).dIndex;
		if (dPowerHigh > powerPer)
		{
			double eprf = eprfLow + (powerPer - dPowerLow) * (eprfHigh - eprfLow) / (dPowerHigh - dPowerLow);
			return  eprf;
		}
	}
	Q_ASSERT(0);
	return 0;
}

#define  PATH_HEADER	QString("D_NARROW")
bool CLaserPath_Epulse::AddPowerPosVector(int iFreq,double dSIndex,POWERPOSVECTOR& vector)
{
	int iSize=m_PowerCali.size();
	if(iSize <= 0){
		return false;
	}
	for (int i=0;i<iSize;i++)
	{
		if (iFreq==m_PowerCali[i].first)
		{
			m_PowerCali[i].second = vector;
			return true;
		}
	}

	POWERPOSVECTOR  Param;
	QPair<int,POWERPOSVECTOR> ParamPair;
	ParamPair.first=iFreq;
	ParamPair.second=Param;
	m_PowerCali.push_back(ParamPair);
	return true;
}

void CLaserPath_Epulse::WritePowerPosMap()
{
	QString strPath=
		CMakePath::make_config_path(PATH_HEADER+"_LaserCaliDataEpulse.ldf");
	QFile file(strPath);
	if(!file.open(QIODevice::WriteOnly))
	{
		MQT_THROW_CODESTR(ALARM_POWER_SETDATA, 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;	//存储EPULSE频率
			stream<<ParamVector[i].dPower;	//存储实际测量功率
			stream<<ParamVector[i].dPower_RT;	//存储单位功率 W/KHZ
		}
	}

	WritePowerPosMapText();
}

void CLaserPath_Epulse::ReadPowerPosMap()
{
	//GenPower();return;
	QString strPath=
		CMakePath::make_config_path(PATH_HEADER+"_LaserCaliDataEpulse.ldf");
	QFile file(strPath);
	if(!file.open(QIODevice::ReadOnly))
	{
		MQT_THROW_CODESTR(ALARM_POWER_SETDATA, 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_Epulse::WritePowerPosMapText()
{
	QString strPath=
		CMakePath::make_config_path(PATH_HEADER+"_LaserCaliDataEpulse.csv");
	QFile file(strPath);
	if(!file.open(QIODevice::WriteOnly))
	{
		MQT_THROW_CODESTR(ALARM_POWER_SETDATA, 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<<"KHZ\r\n";
		POWERPOSVECTOR& ParamVector=ii->second;
		int iSizeReal=ParamVector.size();
		//stream<<"Count"<<iSizeReal<<"\r\n";
		stream<<"EPULSE"<<","<<"Power"<<","<<"PowerRate"<<"\r\n";
		for (int i=0;i<iSizeReal;i++)
		{
			stream<<i+1<<",";
			stream<<ParamVector[i].dIndex<<",";
			stream<<ParamVector[i].dPower<<",";
			stream<<ParamVector[i].dPower_RT<<"\r\n";
		}
	}
}


void CLaserPath_Epulse::GenPower()
{
	m_PowerCali.clear();
	for(int j=1;j<=5;j++){
		POWERPOSVECTOR vec;
		for(int i=1;i<=10;i++)
		{
			PowerPosParam param;
			param.dIndex = i*50*1000;	//EPULSE频率
			param.dPower = (11-i)*1.0;	//功率
			param.dPower_RT = param.dPower/(j*10);	//功率系数 W/KHZ
			vec.push_back(param);
		}
		m_PowerCali.push_back(QPair<int, POWERPOSVECTOR>(j*10, vec));
	}

}