#include "stdafx.h"
#include "BaseConstVariable.h"

#include "BaseTool.h"
#include "SARSimulationTaskSetting.h"
#include <QFile>
#include <QDomDocument>
#include <QDateTime>
#include <QString>
#include <QDebug>

 

SatelliteOribtNode SARSimulationTaskSetting::getSatelliteOribtNode(double& timeFromStartTime, bool& antAzAngleFlag)
{
	return this->OribtModel->getSatelliteOribtNode(timeFromStartTime, antAzAngleFlag);
}

ErrorCode SARSimulationTaskSetting::getSatelliteOribtNode(double& timeFromStartTime, SatelliteOribtNode& node, bool& antAzAngleFlag)
{
	return this->OribtModel->getSatelliteOribtNode(timeFromStartTime, node, antAzAngleFlag);
}

void SARSimulationTaskSetting::setSatelliteOribtModel(std::shared_ptr < AbstractSatelliteOribtModel>  model)
{
	this->OribtModel = model;
}

ErrorCode SARSimulationTaskSetting::getSatelliteAntDirectNormal(SatelliteOribtNode& Rs, Vector3D& Rt, SatelliteAntDirect& antNode)
{
	return this->OribtModel->getSatelliteAntDirectNormal(Rs, Rt, antNode);
}

void SARSimulationTaskSetting::setSARImageStartTime(long double imageStartTime)
{
	this->imageStartTime = imageStartTime;
}

void SARSimulationTaskSetting::setSARImageEndTime(long double imageEndTime)
{
	this->imageEndTime = imageEndTime;
}

double SARSimulationTaskSetting::getSARImageStartTime()
{
	return this->imageStartTime;
}

double SARSimulationTaskSetting::getSARImageEndTime()
{
	return this->imageEndTime;
}

double SARSimulationTaskSetting::getNearRange()
{
	return this->NearRange;
}

void SARSimulationTaskSetting::setNearRange(double NearRange)
{
	this->NearRange = NearRange;
}

double SARSimulationTaskSetting::getFarRange()
{
	return this->FarRange;
}

void SARSimulationTaskSetting::setFarRange(double FarRange)
{
	this->FarRange = FarRange;
}

bool SARSimulationTaskSetting::getIsRightLook()
{
	return this->isR;
}

void SARSimulationTaskSetting::setIsRightLook(bool isR)
{
	this->isR = isR;
}

void SARSimulationTaskSetting::setCenterFreq(double Freq)
{
	this->centerFreq = Freq;
	this->centerLamda = LIGHTSPEED / Freq;
}

double SARSimulationTaskSetting::getCenterFreq()
{
	return this->centerFreq;
}

void SARSimulationTaskSetting::setCenterLamda(double Lamda)
{
	this->centerFreq = LIGHTSPEED/Lamda;
	this->centerLamda =  this->centerFreq;
}

double SARSimulationTaskSetting::getCenterLamda()
{
	return this->centerLamda;
}

void SARSimulationTaskSetting::setBandWidth(double bandwidth)
{
	this->bandWidth = bandwidth;
}

double SARSimulationTaskSetting::getBandWidth()
{
	return this->bandWidth;
}

POLARTYPEENUM SARSimulationTaskSetting::getPolarType()
{
	return this->polarType;
}

void SARSimulationTaskSetting::setPolarType(POLARTYPEENUM type)
{
	this->polarType = type;
}

double SARSimulationTaskSetting::getCenterLookAngle()
{
	return this->centerLookAngle;
}

void SARSimulationTaskSetting::setCenterLookAngle(double angle)
{
	this->centerLookAngle = angle;
}

void SARSimulationTaskSetting::setPRF(double prf)
{
	this->PRF = prf;
}

double SARSimulationTaskSetting::getPRF()
{
	return this->PRF;
}

void SARSimulationTaskSetting::setRefphaseRange(double refRange)
{
	this->refPhaseRange = refRange;
}

double SARSimulationTaskSetting::getRefphaseRange()
{
	return this->refPhaseRange;
}

double SARSimulationTaskSetting::getFs()
{
	return this->Fs;
}

void SARSimulationTaskSetting::setFs(double fs)
{
	this->Fs = fs;
}

void SARSimulationTaskSetting::setTransformRadiationPattern(std::shared_ptr<AbstractRadiationPattern> radiationPanttern)
{
	this->TransformRadiationPattern = radiationPanttern;
}

void SARSimulationTaskSetting::setReceiveRadiationPattern(std::shared_ptr<AbstractRadiationPattern> radiationPanttern)
{
	this->ReceiveRadiationPattern = radiationPanttern;
}

