#include "DataOutputFormats.h"

#include <cassert>
#include <chrono>

using namespace yC;

MLAT_timestamp::MLAT_timestamp() {
    std::chrono::time_point<std::chrono::high_resolution_clock> t = std::chrono::high_resolution_clock::now();
    // 获取当前的系统时间（秒）
    std::chrono::duration duration_since_epoch = t.time_since_epoch();
    // 获取秒数和纳秒数
    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration_since_epoch);
    auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(duration_since_epoch);
    // 获取当天的秒数和纳秒数
    this->daysec = seconds.count() % 86400; // 86400 = 24 * 60 * 60 (一天的秒数)
    this->nanosec = nanoseconds.count() % 86400000000000; // 1秒 = 10^9纳秒
    memory = bin2mem(to_bin_string());
    parse_hhmmss();
}


MLAT_timestamp::MLAT_timestamp(const std::string& memory_6) : memory(memory_6) {
    assert(memory_6.size() == 6);
    auto msg_bin = hex2bin(mem2hex(memory));
    daysec = std::bitset<18>(msg_bin.substr(0, 18)).to_ulong();
    nanosec = std::bitset<30>(msg_bin.substr(18, 30)).to_ulong();
    parse_hhmmss();

    //assert(memory_6 == bin2mem(to_bin_string()), "MLAT_timestamp，内存转换错误！");
}

MLAT_timestamp::MLAT_timestamp(std::uint32_t daysec, std::uint32_t nanosec)  : daysec(daysec), nanosec(nanosec) {
    parse_hhmmss();
}


std::string MLAT_timestamp::to_bin_string() const {
    std::bitset<18> daysec_bits = daysec;
    std::bitset<30> nanosec_bits = nanosec;
    return daysec_bits.to_string() + nanosec_bits.to_string();
}

// std::string MLAT_timestamp::to_memory() {
//     return bin2memory(to_bin_string());
// }

std::string MLAT_timestamp::to_memory() const {
    return memory;
}

void MLAT_timestamp::parse_hhmmss() {
    hh         = (daysec/3600) % 24;
    mm         = (daysec/60)   % 60;
    ss         =  daysec       % 60;
}
std::string packet_to_escape_format(std::string t)  {
    std::string result = "\x1a";
    for (size_t i = 1; i < t.size(); ++i) {
        result.push_back(t[i]);  // 插入当前字符
        if (t[i] == '\x1a') {
            result.push_back('\x1a');  // 如果是 0x1a，插入另一个 0x1a
        }
    }
    return result;
}
void Binary_Format_handle_buffer(std::string& buffer, const std::string& data, const std::function<void(std::string&)>& callback) {
    size_t i = 0;
    while (i < data.size()) {
        if (data[i] == 0x1A) {
            // 如果遇到起始标记 0x1A
            if (i + 1 < data.size() && data[i + 1] == 0x1A) {
                // 如果是转义的 0x1A 0x1A，继续处理
                buffer.push_back(0x1A); // 添加一个正常的 0x1A 字节
                i += 2;  // 跳过这两个字节
            } else {
                // 检测到起始标记 0x1A，认为是新消息的开始
                if (!buffer.empty()) {
                    // 如果当前消息 buffer 中已有数据，调用回调函数处理当前消息
                    callback(buffer);
                    buffer.clear();  // 清空当前消息的缓存
                }
                buffer.push_back(0x1A);
                // 跳过当前的 SOP 标记 (0x1A)
                i++;
            }
        } else
        {
            // 如果不是 0x1A，直接将数据添加到 buffer 中
            buffer.push_back(data[i]);
            i++;
        }
    }
}



// 9 + 2
// 9 + 7
// 9 + 14
std::string mode_s_msg_packet_to_server_packet(std::uint32_t id, std::string packet)  {
    std::string id_code = yC::platform_2_big_endian((char*)&id, sizeof(id));
    //assert(id_code.size() == 4, "id.size != 4");
    packet.insert(1, id_code.c_str(), 4);
    return packet;
}