/**
 * 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 "SDMSubscriptionsCollectionApiImpl.h"

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

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

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

void SDMSubscriptionsCollectionApiImpl::create_sdm_subscriptions(
    const std::string &ueId, SdmSubscription &sdmSubscription,
    Pistache::Http::ResponseWriter &response) {
  MYSQL_RES *res = NULL;
  MYSQL_ROW row;
  nlohmann::json j;

  int32_t subsId = 0;
  int32_t count = 0;
  std::string query = "SELECT subsId from SdmSubscriptions 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;
  }

  while (row = mysql_fetch_row(res)) {
    count++;
    if (strcmp(row[0], std::to_string(count).c_str())) {
      subsId = count;
      break;
    }
  }
  mysql_free_result(res);

  //****** add query *******

  query =
      "insert into SdmSubscriptions set ueid=" + ueId + ",nfInstanceId='" +
      sdmSubscription.getNfInstanceId() + "'" +
      (sdmSubscription.implicitUnsubscribeIsSet()
           ? (sdmSubscription.isImplicitUnsubscribe()
                  ? ",implicitUnsubscribe=1"
                  : ",implicitUnsubscribe=0")
           : "") +
      (sdmSubscription.expiresIsSet()
           ? ",expires='" + sdmSubscription.getExpires() + "'"
           : "") +
      ",callbackReference='" + sdmSubscription.getCallbackReference() + "'" +
      (sdmSubscription.dnnIsSet() ? ",dnn='" + sdmSubscription.getDnn() + "'"
                                  : "") +
      (sdmSubscription.subscriptionIdIsSet()
           ? ",subscriptionId='" + sdmSubscription.getSubscriptionId() + "'"
           : "") +
      (sdmSubscription.immediateReportIsSet()
           ? (sdmSubscription.isImmediateReport() ? ",immediateReport=1"
                                                  : ",immediateReport=0")
           : "") +
      (sdmSubscription.supportedFeaturesIsSet()
           ? ",supportedFeatures='" + sdmSubscription.getSupportedFeatures() +
                 "'"
           : "");

  if (sdmSubscription.amfServiceNameIsSet()) {
    to_json(j, sdmSubscription.getAmfServiceName());
    query += ",amfServiceName='" + j.dump() + "'";
  }
  if (sdmSubscription.singleNssaiIsSet()) {
    to_json(j, sdmSubscription.getSingleNssai());
    query += ",singleNssai='" + j.dump() + "'";
  }
  if (sdmSubscription.plmnIdIsSet()) {
    to_json(j, sdmSubscription.getPlmnId());
    query += ",plmnId='" + j.dump() + "'";
  }
  if (sdmSubscription.reportIsSet()) {
    to_json(j, sdmSubscription.getReport());
    query += ",report='" + j.dump() + "'";
  }
  if (sdmSubscription.contextInfoIsSet()) {
    to_json(j, sdmSubscription.getContextInfo());
    query += ",contextInfo='" + j.dump() + "'";
  }

  nlohmann::json MonitoredResourceUris_json(
      sdmSubscription.getMonitoredResourceUris());
  query += ",monitoredResourceUris='" + MonitoredResourceUris_json.dump() + "'";

  if (subsId && (subsId == count)) {
    query += ",subsId=" + std::to_string(subsId);
  }

  //************************

  //    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;
  }

  to_json(j, sdmSubscription);
  response.send(Pistache::Http::Code::Created, j.dump());
}
void SDMSubscriptionsCollectionApiImpl::querysdmsubscriptions(
    const std::string &ueId,
    const Pistache::Optional<std::string> &supportedFeatures,
    Pistache::Http::ResponseWriter &response) {
  MYSQL_RES *res = NULL;
  MYSQL_ROW row;
  MYSQL_FIELD *field = nullptr;
  std::vector<std::string> fields;

  nlohmann::json j, tmp;

  const std::string query = "SELECT * from SdmSubscriptions 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;
  }

  while (field = mysql_fetch_field(res)) {
    fields.push_back(field->name);
  }

  j.clear();

  while (row = mysql_fetch_row(res)) {
    SdmSubscription sdmsubscriptions;

    tmp.clear();

    for (int i = 0; i < fields.size(); i++) {
      if (!strcmp("nfInstanceId", fields[i].c_str())) {
        sdmsubscriptions.setNfInstanceId(row[i]);
      } else if (!strcmp("implicitUnsubscribe", fields[i].c_str()) &&
                 row[i] != NULL) {
        if (strcmp(row[i], "0"))
          sdmsubscriptions.setImplicitUnsubscribe(true);
        else
          sdmsubscriptions.setImplicitUnsubscribe(false);
      } else if (!strcmp("expires", fields[i].c_str()) && row[i] != NULL) {
        sdmsubscriptions.setExpires(row[i]);
      } else if (!strcmp("callbackReference", fields[i].c_str())) {
        sdmsubscriptions.setCallbackReference(row[i]);
      } else if (!strcmp("amfServiceName", fields[i].c_str()) &&
                 row[i] != NULL) {
        ServiceName amfservicename;
        nlohmann::json::parse(row[i]).get_to(amfservicename);
        sdmsubscriptions.setAmfServiceName(amfservicename);
      } else if (!strcmp("monitoredResourceUris", fields[i].c_str())) {
        std::vector<std::string> monitoredresourceuris;
        nlohmann::json::parse(row[i]).get_to(monitoredresourceuris);
        sdmsubscriptions.setMonitoredResourceUris(monitoredresourceuris);
      } else if (!strcmp("singleNssai", fields[i].c_str()) && row[i] != NULL) {
        Snssai singlenssai;
        nlohmann::json::parse(row[i]).get_to(singlenssai);
        sdmsubscriptions.setSingleNssai(singlenssai);
      } else if (!strcmp("dnn", fields[i].c_str()) && row[i] != NULL) {
        sdmsubscriptions.setDnn(row[i]);
      } else if (!strcmp("subscriptionId", fields[i].c_str()) &&
                 row[i] != NULL) {
        sdmsubscriptions.setSubscriptionId(row[i]);
      } else if (!strcmp("plmnId", fields[i].c_str()) && row[i] != NULL) {
        PlmnId plmnid;
        nlohmann::json::parse(row[i]).get_to(plmnid);
        sdmsubscriptions.setPlmnId(plmnid);
      } else if (!strcmp("immediateReport", fields[i].c_str()) &&
                 row[i] != NULL) {
        if (strcmp(row[i], "0"))
          sdmsubscriptions.setImmediateReport(true);
        else
          sdmsubscriptions.setImmediateReport(false);
      } else if (!strcmp("report", fields[i].c_str()) && row[i] != NULL) {
        SubscriptionDataSets report;
        nlohmann::json::parse(row[i]).get_to(report);
        sdmsubscriptions.setReport(report);
      } else if (!strcmp("supportedFeatures", fields[i].c_str()) &&
                 row[i] != NULL) {
        sdmsubscriptions.setSupportedFeatures(row[i]);
      } else if (!strcmp("contextInfo", fields[i].c_str()) && row[i] != NULL) {
        ContextInfo contextinfo;
        nlohmann::json::parse(row[i]).get_to(contextinfo);
        sdmsubscriptions.setContextInfo(contextinfo);
      }
    }
    to_json(tmp, sdmsubscriptions);
    j += tmp;
  }

  mysql_free_result(res);

  response.send(Pistache::Http::Code::Ok, j.dump());
}

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