std::shared_ptr<AbstractRadiationPattern> SARSimulationTaskSetting::getTransformRadiationPattern()
{
	return this->TransformRadiationPattern;
}

std::shared_ptr<AbstractRadiationPattern> SARSimulationTaskSetting::getReceiveRadiationPattern()
{
	return this->ReceiveRadiationPattern;
}

double SARSimulationTaskSetting::getPt()
{
	return this->pt;
}

double SARSimulationTaskSetting::getGri()
{
	return this->Gri;
}

void SARSimulationTaskSetting::setPt(double InPt)
{
	this->pt = InPt;
}

void SARSimulationTaskSetting::setGri(double Ingri)
{
	this->Gri = Ingri;
}

double SARSimulationTaskSetting::getDopplerParametersReferenceTime()
{
	return this->DopplerParametersReferenceTime;
}

void SARSimulationTaskSetting::setDopplerParametersReferenceTime(double time)
{
	this->DopplerParametersReferenceTime = time;
}

std::vector<double> SARSimulationTaskSetting::getDopplerCentroidCoefficients()
{
	return this->DopplerCentroidCoefficients;
}

void SARSimulationTaskSetting::setDopplerCentroidCoefficients(std::vector<double> DopplerCentroids)
{
	this->DopplerCentroidCoefficients = DopplerCentroids;
}

std::vector<double> SARSimulationTaskSetting::getDopplerRateValuesCoefficients()
{
	return this->DopplerRateValuesCoefficients;
}

void SARSimulationTaskSetting::setDopplerRateValuesCoefficients(std::vector<double> DopplerRateValues)
{
	this->DopplerRateValuesCoefficients	= DopplerRateValues;
}

