/**
 * AUSF API
 * AUSF UE Authentication Service. © 2020, 3GPP Organizational Partners (ARIB,
 * ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
 *
 * The version of the OpenAPI document: 1.1.1
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator
 * (https://openapi-generator.tech). https://openapi-generator.tech Do not edit
 * the class manually.
 */

#include "DefaultApi.h"
#include "Helpers.h"
#include "logger.hpp"
#include <iostream>

using namespace std;

namespace org {
namespace openapitools {
namespace server {
namespace api {

using namespace org::openapitools::server::helpers;
using namespace org::openapitools::server::model;

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

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

void DefaultApi::setupRoutes() {
  using namespace Pistache::Rest;

  Routes::Post(*router, base + "/ue-authentications/:authCtxId/eap-session",
               Routes::bind(&DefaultApi::eap_auth_method_handler, this));
  Routes::Post(
      *router, base + "/rg-authentications",
      Routes::bind(&DefaultApi::rg_authentications_post_handler, this));
  Routes::Put(
      *router, base + "/ue-authentications/:authCtxId/5g-aka-confirmation",
      Routes::bind(
          &DefaultApi::
              ue_authentications_auth_ctx_id5g_aka_confirmation_put_handler,
          this));
  Routes::Post(
      *router, base + "/ue-authentications/deregister",
      Routes::bind(&DefaultApi::ue_authentications_deregister_post_handler,
                   this));
  Routes::Post(
      *router, base + "/ue-authentications",
      Routes::bind(&DefaultApi::ue_authentications_post_handler, this));

  // Default handler, called when a route is not found
  router->addCustomHandler(
      Routes::bind(&DefaultApi::default_api_default_handler, this));
}

void DefaultApi::eap_auth_method_handler(
    const Pistache::Rest::Request &request,
    Pistache::Http::ResponseWriter response) {
  // Getting the path params
  auto authCtxId = request.param(":authCtxId").as<std::string>();

  // Getting the body param

  EapSession eapSession;

  try {
    nlohmann::json::parse(request.body()).get_to(eapSession);
    this->eap_auth_method(authCtxId, eapSession, 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) {
    // send a 500 error
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
}



void DefaultApi::rg_authentications_post_handler(
    const Pistache::Rest::Request &request,
    Pistache::Http::ResponseWriter response) {

  // Getting the body param

  RgAuthenticationInfo rgAuthenticationInfo;

  try {
    nlohmann::json::parse(request.body()).get_to(rgAuthenticationInfo);
    this->rg_authentications_post(rgAuthenticationInfo, 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) {
    // send a 500 error
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
}

void DefaultApi::ue_authentications_auth_ctx_id5g_aka_confirmation_put_handler(
    const Pistache::Rest::Request &request,
    Pistache::Http::ResponseWriter response) {
      Logger::ausf_server().info("--Received 5g_aka_confirmation put Request");
  // Getting the path params
  auto authCtxId = request.param(":authCtxId").as<std::string>();
  Logger::ausf_server().info("5gaka confirmation received with authctxID %s",authCtxId.c_str());

  // Getting the body param

  ConfirmationData confirmationData;

  try {
    nlohmann::json::parse(request.body()).get_to(confirmationData);
    Logger::ausf_server().debug("json parsed");
    this->ue_authentications_auth_ctx_id5g_aka_confirmation_put(
        authCtxId, confirmationData, response);
  } catch (nlohmann::detail::exception &e) {
    // send a 400 error
    Logger::ausf_server().error("**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) {
    // send a 500 error
    Logger::ausf_server().error("**500 error");
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
}

void DefaultApi::ue_authentications_deregister_post_handler(
    const Pistache::Rest::Request &request,
    Pistache::Http::ResponseWriter response) {

  // Getting the body param

  DeregistrationInfo deregistrationInfo;

  try {
    nlohmann::json::parse(request.body()).get_to(deregistrationInfo);
    this->ue_authentications_deregister_post(deregistrationInfo, 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) {
    // send a 500 error
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
}

void DefaultApi::ue_authentications_post_handler(
    const Pistache::Rest::Request &request,
    Pistache::Http::ResponseWriter response) {
//   cout << "------------ue authentications post handler---------" << endl;
  Logger::ausf_server().info("--Received ue-authentications post Request");

  // Getting the body param

  AuthenticationInfo authenticationInfo;

  try {
    nlohmann::json::parse(request.body()).get_to(authenticationInfo);
    // Logger::ausf_server().debug("json parsed");
    this->ue_authentications_post(authenticationInfo, response);
  } catch (nlohmann::detail::exception &e) {
    // send a 400 error
    Logger::ausf_server().error("**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) {
    // send a 500 error
    Logger::ausf_server().error("**500 error");
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
}

void DefaultApi::default_api_default_handler(
    const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) {

  Logger::ausf_server().info("--in default api handler--");
  response.send(Pistache::Http::Code::Not_Found,
                "The requested method does not exist-default");
}

} // namespace api
} // namespace server
} // namespace openapitools
} // namespace org
