/**
 * Nsmf_EventExposure
 * Session Management Event Exposure Service. © 2019, 3GPP Organizational
 * Partners (ARIB, ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
 *
 * The version of the OpenAPI document: 1.1.0.alpha-1
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator
 * (https://openapi-generator.tech). https://openapi-generator.tech Do not edit
 * the class manually.
 */

/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

#include "3gpp_29.500.h"
#include "logger.hpp"
#include "itti_msg_sbi.hpp"
#include "StatusNotifyApiImpl.h"
#include "conversions.hpp"

extern itti_mw* itti_inst;

namespace oai {
namespace amf {
namespace api {

using namespace oai::amf::model;

StatusNotifyApiImpl::StatusNotifyApiImpl(
    std::shared_ptr<Pistache::Rest::Router> rtr,
    amf_application::amf_app* amf_app_inst)
    : StatusNotifyApi(rtr), m_amf_app(amf_app_inst) {}

void StatusNotifyApiImpl::receive_pdu_session_status_notification(
    const std::string& ueContextId, const std::string& pduSessionId,
    const SmContextStatusNotification& statusNotification,
    Pistache::Http::ResponseWriter& response) {
  Logger::amf_server().debug(
      "Receive PDU Session Release notification, handling...");

  uint8_t pdu_session_id = 0;
  if (conv::string_to_int8(pduSessionId, pdu_session_id)) {
    // TODO:
    Logger::amf_server().debug("Invalid PDU Session ID value");
    response.send(Pistache::Http::Code::No_Content);
  }

  // Generate a promise and associate this promise to the ITTI message
  uint32_t promise_id = m_amf_app->generate_promise_id();
  Logger::amf_n1().debug("Promise ID generated %d", promise_id);

  boost::shared_ptr<boost::promise<nlohmann::json>> p =
      boost::make_shared<boost::promise<nlohmann::json>>();
  boost::shared_future<nlohmann::json> f = p->get_future();
  m_amf_app->add_promise(promise_id, p);

  // Handle the PDU Session Release in amf_app
  std::shared_ptr<itti_sbi_pdu_session_release_notif> itti_msg =
      std::make_shared<itti_sbi_pdu_session_release_notif>(
          TASK_AMF_SBI, TASK_AMF_APP, promise_id);

  itti_msg->http_version                = 1;
  itti_msg->promise_id                  = promise_id;
  itti_msg->ue_id                       = ueContextId;
  itti_msg->pdu_session_id              = pdu_session_id;
  itti_msg->smContextStatusNotification = statusNotification;

  int ret = itti_inst->send_msg(itti_msg);
  if (0 != ret) {
    Logger::amf_server().error(
        "Could not send ITTI message %s to task TASK_AMF_APP",
        itti_msg->get_msg_name());
  }

  boost::future_status status;
  // wait for timeout or ready
  status = f.wait_for(boost::chrono::milliseconds(FUTURE_STATUS_TIMEOUT_MS));
  if (status == boost::future_status::ready) {
    assert(f.is_ready());
    assert(f.has_value());
    assert(!f.has_exception());
    // Wait for the result from APP
    // result includes json content and http response code
    nlohmann::json result = f.get();
    Logger::amf_server().debug("Got result for promise ID %d", promise_id);

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

}  // namespace api
}  // namespace amf
}  // namespace oai
