#include "SatelliteGF3xmlParser.h"
#include <QDateTime>


bool SatelliteGF3xmlParser::loadFile(const QString& filename) {
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Cannot open file:" << filename;
        return false;
    }

    QDomDocument doc;
    if (!doc.setContent(&file)) {
        file.close();
        qWarning() << "Failed to parse the file into a DOM tree.";
        return false;
    }
    file.close();

    xml = doc;
    return true;
}

void SatelliteGF3xmlParser::parsePlatform() {
    QDomElement platform = xml.firstChildElement("root").firstChildElement("platform");
    if (!platform.isNull()) {
        CenterTime = platform.firstChildElement("CenterTime").text();
        Rs = platform.firstChildElement("Rs").text().toDouble();
        satVelocity = platform.firstChildElement("satVelocity").text().toDouble();
        RollAngle = platform.firstChildElement("RollAngle").text().toDouble();
        PitchAngle = platform.firstChildElement("PitchAngle").text().toDouble();
        YawAngle = platform.firstChildElement("YawAngle").text().toDouble();
        Xs = platform.firstChildElement("Xs").text().toDouble();
        Ys = platform.firstChildElement("Ys").text().toDouble();
        Zs = platform.firstChildElement("Zs").text().toDouble();
        Vxs = platform.firstChildElement("Vxs").text().toDouble();
        Vys = platform.firstChildElement("Vys").text().toDouble();
        Vzs = platform.firstChildElement("Vzs").text().toDouble();

        qDebug() << "Platform Data:";
        qDebug() << "CenterTime:" << CenterTime;
        qDebug() << "Rs:" << Rs;
        qDebug() << "satVelocity:" << satVelocity;
        qDebug() << "RollAngle:" << RollAngle;
        qDebug() << "PitchAngle:" << PitchAngle;
        qDebug() << "YawAngle:" << YawAngle;
        qDebug() << "Xs:" << Xs;
        qDebug() << "Ys:" << Ys;
        qDebug() << "Zs:" << Zs;
        qDebug() << "Vxs:" << Vxs;
        qDebug() << "Vys:" << Vys;
        qDebug() << "Vzs:" << Vzs;
    }
}

void SatelliteGF3xmlParser::parseGPS() {
    QDomElement GPS = xml.firstChildElement("root").firstChildElement("GPS");
    if (!GPS.isNull()) {
        QDomNodeList GPSParams = GPS.elementsByTagName("GPSParam");
        for (int i = 0; i < GPSParams.count(); ++i) {
            QDomElement GPSParam = GPSParams.at(i).toElement();
            SatellitePos satepos{0.0 ,0.0,0.0,0.0, 0.0,0.0,0.0 };

            QString TimeStamp = GPSParam.firstChildElement("TimeStamp").text();
            QString xPosition = GPSParam.firstChildElement("xPosition").text();
            QString yPosition = GPSParam.firstChildElement("yPosition").text();
            QString zPosition = GPSParam.firstChildElement("zPosition").text();
            QString xVelocity = GPSParam.firstChildElement("xVelocity").text();
            QString yVelocity = GPSParam.firstChildElement("yVelocity").text();
            QString zVelocity = GPSParam.firstChildElement("zVelocity").text();

            QDateTime dateTime = QDateTime::fromString(TimeStamp, "yyyy-MM-dd HH:mm:ss.zzzzzz");
            satepos.time   = dateTime.toMSecsSinceEpoch() / 1000.0;
            satepos.Px      = xPosition.toDouble();
            satepos.Py      = yPosition.toDouble();
            satepos.Pz      = zPosition.toDouble();
            satepos.Vx      = xVelocity.toDouble();
            satepos.Vy      = yVelocity.toDouble();
            satepos.Vz      = zVelocity.toDouble();

            this->antposs.append(satepos);

            qDebug() << "\nGPS Param Data:";
            qDebug() << "TimeStamp:" << TimeStamp;
            qDebug() << "xPosition:" << xPosition;
            qDebug() << "yPosition:" << yPosition;
            qDebug() << "zPosition:" << zPosition;
            qDebug() << "xVelocity:" << xVelocity;
            qDebug() << "yVelocity:" << yVelocity;
            qDebug() << "zVelocity:" << zVelocity;
        }
    }
}

