#include <cstring>
#include <iosfwd>
#include <iostream>
#include <string>

#include "main.h"
#include <Core/spdlog/export.h>
#include <cstring> // 蹇呴』鍖呭惈杩欎釜澶存枃浠朵互浣跨敤 std::memcpy
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>

using Func_Type = void(*)(MLAT_Data* buf, std::uint16_t* len);
Func_Type read_func_ptr{};


double lon = 121.473701;
double lat = 121.473701;

// void read(MLAT_Data* buf, std::uint16_t* len) {
//     // 第二条
//     strncpy(buf[0].icao, "XYZ789", sizeof(buf[1].icao));
//     buf[0].lon = lon;
//     buf[0].lat = lat;
//     lon += 0.001;
//     lat += 0.001;
//
//     *len = 1;
// }
time_t datetime_to_timestamp(const std::string& datetime) {
    std::tm tm = {};
    std::istringstream ss(datetime);
    ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
    if (ss.fail()) {
        throw std::runtime_error("时间格式错误");
    }
    // std::mktime按本地时区转换
    return std::mktime(&tm);
}

struct Base_MLAT_Data {
    MLAT_Data mlat_data;
    std::string msg;
};

std::vector<MLAT_Data> read_lines() {
    Base_Logger_Manager::instance()->init();
    std::multimap<size_t, Base_MLAT_Data> ordered_multimap;
    std::ifstream fin("C:/wyc/CLionProjects/plantSunCat/mainProjects/Radarcape/data/0701/info.txt");
    if (!fin) {
        std::cerr << "无法打开文件！" << std::endl;
        return {};
    }
    std::string line;
    while (std::getline(fin, line)) {
        if (line.empty()) continue;

        std::vector<std::string> fields;
        std::stringstream ss(line);
        std::string item;
        while (std::getline(ss, item, ';')) {
            fields.push_back(item);
        }
        // 检查字段数
        if (fields.size() < 9) continue;
        // 2025-07-03 10:16:21;VG_MSDW;28069B;2025-06-27 08:00:14;A800103FFF9CCB27FFFCA89DE0D3;121.691398;37.344925;8000;;0000;2_1_3
        // 按 1-base 提取
        std::string icao = fields[2];
        icao.resize(6, '_');
        std::string msg = fields[4];
        std::string lon = fields[5];
        std::string lat = fields[6];
        std::string alt = fields[7];


        std::string time = fields[3];
        // 输出（或者你可以存到 vector 里）
        // std::cout << icao << " " << lat << " " << lon << " " << alt << std::endl;

        Base_MLAT_Data b;
        //cur.icao = icao;
        b.mlat_data.lon = stod(lon);
        b.mlat_data.lat = stod(lat);
        b.msg = line + "\n";


        //std::cout << cur.lat << "  " << cur.lon << std::endl;
        // if (35 < cur.lat && cur.lat < 37
        //     &&
        //     120 < cur.lon && cur.lon < 122
        // ) {
        //
        //     pure_log("@/logs/mlat_.log", line + "\n");
        // }


        size_t icao_size = icao.size();


        strncpy(b.mlat_data.icao, icao.c_str(), 6);
        ordered_multimap.insert({datetime_to_timestamp(time), b});
    }
    std::vector<MLAT_Data> list;
    for (auto it : ordered_multimap) {
        Base_MLAT_Data& b = it.second;
        list.push_back(b.mlat_data);
        auto icao = std::string(b.mlat_data.icao, 6);
        //if (icao == "78139F")

       // 58161E_mlat_result.log

            pure_log("@/logs/icao/" +  icao + "_mlat_result.log", b.msg);

    }
    return list;
}
std::vector<MLAT_Data> all;

void read(MLAT_Data* buf, std::uint16_t* len) {
    if (all.empty()) {
        all = read_lines();
    }
    static size_t pos = 0; // 上一次拷贝结束后的位置
    const size_t BATCH = 100;
    size_t total = all.size();
    size_t n = std::min(BATCH, total - pos); // 本次实际要拷贝多少

    for (size_t i = 0; i < n; ++i) {
        buf[i] = all[pos + i];
    }
    *len = static_cast<std::uint16_t>(n);

    pos += n;
    if (pos >= total) {
        pos = 0; // 如果需要循环一轮后归零，否则你可以不归零
    }
}





int main() {
    // std::uint16_t size;
    // MLAT_Data arr[1000];
    // read(arr, &size);

    printf("默认最大文件数: %d\n", _getmaxstdio());
    _setmaxstdio(2048); // 可以设置更高（最高8192，部分环境2048）
    printf("修改后最大文件数: %d\n", _getmaxstdio());


    for (int i = 0; i < 1000; ++i) {
        pure_log("@/logs/num/" + std::to_string(i) + ".log", "\n");
    }
}
