#include "SPG4Function.h"
#include "SPG4Tool.h"
#include <Tle.h>
#include <SGP4.h>
#include <Observer.h>
#include <CoordGeodetic.h>
#include <CoordTopocentric.h>

#include <list>
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <QDate>
#include <QDateTime>



std::vector<SatelliteAntPos> RunTle(libsgp4::Tle tle, double start, double end, double inc, bool printfinfoflag, bool running, bool first_run)
{
    std::cout << "RunTle\t"  ;
    std::cout << "Start time:\t" << start;
    std::cout << "End time:\t" << end;
    std::cout << "inc time:\t" << inc << std::endl;

    std::vector<SatelliteAntPos> resultpos(0);
    double current = start;
    libsgp4::SGP4 model(tle);
    //bool running = true;
    //bool first_run = true;

    std::cout << std::setprecision(0) << tle.NoradNumber() << " xx"
        << std::endl;

    while (running)
    {
        bool error = false;
        libsgp4::Vector position;
        libsgp4::Vector velocity;
        double tsince;

        try
        {
            if (first_run && current != 0.0)
            {
                /*
                 * make sure first run is always as zero
                 */
                tsince = 0.0;
            }
            else
            {
                /*
                 * otherwise run as normal
                 */
                tsince = current;
            }

            libsgp4::Eci eci = model.FindPosition(tsince);
            position = eci.Position();
            velocity = eci.Velocity();

        }
        catch (libsgp4::SatelliteException& e)
        {
            std::cerr << "SatelliteException:\t" << e.what() << std::endl;
            error = true;
            running = false;
        }
        catch (libsgp4::DecayedException& e)
        {
            std::cerr <<"DecayedException:\t" << e.what() << std::endl;

            position = e.Position();
            velocity = e.Velocity();

            if (!first_run)
            {
                // print out position on first run
                error = true;
            }

            running = false;
        }

        if (!error)
        {
            SatelliteAntPos antpos{};
            antpos.time = tsince;
            antpos.Px = position.x;
            antpos.Py = position.y;
            antpos.Pz = position.z;
            antpos.Vx = velocity.x;
            antpos.Vy = velocity.y;
            antpos.Vz = velocity.z;

            resultpos.push_back(antpos);
            if (printfinfoflag) {
                std::cout << std::setprecision(8) << std::fixed;
                std::cout.width(17);
                std::cout << tsince << " ";
                std::cout.width(16);
                std::cout << position.x << " ";
                std::cout.width(16);
                std::cout << position.y << " ";
                std::cout.width(16);
                std::cout << position.z << " ";
                std::cout << std::setprecision(9) << std::fixed;
                std::cout.width(14);
                std::cout << velocity.x << " ";
                std::cout.width(14);
                std::cout << velocity.y << " ";
                std::cout.width(14);
                std::cout << velocity.z << std::endl;
            }
        }

        if ((first_run && current == 0.0) || !first_run)
        {
            if (current == end)
            {
                running = false;
            }
            else if ((current + inc) > end)
            {
                current = end;
            }
            else
            {
                current += inc;
            }
        }
        first_run = false;
    }


    return resultpos;
}

std::vector<SatelliteAntPos> getGPSPoints(std::string line1, std::string line2, double start, double end, double inc, bool printfinfoflag, bool running , bool first_run )
{
    libsgp4::Tle tle("satellites", line1, line2);
    std::cout << "Start time:\t" << start;
    std::cout << "End time:\t" << end;
    std::cout << "inc time:\t" << inc << std::endl;
    std::vector<SatelliteAntPos> result= RunTle(tle, start, end, inc, printfinfoflag,running,first_run);
    return  result;
}


double parseTLETimeOffset(const std::string& tle) {
    // 假设TLE字符串的时间信息在特定位置
    // 例如，TLE的第19到32个字符表示纪元时间
    if (tle.length() < 32) {
        return 0.0;
    }

    std::string epochStr = tle.substr(18, 14);
    int year = std::stoi(epochStr.substr(0, 2));
    double dayOfYear = std::stod(epochStr.substr(2));

    // 将两位数年份转换为四位数年份
    if (year < 57) {
        year += 2000;
    }
    else {
        year += 1900;
    }

    // 计算从纪元开始到指定日期的毫秒数
    QDate date(year, 1, 1);
    QDateTime dateTime(date.addDays(static_cast<int>(dayOfYear) - 1), QTime(0, 0), Qt::UTC);
    qint64 millisecondsSinceEpoch = dateTime.toMSecsSinceEpoch() + static_cast<qint64>((dayOfYear - static_cast<int>(dayOfYear)) * 86400000);

    return millisecondsSinceEpoch / 1000.0;
}
