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

#include "AuthenticationStatusDocumentApiImpl.h"

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

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

AuthenticationStatusDocumentApiImpl::AuthenticationStatusDocumentApiImpl(
    std::shared_ptr<Pistache::Rest::Router> rtr, MYSQL *mysql)
    : AuthenticationStatusDocumentApi(rtr) {
  mysql_WitcommUDRDB = mysql;
}

void AuthenticationStatusDocumentApiImpl::create_authentication_status(
    const std::string &ueId, const AuthEvent &authEvent,
    Pistache::Http::ResponseWriter &response) {
  // response.send(Pistache::Http::Code::Ok, "create_authentication_status\n");
  MYSQL_RES *res = NULL;
  MYSQL_ROW row;
  const std::string select_AuthenticationStatus =
      "select * from AuthenticationStatus WHERE ueid=" + ueId;
  std::string query;
  nlohmann::json j;

  if (mysql_real_query(mysql_WitcommUDRDB, select_AuthenticationStatus.c_str(),
                       (unsigned long)select_AuthenticationStatus.size())) {
    std::cout << "mysql_real_query failure！" << std::endl;
    return;
  }

  res = mysql_store_result(mysql_WitcommUDRDB);
  if (res == NULL) {
    std::cout << "mysql_store_result failure！" << std::endl;
    return;
  }
  if (mysql_num_rows(res)) {
    query = "update AuthenticationStatus set nfInstanceId='" +
            authEvent.getNfInstanceId() + "'" +
            ",success=" + (authEvent.isSuccess() ? "1" : "0") + ",timeStamp='" +
            authEvent.getTimeStamp() + "'" + ",servingNetworkName='" +
            authEvent.getServingNetworkName() + "'" +
            (authEvent.authRemovalIndIsSet()
                 ? (authEvent.isAuthRemovalInd() ? ",authRemovalInd=1"
                                                 : ",authRemovalInd=0")
                 : "");
    to_json(j, authEvent.getAuthType());
    query += ",authType='" + j.dump() + "'";
    query += " where ueid=" + ueId;
  } else {
    query = "insert into AuthenticationStatus set ueid=" + ueId +
            ",nfInstanceId='" + authEvent.getNfInstanceId() + "'" +
            ",success=" + (authEvent.isSuccess() ? "1" : "0") + ",timeStamp='" +
            authEvent.getTimeStamp() + "'" + ",servingNetworkName='" +
            authEvent.getServingNetworkName() + "'" +
            (authEvent.authRemovalIndIsSet()
                 ? (authEvent.isAuthRemovalInd() ? ",authRemovalInd=1"
                                                 : ",authRemovalInd=0")
                 : "");
    to_json(j, authEvent.getAuthType());
    query += ",authType='" + j.dump() + "'";
  }

  mysql_free_result(res);
  //    std::cout << query << std::endl;
  if (mysql_real_query(mysql_WitcommUDRDB, query.c_str(),
                       (unsigned long)query.size())) {
    std::cout << "mysql_real_query failure！" << std::endl;
    return;
  }

  response.send(Pistache::Http::Code::No_Content, "");
}

void AuthenticationStatusDocumentApiImpl::delete_authentication_status(
    const std::string &ueId, Pistache::Http::ResponseWriter &response) {
  // response.send(Pistache::Http::Code::Ok, "delete_authentication_status\n");
  const std::string query =
      "DELETE from AuthenticationStatus WHERE ueid=" + ueId;

  if (mysql_real_query(mysql_WitcommUDRDB, query.c_str(),
                       (unsigned long)query.size())) {
    std::cout << "mysql_real_query failure！" << std::endl;
    return;
  }

  response.send(Pistache::Http::Code::No_Content, "");
}
void AuthenticationStatusDocumentApiImpl::query_authentication_status(
    const std::string &ueId,
    const Pistache::Optional<std::vector<std::string>> &fields,
    const Pistache::Optional<std::string> &supportedFeatures,
    Pistache::Http::ResponseWriter &response) {
  MYSQL_RES *res = NULL;
  MYSQL_ROW row;
  MYSQL_FIELD *field = nullptr;

  nlohmann::json j;

  AuthEvent authenticationstatus;
  const std::string query =
      "select * from AuthenticationStatus WHERE ueid=" + ueId;

  if (mysql_real_query(mysql_WitcommUDRDB, query.c_str(),
                       (unsigned long)query.size())) {
    std::cout << "mysql_real_query failure！" << std::endl;
    return;
  }

  res = mysql_store_result(mysql_WitcommUDRDB);
  if (res == NULL) {
    std::cout << "mysql_store_result failure！" << std::endl;
    return;
  }

  row = mysql_fetch_row(res);
  if (row != NULL) {
    for (int i = 0; field = mysql_fetch_field(res); i++) {
      if (!strcmp("nfInstanceId", field->name)) {
        authenticationstatus.setNfInstanceId(row[i]);
      } else if (!strcmp("success", field->name)) {
        if (strcmp(row[i], "0"))
          authenticationstatus.setSuccess(true);
        else
          authenticationstatus.setSuccess(false);
      } else if (!strcmp("timeStamp", field->name)) {
        authenticationstatus.setTimeStamp(row[i]);
      } else if (!strcmp("authType", field->name)) {
        AuthType authtype;
        nlohmann::json::parse(row[i]).get_to(authtype);
        authenticationstatus.setAuthType(authtype);
      } else if (!strcmp("servingNetworkName", field->name)) {
        authenticationstatus.setServingNetworkName(row[i]);
      } else if (!strcmp("authRemovalInd", field->name) && row[i] != NULL) {
        if (strcmp(row[i], "0"))
          authenticationstatus.setAuthRemovalInd(true);
        else
          authenticationstatus.setAuthRemovalInd(false);
      }
    }

    to_json(j, authenticationstatus);
    response.send(Pistache::Http::Code::Ok, j.dump());
  } else {
    std::cout << "AuthenticationStatus no data！" << std::endl;
  }

  mysql_free_result(res);
}

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