#pragma once

#include "../Data_Source/Database.h"

// https://wiki.jetvision.de/wiki/Radarcape:Software_Features#Aircraft_List_JSON_Service
struct Aircraft_List_JSON_Service_O {
    size_t times; //自定义添加出现的次数  用于显示的优先级
    time_t uti{}; // Time of last message: Seconds since UNIX epoch
    std::uint32_t ns{}; // Time of last message: Nanoseconds since Second epoch
    std::uint32_t day_second{}; // Time of last message: Nanoseconds since Second epoch
    std::string hex; // ICAO Mode-S or c, in Hex format
    RET<std::string> fli; // Flight Identification
    RET<std::string> ava; // Flight data: available flight data sources
    std::string src;	//  Flight data: Source
    std::string cla; // Subclass of ADS-B data
    RET<double> lat; // Flight data: Latitude (-90 to 90º)
    RET<double> lon; // Flight data: Longitude (-180 to 180º)
    RET<double> alt; // Flight data: Barometric uncalibrated altitude (ft)
    RET<double> spd; // Flight data: Ground Speed (kt)
    RET<double> trk; // Flight data: True track (0-360º)
    RET<double> vrt; // Flight data: Vertical Rate (ft/min)
    std::string gda; //	Airborne/Ground
    std::string cat; //	Aircraft category
    RET<std::string> org; // Origin           DB       起点机场
    RET<std::string> dst; // Destination      DB       目标机场
    RET<std::string> opr; // Operator         DB
    RET<std::string> typ; // Type             DB
    RET<std::string> reg; // Registration     DB
    RET<std::string> squ;	//  Mode-A (Squawk)
    RET<std::string> cou;	// Country
    RET<double> dis;	// Distance (km or NM depending on configuration)
    RET<size_t> tru;	// Trust count Number of Mode-S frames successfully decoded from this A/C. Statistically it's possible that for a very low count the A/C does not actually exist
    int dbm{};	// Approximate signal level in dBm;
    RET<time_t> lla;	// int Age of last Position (s)
    RET<int> tmp;	 // Temperature (ºC)
    RET<int> wsp	; // Wind Speed (kt)
    RET<double> wdi;	// Wind from Direction (0-360º)
    RET<int> mop;	// ADS-B MOPS version;
    RET<std::string> spi;	// SPI (Squawk ident);
    RET<bool> alr;	// Alert status
    RET<double> ias; // IAS
    RET<double> tas; // TAS
    RET<double> hdgm;	// Magnetic Heading
    RET<std::string> hdgt;	// True Heading
    RET<std::string> qnhs;	// Selected QNH
    RET<std::string> alts;	// Selected QNH
    RET<double> hdgs;	// Selected Heading
    RET<double> altg;	// GNSS elipsoidal altitude (ft)
    RET<std::string> pic; // Asterix PIC
    RET<std::string> tcm; // ACAS/TCAS status
    RET<std::string> ape; // Autopilot engaged
    RET<int> sil; // ADS-B SIL
    RET<std::string> sda; // ADS-B SDA
    RET<int> nacp; // ADS-B SDA
    RET<int> pest; // Number of position estimations per second
    RET<size_t> nocl; // Number of sensors providing data aircraft
    RET<std::string> tq;  // Time quality of this entry

    Psc::JSON toJson() {
        auto ret = Psc::JSON::object();
        ADD_Json(times);
        ADD_Json(uti);
        ADD_Json(ns);
        ADD_Json(day_second);
        ADD_Json(hex);
        ADD_Json_RET(fli);
        ADD_Json_RET(ava);
        ADD_Json(src);
        ADD_Json(cla);
        ADD_Json_RET(lat);
        ADD_Json_RET(lon);
        ADD_Json_RET(alt);
        ADD_Json_RET(spd);
        ADD_Json_RET(trk);
        ADD_Json_RET(vrt);
        ADD_Json(gda);
        ADD_Json(cat);
        ADD_Json_RET(org);
        ADD_Json_RET(dst);
        ADD_Json_RET(opr);
        ADD_Json_RET(typ);
        ADD_Json_RET(reg);
        ADD_Json_RET(squ);
        ADD_Json_RET(cou);
        ADD_Json_RET(dis);
        ADD_Json(tru);
        ADD_Json(dbm);
        ADD_Json_RET(lla);
        ADD_Json_RET(tmp);
        ADD_Json_RET(wsp);
        ADD_Json_RET(wdi);
        ADD_Json_RET(mop);
        ADD_Json_RET(spi);
        ADD_Json_RET(alr);
        ADD_Json_RET(ias);
        ADD_Json_RET(tas);
        ADD_Json_RET(hdgm);
        ADD_Json_RET(hdgt);
        ADD_Json_RET(qnhs);
        ADD_Json_RET(alts);
        ADD_Json_RET(pic);
        ADD_Json_RET(tcm);
        ADD_Json_RET(ape);
        ADD_Json_RET(sil);
        ADD_Json_RET(sda);
        ADD_Json_RET(nacp);
        ADD_Json_RET(pest);
        ADD_Json_RET(nocl);
        ADD_Json_RET(tq);
        return ret;
    }


