#pragma once

#include "abstract_coder.hpp"
#include <cstdint>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

namespace tinyrpc {
static thread_local std::string t_msg_req_nu;
static thread_local std::string t_max_msg_req_nu;
static int g_random_fd = -1;
struct TinypbData : public AbstractData {
    static std::string genMsgNumber()
    {
        int t_msg_req_len = 8;

        if (t_msg_req_nu.empty() || t_msg_req_nu == t_max_msg_req_nu) {
            if (g_random_fd == -1) {
                g_random_fd = open("/dev/urandom", O_RDONLY);
            } 
            std::string res(t_msg_req_len, 0);

            if ((read(g_random_fd, &res[0], t_msg_req_len)) != t_msg_req_len) {
                return "";
            }
            t_max_msg_req_nu = "";

            for (int i = 0; i < t_msg_req_len; ++i) {
                uint8_t x = ((uint8_t)(res[i])) % 10;
                res[i] = x + '0';
                t_max_msg_req_nu += "9";
            }

            t_msg_req_nu = res;


        } else {
            int i = t_msg_req_nu.length() - 1;
            while (t_msg_req_nu[i] == '9' && i >= 0) {
                i--;
            }
            if (i >= 0) {
                t_msg_req_nu[i] += 1;
                for (size_t j = i + 1; j < t_msg_req_nu.length(); ++j) {
                    t_msg_req_nu[j] = '0';
                }
            }
        }
        return t_msg_req_nu;
    }


    TinypbData() {};
    ~TinypbData() {};

    // char start = 0x02;                      // indentify start of a TinyPb protocal data
    int32_t pk_len { 0 }; // len of all package(include start char and end char)
    int32_t msg_req_len { 0 }; // len of msg_req
    std::string msg_req; // msg_req, which identify a request
    int32_t service_name_len { 0 }; // len of service full name
    std::string service_full_name; // service full name, like QueryService.query_name
    int32_t err_code { 0 }; // err_code, 0 -- call rpc success, otherwise -- call rpc failed. it only be seted by RpcController
    int32_t err_info_len { 0 }; // len of err_info
    std::string err_info; // err_info, empty -- call rpc success, otherwise -- call rpc failed, it will display details of reason why call rpc failed. it only be seted by RpcController
    std::string pb_data; // business pb data
    int32_t check_num { -1 }; // check_num of all package. to check legality of data
    // char end = 0x03;                        // identify end of a TinyPb protocal data
};

}