void SatelliteGF3xmlParser::parseImageInfo() {
    QDomElement imageinfo = xml.firstChildElement("root").firstChildElement("imageinfo");
    if (!imageinfo.isNull()) {
        QDomElement imagingTime = imageinfo.firstChildElement("imagingTime");
        ;
        start = QDateTime::fromString(imagingTime.firstChildElement("start").text(), "yyyy-MM-dd HH:mm:ss.zzzzzz").toMSecsSinceEpoch()/1000.0;
        end = QDateTime::fromString(imagingTime.firstChildElement("end").text(), "yyyy-MM-dd HH:mm:ss.zzzzzz").toMSecsSinceEpoch() / 1000.0;
       
        nearRange = imageinfo.firstChildElement("nearRange").text().toDouble();
        refRange = imageinfo.firstChildElement("refRange").text().toDouble();
        eqvFs = imageinfo.firstChildElement("eqvFs").text().toDouble();
        eqvPRF = imageinfo.firstChildElement("eqvPRF").text().toDouble();
        QDomElement center = imageinfo.firstChildElement("center");
        latitude_center = center.firstChildElement("latitude").text().toDouble();
        longitude_center = center.firstChildElement("longitude").text().toDouble();
        QDomElement corner = imageinfo.firstChildElement("corner");
        QDomElement topLeft = corner.firstChildElement("topLeft");
        latitude_topLeft = topLeft.firstChildElement("latitude").text().toDouble();
        longitude_topLeft = topLeft.firstChildElement("longitude").text().toDouble();
        QDomElement topRight = corner.firstChildElement("topRight");
        latitude_topRight = topRight.firstChildElement("latitude").text().toDouble();
        longitude_topRight = topRight.firstChildElement("longitude").text().toDouble();
        QDomElement bottomLeft = corner.firstChildElement("bottomLeft");
        latitude_bottomLeft = bottomLeft.firstChildElement("latitude").text().toDouble();
        longitude_bottomLeft = bottomLeft.firstChildElement("longitude").text().toDouble();
		QDomElement bottomRight = corner.firstChildElement("bottomRight");
		latitude_bottomRight = bottomRight.firstChildElement("latitude").text().toDouble();
		longitude_bottomRight = bottomRight.firstChildElement("longitude").text().toDouble();
		width = imageinfo.firstChildElement("width").text().toInt();
		height = imageinfo.firstChildElement("height").text().toInt();
		widthspace = imageinfo.firstChildElement("widthspace").text().toDouble();
		heightspace = imageinfo.firstChildElement("heightspace").text().toDouble();
		sceneShift = imageinfo.firstChildElement("sceneShift").text().toDouble();
		imagebit = imageinfo.firstChildElement("imagebit").text();
		QDomElement qualifyValue = imageinfo.firstChildElement("QualifyValue");

        
         bool HH_QualifyValueISNULL=false;
         bool HV_QualifyValueISNULL=false;
         bool VH_QualifyValueISNULL=false;
         bool VV_QualifyValueISNULL = false;



		HH_QualifyValue = qualifyValue.firstChildElement("HH").text().toDouble(&HH_QualifyValueISNULL);
		HV_QualifyValue = qualifyValue.firstChildElement("HV").text().toDouble(&HV_QualifyValueISNULL);
        VH_QualifyValue = qualifyValue.firstChildElement("HH").text().toDouble(&VH_QualifyValueISNULL);
        VV_QualifyValue = qualifyValue.firstChildElement("HV").text().toDouble(&VV_QualifyValueISNULL);


		HH_QualifyValue = HH_QualifyValueISNULL ? HH_QualifyValue : -1;
		HV_QualifyValue = HV_QualifyValueISNULL ? HV_QualifyValue : -1;
		VH_QualifyValue = VH_QualifyValueISNULL ? VH_QualifyValue : -1;
		VV_QualifyValue = VV_QualifyValueISNULL ? VV_QualifyValue : -1;


		QDomElement echoSaturation = imageinfo.firstChildElement("echoSaturation");
		HH_echoSaturation = echoSaturation.firstChildElement("HH").text().toDouble();
		HV_echoSaturation = echoSaturation.firstChildElement("HV").text().toDouble();
		incidenceAngleNearRange = imageinfo.firstChildElement("incidenceAngleNearRange").text().toDouble();
		incidenceAngleFarRange = imageinfo.firstChildElement("incidenceAngleFarRange").text().toDouble();

        qDebug() << "\nImage Info Data:";
        qDebug() << "Start:" << start;
        qDebug() << "End:" << end;
        qDebug() << "Near Range:" << nearRange;
        qDebug() << "Ref Range:" << refRange;
        qDebug() << "Eqv Fs:" << eqvFs;
        qDebug() << "Eqv PRF:" << eqvPRF;
        qDebug() << "Center Latitude:" << latitude_center << ", Longitude:" << longitude_center;
        qDebug() << "Top Left Corner Latitude:" << latitude_topLeft << ", Longitude:" << longitude_topLeft;
        qDebug() << "Top Right Corner Latitude:" << latitude_topRight << ", Longitude:" << longitude_topRight;
        qDebug() << "Bottom Left Corner Latitude:" << latitude_bottomLeft << ", Longitude:" << longitude_bottomLeft;
        qDebug() << "Bottom Right Corner Latitude:" << latitude_bottomRight << ", Longitude:" << longitude_bottomRight;
        qDebug() << "Width:" << width;
        qDebug() << "Height:" << height;
        qDebug() << "Width Space:" << widthspace;
        qDebug() << "Height Space:" << heightspace;
        qDebug() << "Scene Shift:" << sceneShift;
        qDebug() << "Image Bit:" << imagebit;
        qDebug() << "HH Qualify Value:" << HH_QualifyValue;
        qDebug() << "HV Qualify Value:" << HV_QualifyValue;
        qDebug() << "HH Echo Saturation:" << HH_echoSaturation;
        qDebug() << "HV Echo Saturation:" << HV_echoSaturation;
        qDebug() << "incidenceAngleNearRange:" << incidenceAngleNearRange;
        qDebug() << "incidenceAngleFarRange:" << incidenceAngleFarRange;
    }
}