    // https://wiki.jetvision.de/wiki/Radarcape:Software_Features#Aircraft_List_JSON_Service
    static Aircraft_List_JSON_Service_O to_That(DataBase* db, Aircraft* info) {
        Aircraft_List_JSON_Service_O ret;
        // 航线查询结果保存在 Aircraft 中，仅在首次加载或 callsign 变化时重新读取数据库。
        ret.times = info->times;
        ret.uti = info->timestamp;
        ret.ns = info->mlat_timestamp.nanosec;
        ret.day_second = info->mlat_timestamp.daysec;
        ret.hex = info->icao; // TODO not c
        // fli	Flight Identification		Mode-S	From ADS-B or BDS2,0 as specified on ICAO Annex 10, Volume IV, Table 3-9; "?" when A/C reports loss of interface
        ret.fli = info->bds20_call_sign();
        // ava ava	Flight data: available flight data sources			One letter per available flight data sources. A=ADSB, M=Jetvision MLAT, F=FlightAware MLAT, O=OGN, L=FLARM
        // ret.ava
        ret.ava = "暂不支持";
        // ret.src
        ret.src = "暂不支持";
        // ret.cla
        ret.cla = "暂不支持";
        RET<SSR::CPR::Position> pos = info->pos();
        if(pos.has_value()) {
            ret.lat = pos.value().lat;
            ret.lon = pos.value().lon;
        }
        ret.alt = info->altitude_meter();
        ret.spd = info->speed();
        ret.trk = info->air_magnetic_heading();
        ret.vrt = info->vertical_rate();
        // 飞机在地面还是空中 G for on ground, A for airborne (if TIS-B then lowercase g or a)
        ret.gda = info->is_on_ground() ? "G" : "A";
        auto vt = info->vortex_type();
        if (vt.has_value()) {
            ret.cat = Vortex_Type_to_radarcape_code(vt.value());
        }
        // =============== 来自DB 航班数据库 ================
        auto route_info = info->external_database_row("vrs_routes");
        if (!route_info) {
            route_info = info->external_database_row("adsblol_vrs_standing_data_routes");
        }
        if (!route_info) {
            route_info = info->external_database_row("vradarserver_standing_data_routes");
        }
        if (route_info) {
            const auto airport_codes = route_info->get("AirportCodes");
            if (airport_codes && !airport_codes->empty()) {
                const auto first_separator = airport_codes->find('-');
                const auto last_separator = airport_codes->rfind('-');
                ret.org = airport_codes->substr(0, first_separator);
                ret.dst = last_separator == std::string::npos
                              ? *airport_codes
                              : airport_codes->substr(last_separator + 1);
            }
        }
        //ret.opr
        const auto opensky_info =
            info->external_database_row("opensky_aircraft_database");
        if (opensky_info) {
            ret.opr = opensky_info->get("operator");
        }
        //ret.typ
        if (opensky_info) {
            ret.typ = opensky_info->get("typecode");
        }
        //ret.reg
        if (opensky_info) {
            ret.reg = opensky_info->get("registration");
        }
        // ===============================
        // squ	Mode-A (Squawk)		Mode-S	While this is a Mode-A code,
        // it can only be correlated in Mode-S targets. Mode-A or Mode-A/C targets are not
        // displayed in decoded formats since they cannot be correlated with a <hex> value
        ret.squ = info->squawk();
        // cou	Country		<hex>	Country according to Mode-S allocation block if decodable, mostly unusable/invalid for FLARM A/C
        //ret.cou
        const auto faa_info = info->external_database_row("faa_aircraft_registry");
        if (faa_info) {
            ret.cou = faa_info->get("country");
        }
        // dis	Distance (km or NM depending on configuration)			To the Receiver GNSS location
        auto p = db->base_station.get_pos();
        if (p.has_value() && pos.has_value()) {
            ret.dis = SSR::CPR::haversine(pos.value(), p.value());
        }
        ret.tru = info->times;
        ret.dbm = info->signal_level;
        //ret.lla
        ret.lla = std::time(nullptr) - info->timestamp;
        //ret.tmp
        ret.tmp = info->temperature();
        //ret.wsp
        ret.wsp = info->wind_speed();
        //ret.wdi
        ret.wdi = info->wind_direction();
        ret.mop = info->ADS_B_Ver();
        //ret.spi
        ret.spi = info->squawk();
        //ret.alr
        ret.alr = info->alart();
        auto air_speed = info->get_airspeed();
        if (air_speed.has_value()) {
            auto& t = air_speed.value();
            auto type = t.get_airspeed_type();
            if (type == SSR::Airspeed_type::Indicated_airspeed_IAS) {
                ret.ias = air_speed.value().get_speed();
            }
            if (type == SSR::Airspeed_type::True_airspeed_TAS) {
                ret.tas= air_speed.value().get_speed();
            }
            ret.hdgm = air_speed.value().get_magnetic_heading();
            ret.hdgt = "暂不支持";
        }
        //ret.qnhs   Selected QNH
        ret.qnhs = "暂不支持";
        //ret.alts   Selected Altitude
        ret.alts = "暂不支持";
        //ret.hdgs   Selected Heading
        ret.hdgs = info->select_heading();
        auto it = info->get_speed_base();
        if (it.has_value()) {
            auto i = it.value();
            i.get_GNSS_barometric_altitudes_difference();
            auto diff = i.get_GNSS_barometric_altitudes_difference();
            if (diff.has_value() && ret.alt.has_value()) {
                ret.altg =  diff.value() + ret.alt.value();
            }
        }
        //ret.pic
        ret.pic = "暂不支持";
        //ret.tcm
        ret.tcm = "暂不支持";
        // ape	Autopilot engaged	2	Mode-S
        //ret.ape
        ret.ape = "暂不支持";
        ret.sil = info->SIL();
        //ret.sda
        ret.sda = "暂不支持";
        ret.nacp = info->NACp();
        // pest	Number of position estimations per second		MLAT
        ret.pest = info->get_pest();
        // nocl	Number of sensors providing data aircraft		MLAT
        ret.nocl = 1;
        // tq	Time quality of this entry		Sensor	0: GNSS 1: Sensor NTP 2: Server NTP
        //ret.tq
        ret.tq = "暂不支持";
        return ret;
    }
};

