#include "AmfToDataApi.h"
#include "Helpers.h"
#include <nlohmann/json.hpp>
#include "logger.hpp"
#include "amf_config.hpp"

extern config::amf_config amf_cfg;

namespace oai {
namespace amf {
namespace api {

using namespace oai::amf::helpers;


AmfToDataApi::AmfToDataApi(
    std::shared_ptr<Pistache::Rest::Router> rtr) {
  router = rtr;
}

void AmfToDataApi::init() {
  setupRoutes();
}

void AmfToDataApi::setupRoutes() {
  using namespace Pistache::Rest;
  //获取gnb数列表
  Routes::Get(
        *router, base + amf_cfg.sbi_api_version +"/query_connected_gnbs/", 
        Routes::bind(&AmfToDataApi::gnbs_get_am_data_handler, this));
 router->addCustomHandler(Routes::bind(&AmfToDataApi::gnbs_default_handler, this));
  
  //获取ue数列表
  Routes::Get(
        *router, base + amf_cfg.sbi_api_version + "/query_connected_ues/", 
        Routes::bind(&AmfToDataApi::ues_get_am_data_handler, this));
  router->addCustomHandler(Routes::bind(&AmfToDataApi::ues_default_handler, this));
  
}
//获取gnb数列表
void AmfToDataApi::gnbs_get_am_data_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
    
    try {
         //Logger::amf_server().info( "==============Api=======获取gnb数列表 =========");
         this->gnbs_get_am_data(response);
    } catch (nlohmann::detail::exception &e) {
        //send a 400 error
        response.send(Pistache::Http::Code::Bad_Request, e.what());
        return;
    } catch (Pistache::Http::HttpError &e) {
        response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
        return;
    } catch (std::exception &e) {
        response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
        return;
    }
}
void AmfToDataApi::gnbs_default_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) {
    response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist");
}
//获取ue数列表
void AmfToDataApi::ues_get_am_data_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
    
    try {
         this->ues_get_am_data(response);
    } catch (nlohmann::detail::exception &e) {
        //send a 400 error
        response.send(Pistache::Http::Code::Bad_Request, e.what());
        return;
    } catch (Pistache::Http::HttpError &e) {
        response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
        return;
    } catch (std::exception &e) {
        response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
        return;
    }
}
void AmfToDataApi::ues_default_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) {
    response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist");
    
}
}  // namespace api
}  // namespace amf
}  // namespace oai