void SatelliteGF3xmlParser::parseAdditionalData() {
    QDomElement calibrationConst = xml.firstChildElement("root").firstChildElement("CalibrationConst");
    if (!calibrationConst.isNull()) {

        bool HH_CalibrationConstISNULL=false;
        bool HV_CalibrationConstISNULL=false;
        bool VH_CalibrationConstISNULL=false;
        bool VV_CalibrationConstISNULL = false;
        HH_CalibrationConst = calibrationConst.firstChildElement("HH").text().toDouble(&HH_CalibrationConstISNULL);
        HV_CalibrationConst = calibrationConst.firstChildElement("HV").text().toDouble(&HV_CalibrationConstISNULL);
        VH_CalibrationConst = calibrationConst.firstChildElement("VH").text().toDouble(&VH_CalibrationConstISNULL);
        VV_CalibrationConst = calibrationConst.firstChildElement("VV").text().toDouble(&VV_CalibrationConstISNULL);

		HH_CalibrationConst = HH_CalibrationConstISNULL ? HH_CalibrationConst : -1;
		HV_CalibrationConst = HV_CalibrationConstISNULL ? HV_CalibrationConst : -1;
		VH_CalibrationConst = VH_CalibrationConstISNULL ? VH_CalibrationConst : -1;
		VV_CalibrationConst = VV_CalibrationConstISNULL ? VV_CalibrationConst : -1;



        qDebug() << "\nCalibration Const Data:";
        qDebug() << "HH Calibration Const:" << HH_CalibrationConst;
        qDebug() << "HV Calibration Const:" << HV_CalibrationConst;
        qDebug() << "VH Calibration Const:" << VH_CalibrationConst;
        qDebug() << "VV Calibration Const:" << VV_CalibrationConst;
    }

    AzFdc0 = xml.firstChildElement("root").firstChildElement("AzFdc0").text().toDouble();
    AzFdc1 = xml.firstChildElement("root").firstChildElement("AzFdc1").text().toDouble();

    sensorID = xml.firstChildElement("root").firstChildElement("sensorID").text();
    imagingMode = xml.firstChildElement("root").firstChildElement("imagingMode").text();
    lamda = xml.firstChildElement("root").firstChildElement("lamda").text().toDouble();
    RadarCenterFrequency = xml.firstChildElement("root").firstChildElement("RadarCenterFrequency").text().toDouble();

    TotalProcessedAzimuthBandWidth = xml.firstChildElement("root").firstChildElement("TotalProcessedAzimuthBandWidth").text().toDouble();
    DopplerParametersReferenceTime = xml.firstChildElement("root").firstChildElement("DopplerParametersReferenceTime").text().toDouble();

    QDomElement dopplerCentroidCoefficients = xml.firstChildElement("root").firstChildElement("DopplerCentroidCoefficients");
    d0 = dopplerCentroidCoefficients.firstChildElement("d0").text().toDouble();
    d1 = dopplerCentroidCoefficients.firstChildElement("d1").text().toDouble();
    d2 = dopplerCentroidCoefficients.firstChildElement("d2").text().toDouble();
    d3 = dopplerCentroidCoefficients.firstChildElement("d3").text().toDouble();
    d4 = dopplerCentroidCoefficients.firstChildElement("d4").text().toDouble();

    QDomElement dopplerRateValuesCoefficients = xml.firstChildElement("root").firstChildElement("DopplerRateValuesCoefficients");
    r0 = dopplerRateValuesCoefficients.firstChildElement("r0").text().toDouble();
    r1 = dopplerRateValuesCoefficients.firstChildElement("r1").text().toDouble();
    r2 = dopplerRateValuesCoefficients.firstChildElement("r2").text().toDouble();
    r3 = dopplerRateValuesCoefficients.firstChildElement("r3").text().toDouble();
    r4 = dopplerRateValuesCoefficients.firstChildElement("r4").text().toDouble();

    DEM = xml.firstChildElement("root").firstChildElement("DEM").text().toDouble();

    qDebug() << "\nAdditional Data:";
    qDebug() << "AzFdc0:" << AzFdc0;
    qDebug() << "AzFdc1:" << AzFdc1;
    qDebug() << "Sensor ID:" << sensorID;
    qDebug() << "Imaging Mode:" << imagingMode;
    qDebug() << "Lambda:" << lamda;
    qDebug() << "Radar Center Frequency:" << RadarCenterFrequency;
    qDebug() << "Total Processed Azimuth Band Width:" << TotalProcessedAzimuthBandWidth;
    qDebug() << "Doppler Parameters Reference Time:" << DopplerParametersReferenceTime;
    qDebug() << "Doppler Centroid Coefficients: d0=" << d0 << ", d1=" << d1 << ", d2=" << d2 << ", d3=" << d3 << ", d4=" << d4;
    qDebug() << "Doppler Rate Values Coefficients: r0=" << r0 << ", r1=" << r1 << ", r2=" << r2 << ", r3=" << r3 << ", r4=" << r4;
    qDebug() << "DEM:" << DEM;
}
