#include "report_svr.h"

#include <httplib.h>
#include <nlohmann/json.hpp>

#include "common/logging.h"

namespace zsy
{
    ReportSvr::ReportSvr(const nlohmann::json &config)
        : config(config.get<ReportServerConfig>().reportSvr)
    {
    }

    ReportPassResult ReportSvr::reportPass(const std::string &license, const std::string &sn)
    {
        try
        {
            const httplib::Headers headers = {
                {"Content-Type", "application/json"}
            };
            ReportData reportData;
            reportData.carNumber = license;
            reportData.carDiscernCode = sn;

            nlohmann::json data = reportData;

            auto body = data.dump();

            LOGGER_INFO("上报数据：{}{} {}", config.server, config.passUrl, body);

            httplib::Client cli(config.server);
            cli.enable_server_certificate_verification(false);
            cli.set_connection_timeout(5);
            cli.set_read_timeout(10);
            cli.set_write_timeout(10);
            auto response = cli.Post(config.passUrl.c_str(), headers, body, "application/json");
            if (response)
            {
                LOGGER_INFO("上报结果：{}", response->body);
                const int status = response->status;
                if (status >= 300 || status < 200)
                {
                    LOGGER_ERROR("上报失败，错误码：{}", status);
                    return {.code = 1, .msg = "上报失败", .data = {.type = 0, .errMsg = "上报失败"}};
                } else
                {
                    auto json = nlohmann::json::parse(response->body);
                    return json.get<ReportPassResult>();
                }
            } else
            {
                LOGGER_ERROR("上报失败，错误码：{}", httplib::to_string(response.error()));
                return {.code = 1, .msg = "上报失败", .data = {.type = 0, .errMsg = "上报失败"}};
            }
        } catch (std::exception &e)
        {
            LOGGER_ERROR("上报失败：{}", e.what());
            return {.code = 1, .msg = "上报失败", .data = {.type = 0, .errMsg = "上报失败"}};
        }
        catch (...)
        {
            LOGGER_ERROR("未知异常，上报失败");
            return {.code = 1, .msg = "上报失败", .data = {.type = 0, .errMsg = "上报失败"}};
        }
    }

    ReportResult ReportSvr::report(const std::string &license,
                                   const std::string &sn,
                                   const double weight,
                                   const std::string &carFrontUrl,
                                   const std::string &carBodyPhotoUrl,
                                   const std::string &orderNo)
    {
        try
        {
            const httplib::Headers headers = {
                {"Content-Type", "application/json"}
            };
            ReportData reportData;
            reportData.carNumber = license;
            reportData.carDiscernCode = sn;
            reportData.weight = weight;
            reportData.photoFront = carFrontUrl;
            reportData.photoBody = carBodyPhotoUrl;
            reportData.orderNo = orderNo;
            reportData.onlyCar = 0;

            nlohmann::json data = reportData;

            auto body = data.dump();

            LOGGER_INFO("上报数据：{}{} {}", config.server, config.reportUrl, body);

            httplib::Client cli(config.server);
            cli.enable_server_certificate_verification(false);
            cli.set_connection_timeout(5);
            cli.set_read_timeout(10);
            cli.set_write_timeout(10);
            auto response = cli.Post(config.reportUrl.c_str(), headers, body, "application/json");
            if (response)
            {
                LOGGER_INFO("上报结果：{}", response->body);
                const int status = response->status;
                if (status >= 300 || status < 200)
                {
                    LOGGER_ERROR("上报失败，错误码：{}", status);
                    return {.code = 1, .msg = "上报失败"};
                } else
                {
                    auto json = nlohmann::json::parse(response->body);
                    return json.get<ReportResult>();
                }
            } else
            {
                LOGGER_ERROR("上报失败，错误码：{}", httplib::to_string(response.error()));
                return {.code = 1, .msg = "上报失败"};
            }
        } catch (std::exception &e)
        {
            LOGGER_ERROR("上报失败：{}", e.what());
            return {.code = 1, .msg = "上报失败"};
        }
        catch (...)
        {
            LOGGER_ERROR("未知异常，上报失败");
            return {.code = 1, .msg = "上报失败"};
        }
    }
} // zsy
