#include "test.h"
#include "application.h"
#include "common/date_util.h"
#include "common/snowflake.h"
#include <fmt/core.h>

namespace zsy
{
    Test::Test() : type(0)
    {
        Application::httpSvr->getEndpoint("/type", [this](const httplib::Request &req, httplib::Response &res)
        {
            std::string type = req.get_param_value("type");
            if (type.empty())
            {
                res.set_content("未指定类型：0-->前置、1-->进、2-->出", "application/json");
                return;
            }
            this->type = std::stoi(type);

            res.set_content("已改为" + type, "application/json");
        });

        Application::httpSvr->postEndpoint("/api/site/report-car-pass", [this](const httplib::Request &req, httplib::Response &res)
        {
            auto type = this->type;
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
            res.set_content( fmt::format(
                R"({{
                          "code": 0,
                          "msg": "",
                          "data": {{
                            "car_number": "",
                            "type": {},
                            "order_no": "",
                            "err_msg": "",
                            "scene": "",
                            "no_weight": false
                          }}
                        }})", type), "application/json");
        });
        Application::httpSvr->postEndpoint("/api/site/report-v2", [this](const httplib::Request &req, httplib::Response &res)
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
            std::string data = R"({
                              "code": 0,
                              "msg": "",
                              "data": {
                                "car_number": "",
                                "order_no": "",
                                "lane_code": ""
                              }
                            })";
            res.set_content(data, "application/json");
        });
        Application::httpSvr->postEndpoint("/v1/speech", [this](const httplib::Request &req, httplib::Response &res)
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
            std::string data = R"({
                              "code": 200,
                              "message": "播放成功"
                            })";
            res.set_content(data, "application/json");
        });

        Application::httpSvr->getEndpoint("/weight", [](const httplib::Request &req, httplib::Response &res)
        {
            nlohmann::json j;
            try
            {
                auto sn = req.get_param_value("sn");
                double weight = Application::deviceHolder->getPlatformScale(sn)->reading();
                j["结果"] = weight;
            } catch (std::exception &e)
            {
                LOGGER_ERROR("错误", e.what());
                j["结果"] = "错误";
            } catch (...)
            {
                LOGGER_ERROR("错误");
                j["结果"] = "错误";
            }
            res.set_content(j.dump(), "application/json");
        });

        Application::httpSvr->getEndpoint("/opendoor", [](const httplib::Request &req, httplib::Response &res)
        {
            nlohmann::json j;
            try
            {
                auto sn = req.get_param_value("sn");
                Application::deviceHolder->getBarrier(sn)->open();
                j["结果"] = "成功";
            } catch (std::exception &e)
            {
                LOGGER_ERROR("错误", e.what());
                j["结果"] = "错误";
            } catch (...)
            {
                LOGGER_ERROR("错误");
                j["结果"] = "错误";
            }
            res.set_content(j.dump(), "application/json");
        });

        Application::httpSvr->getEndpoint("/play", [](const httplib::Request &req, httplib::Response &res)
        {
            nlohmann::json j;
            try
            {
                auto sn = req.get_param_value("sn");
                Application::deviceHolder->getSoundColumn(sn)->play("欢迎光临");
                j["结果"] = "成功";
            } catch (std::exception &e)
            {
                LOGGER_ERROR("错误", e.what());
                j["结果"] = "错误";
            } catch (...)
            {
                LOGGER_ERROR("错误");
                j["结果"] = "错误";
            }
            res.set_content(j.dump(), "application/json");
        });

        Application::httpSvr->getEndpoint("/photo", [](const httplib::Request &req, httplib::Response &res)
        {
            nlohmann::json j;
            try
            {
                auto sn = req.get_param_value("sn");
                auto photograph = Application::deviceHolder->getVidicon(sn)->photograph();
                std::string date = DateUtil::format(Ymd);
                auto photoId = Snowflake::genIdStr();
                auto [carFrontUrlSucc,carFrontUrl] = Application::oss->upload(date + "/" + "front_" + photoId + ".jpg", photograph);
                j["结果"] = carFrontUrlSucc;
                j["地址"] = carFrontUrl;
            } catch (std::exception &e)
            {
                LOGGER_ERROR("错误", e.what());
                j["结果"] = "错误";
            } catch (...)
            {
                LOGGER_ERROR("错误");
                j["结果"] = "错误";
            }
            res.set_content(j.dump(), "application/json");
        });
    }
}