std::shared_ptr<AbstractSARSatelliteModel> ReadSimulationSettingsXML(QString xmlPath)
{
    QDomDocument doc;
    QFile file(xmlPath);

    // 打开XML文件
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning("Could not open XML file");
        return nullptr;
    }

    // 解析XML内容
    if (!doc.setContent(&file)) {
        file.close();
        qWarning("Failed to parse XML file");
        return nullptr;
    }
    file.close();

    // 获取根元素
    QDomElement root = doc.documentElement();

    // 提取信息
    QDomElement satellite = root.firstChildElement("satellite");
    QDomElement taskSensor = root.firstChildElement("TaskSensor");

	if (satellite.isNull() || taskSensor.isNull()) {
		qDebug() << QString::fromStdString(errorCode2errInfo(ErrorCode::XMLNOTFOUNDElEMENT));
		return nullptr;
	}
	else {}


    QDomElement imagingMode = taskSensor.firstChildElement("imagingMode");
    QDomElement radarCenterFrequency = taskSensor.firstChildElement("RadarCenterFrequency");
    QDomElement bandWidth = taskSensor.firstChildElement("bandWidth");
    QDomElement centerLookAngle = taskSensor.firstChildElement("centerLookAngle");
    QDomElement prf = taskSensor.firstChildElement("prf");
    QDomElement fs = taskSensor.firstChildElement("fs");
    QDomElement refphaseRange = taskSensor.firstChildElement("refPhaseRange");
    QDomElement polar = taskSensor.firstChildElement("polar");
    QDomElement nearRange = taskSensor.firstChildElement("nearRange");
    QDomElement farRange = taskSensor.firstChildElement("farRange");
	QDomElement lookDirection = taskSensor.firstChildElement("lookDirection");
	QDomElement ptitem = taskSensor.firstChildElement("Pt");
	QDomElement griitem = taskSensor.firstChildElement("Gri");


	
	if (imagingMode.isNull() || radarCenterFrequency.isNull() || bandWidth.isNull() 
		|| centerLookAngle.isNull() || prf.isNull() || polar.isNull() 
		|| nearRange.isNull() || farRange.isNull()|| refphaseRange.isNull()
		|| ptitem.isNull()
		|| griitem.isNull())
	{
		qDebug() << QString::fromStdString(errorCode2errInfo(ErrorCode::XMLNOTFOUNDElEMENT));
		return nullptr;
	}
	else {}

    // 提取成像时间
    QDomElement imagingTime = taskSensor.firstChildElement("imagingTime");


    QDomElement startTime = imagingTime.firstChildElement("start");
    QDomElement endTime = imagingTime.firstChildElement("end");

    // 提取多普勒参数
    //QDomElement dopplerParametersReferenceTime = taskSensor.firstChildElement("DopplerParametersReferenceTime");
 
	if (imagingTime.isNull() || startTime.isNull() || endTime.isNull())
	{
		qDebug() << QString::fromStdString(errorCode2errInfo(ErrorCode::XMLNOTFOUNDElEMENT));
		return nullptr;
 
	}
	else {}

	std::shared_ptr<SARSimulationTaskSetting> taskSetting = std::make_shared<SARSimulationTaskSetting>();
 
	double starttimestamp = convertToMilliseconds(startTime.text().trimmed().toStdString());
	double endtimestamp = convertToMilliseconds(endTime.text().trimmed().toStdString());

	if (polar.text().toLower().contains("hh")) {
		taskSetting->setPolarType(POLARHH);
	}
	else if (polar.text().toLower().contains("hv")) {
		taskSetting->setPolarType(POLARHV);
	}	
	else if (polar.text().toLower().contains("vh")) {
		taskSetting->setPolarType(POLARVH);
	}	
	else if (polar.text().toLower().contains("vv")) {
		taskSetting->setPolarType(POLARVV);
	}
	else {}

	bool isR = false;
	if (!lookDirection.isNull()) {
		qDebug() <<"lookDirection" << lookDirection.text();
		if (lookDirection.text().toUpper() == "R") {
			isR = true;
		}
	}

	// 解析Dopper参数 DopplerParametersReferenceTime
	QDomElement DopplerParametersReferenceTimeItem = taskSensor.firstChildElement("DopplerParametersReferenceTime");
	QDomElement DopplerCentroidCoefficientItem = taskSensor.firstChildElement("DopplerCentroidCoefficients");
	QDomElement DopplerRateValuesCoefficientItem = taskSensor.firstChildElement("DopplerRateValuesCoefficients");

	// 处理多普勒读取

	double dopplerRefrenceTime = DopplerParametersReferenceTimeItem.text().toDouble();
	std::vector<double> DopplerCentroidCoefficients(5);
	std::vector<double> DopplerRateValuesCoefficient(5);

	DopplerCentroidCoefficients[0]= DopplerCentroidCoefficientItem.firstChildElement("d0").text().toDouble();
	DopplerCentroidCoefficients[1]= DopplerCentroidCoefficientItem.firstChildElement("d1").text().toDouble();
	DopplerCentroidCoefficients[2]= DopplerCentroidCoefficientItem.firstChildElement("d2").text().toDouble();
	DopplerCentroidCoefficients[3]= DopplerCentroidCoefficientItem.firstChildElement("d3").text().toDouble();
	DopplerCentroidCoefficients[4]= DopplerCentroidCoefficientItem.firstChildElement("d4").text().toDouble();


	DopplerRateValuesCoefficient[0] = DopplerRateValuesCoefficientItem.firstChildElement("r0").text().toDouble();
	DopplerRateValuesCoefficient[1] = DopplerRateValuesCoefficientItem.firstChildElement("r1").text().toDouble();
	DopplerRateValuesCoefficient[2] = DopplerRateValuesCoefficientItem.firstChildElement("r2").text().toDouble();
	DopplerRateValuesCoefficient[3] = DopplerRateValuesCoefficientItem.firstChildElement("r3").text().toDouble();
	DopplerRateValuesCoefficient[4] = DopplerRateValuesCoefficientItem.firstChildElement("r4").text().toDouble();



	taskSetting->setRefphaseRange(refphaseRange.text().toDouble());
	taskSetting->setCenterLookAngle(centerLookAngle.text().toDouble());
	taskSetting->setSARImageStartTime(starttimestamp); // 成像开始时间
	taskSetting->setSARImageEndTime(endtimestamp); // 成像终止时间
	taskSetting->setBandWidth(bandWidth.text().toDouble());
	taskSetting->setCenterFreq(radarCenterFrequency.text().toDouble()); // 中心频率
	taskSetting->setPRF(prf.text().toDouble());	// PRF
	taskSetting->setFs(fs.text().toDouble());      //Fs
	taskSetting->setNearRange(nearRange.text().toDouble()); // NearRange
	taskSetting->setFarRange(farRange.text().toDouble()); // FarRange
	taskSetting->setIsRightLook(isR);
	taskSetting->setPt(ptitem.text().toDouble());
	taskSetting->setGri(griitem.text().toDouble());
	taskSetting->setDopplerParametersReferenceTime(dopplerRefrenceTime);
	taskSetting->setDopplerCentroidCoefficients(DopplerCentroidCoefficients);
	taskSetting->setDopplerRateValuesCoefficients(DopplerRateValuesCoefficient);


	return  taskSetting;
}