/*
  responder.cpp

  Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/
#include "responder.h"

#include <algorithm>
#include <limits>

namespace RDM {

/**
 * @brief Responder::Responder
 * @param id
 * @param parent
 */
Responder::Responder(UID id, Device* parent)
  : Device(id, parent)
  , control_field(0)
{
  deviceModelID = 1;
  deviceModelDescription = "Basic RDM Responder";
  subdevice_flag = true;

  /// 7.5 Discovery Unique Branch Message (DISC_UNIQUE_BRANCH)
  parameters_.try_emplace(DISC_UNIQUE_BRANCH, new Parameter());
  parameters_.at(DISC_UNIQUE_BRANCH)->discAction(std::bind(
                                          &Responder::actionDiscoverUniqueBranch,
                                          this, std::placeholders::_1,
                                                std::placeholders::_2));
  /// 7.6.3 Discovery Mute Message (DISC_MUTE)
  /// A responder port shall set its Mute flag when it receives this message
  /// containing its UID, or a broadcast address.
  parameters_.try_emplace(DISC_MUTE, new Parameter());
  parameters_.at(DISC_MUTE)->discAction(std::bind(
                                          &Responder::actionDiscoveryMute,
                                          this, std::placeholders::_1,
                                                std::placeholders::_2));
  /// 7.6.4 Discovery Un-Mute Message (DISC_UN_MUTE)
  /// A responder port shall clear its Mute flag when it receives this message
  /// containing its UID, or a broadcast address.
  parameters_.try_emplace(DISC_UN_MUTE, new Parameter());
  parameters_.at(DISC_UN_MUTE)->discAction(std::bind(
                                          &Responder::actionDiscoveryUnmute,
                                          this, std::placeholders::_1,
                                                std::placeholders::_2));
  /// 10.2.1 Communication Status (COMMS_STATUS)
  /// The COMMS_STATUS parameter is used to collect information that may be
  /// useful in analyzing the integrity of the communication system.
  parameters_.try_emplace(COMMS_STATUS, new Parameter());
  parameters_.at(COMMS_STATUS)->getAction(std::bind(
                                          &Responder::actionGetCommsStatus,
                                          this, std::placeholders::_1,
                                                std::placeholders::_2));
  parameters_.at(COMMS_STATUS)->setAction(std::bind(
                                          &Responder::actionSetCommsStatus,
                                          this, std::placeholders::_1,
                                                std::placeholders::_2));
  /// 10.3.1 Get Queued Message (QUEUED_MESSAGE)
  /// The QUEUED_MESSAGE parameter shall be used to retrieve a message from the
  /// responder’s message queue. The Message Count field of all response
  /// messages defines the number of messages that are queued in the responder.
  /// Each QUEUED_MESSAGE response shall be composed of a single message response.
  parameters_.try_emplace(QUEUED_MESSAGE, new Parameter());
  parameters_.at(QUEUED_MESSAGE)->getAction(std::bind(
                                          &Responder::actionGetQueuedMessage,
                                          this, std::placeholders::_1,
                                                std::placeholders::_2));
  /// 10.3.2 Get Status Messages (STATUS_MESSAGES)
  /// This parameter is used to collect Status or Error information
  /// from a device.
  parameters_.try_emplace(STATUS_MESSAGES, new Parameter());
  parameters_.at(STATUS_MESSAGES)->getAction(std::bind(
                                          &Responder::actionGetStatusMessages,
                                          this, std::placeholders::_1,
                                                std::placeholders::_2));
  /// 10.3.3 Get Status ID Description (STATUS_ID_DESCRIPTION)
  /// This parameter is used to request an ASCII text description of a given
  /// Status ID. The description may be up to 32 characters.
  parameters_.try_emplace(STATUS_ID_DESCRIPTION, new Parameter());
  parameters_.at(STATUS_ID_DESCRIPTION)->getAction(std::bind(
                                          &Responder::actionGetStatusIdDescription,
                                          this, std::placeholders::_1,
                                                std::placeholders::_2));
}


/**
 * @brief Responder::~Responder
 */
Responder::~Responder()
{
}


/**
 * @brief Responder::send
 * @param data
 */
void Responder::send(const std::vector<uint8_t> &data)
{
  (void)data;
}


/**
 * @brief Responder::send
 * @param response
 */
void Responder::send(MsgPtr response)
{
  if (!response)
    return;

  if (response->do_not_send)
      return;

  // 6.2.8.2 Message Count field for Responder Generated Messages
  // If a responder has more than 255 messages queued, then the Message Count
  // field shall remain at 255 until the number of queued messages is reduced
  // below that number.
  response->messageCount = std::min(queued_messages_.size(),
                                   (size_t)std::numeric_limits<uint8_t>::max());
  std::vector<uint8_t> data;
  response->write(data);
  send(data);
}


/**
 * @brief Responder::receive
 * @param data
 */
void Responder::receive(const std::vector<uint8_t> &data)
{
  auto message = MsgPtr(new Message(data));
  receive(message);
}


/**
 * @brief Responder::receive
 * @param message
 */
void Responder::receive(const MsgPtr message)
{
  if (message->short_message)
    {
      if (short_message_counter_ != std::numeric_limits<uint16_t>::max())
        short_message_counter_++;
      return;
    }

  // RDM::UID::operator== also returns true for broadcast messages
  if (message->destination != id_ ||
      message->incorrect_sc ||
      message->incorrect_sub_sc)
    return;

  if (message->length_mismatch)
    {
      if (length_mismatch_counter_ != std::numeric_limits<uint16_t>::max())
        length_mismatch_counter_++;
      return;
    }

  if (message->checksum_fail)
    {
      if (checksum_fail_counter_ != std::numeric_limits<uint16_t>::max())
        checksum_fail_counter_++;
      return;
    }

  // all other uncaught errors
  if (message->failure_mode != 0)
    return;

  // responder can ignore _COMMAND_RESPONSE class messages
  if (message->commandClass == DISCOVERY_COMMAND_RESPONSE ||
      message->commandClass == GET_COMMAND_RESPONSE ||
      message->commandClass == SET_COMMAND_RESPONSE)
    return;

  auto response = MsgPtr(new Message());
  response->source = id_;
  response->destination = message->source;
  response->subDevice = message->subDevice;
  response->parameterId = message->parameterId;
  response->transaction = message->transaction;

  /// 5.3 Broadcast Message Addressing
  /// When Broadcast Addressing is used for non-Discovery messages, the
  /// responders shall not send a response.
  if (message->destination.isBroadcast() &&
      message->commandClass != DISCOVERY_COMMAND)
    response->do_not_send = true;

  switch (message->commandClass) {
    case DISCOVERY_COMMAND:
      response->commandClass = DISCOVERY_COMMAND_RESPONSE;
      rxDiscovery(message, response);
      break;
    case GET_COMMAND:
      response->commandClass = GET_COMMAND_RESPONSE;
      rxGet(message, response);
      break;
    case SET_COMMAND:
      response->commandClass = SET_COMMAND_RESPONSE;
      rxSet(message, response);
      break;
    default:
      return;
  }

  if (!response)
    return;

  if (response->do_not_send)
    return;

  send(response);
}


/**
 * @brief Responder::reset
 * @param hard
 */
void Responder::reset(bool hard)
{
  (void)hard;

  /// 10.11.2 Reset Device (RESET_DEVICE)
  /// This parameter shall also clear the Discovery Mute flag.
  discovery_mute_flag_ = false;
}


/**
 * @brief Responder::rxDiscovery
 * @param message
 * @param response
 */
void Responder::rxDiscovery(const MsgPtr message, MsgPtr response)
{
  if (message->subDevice != 0)
    {
      response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
      return;
    }

  if (!actionPrep_(message, response))
    return;

  parameters_.at(message->parameterId)->disc(message, response);
}


/**
 * @brief Responder::rxGet
 * @param message
 * @param response
 */
void Responder::rxGet(const MsgPtr message, MsgPtr response)
{
  // 9.2.2 Using Sub-Devices
  // Broadcast GET commands sent to the SUB_DEVICE_ALL_CALL Sub-Device ID are
  // not allowed. Any responder receiving a GET command sent to this Sub-Device
  // ID shall respond with a NACK with a NACK Reason Code of
  // NR_SUB_DEVICE_OUT_OF_RANGE.
  if (message->subDevice == SUB_DEVICE_ALL_CALL)
    {
      response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
      return;
    }

  if (message->subDevice == 0)
    {
      get(message, response);
      return;
    }

  if (!sub_devices_.count(message->subDevice))
    {
      response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
      return;
    }

  sub_devices_.at(message->subDevice)->get(message, response);
}


/**
 * @brief Responder::rxSet
 * @param message
 * @param response
 */
void Responder::rxSet(const MsgPtr message, MsgPtr response)
{
  if (message->subDevice == 0)
    {
      set(message, response);
      return;
    }

  if (sub_devices_.empty())
    {
      response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
      return;
    }

  if (message->subDevice == SUB_DEVICE_ALL_CALL)
    {
      for (auto& [num, dev] : sub_devices_)
          dev->set(message, response);
      return;
    }

  if (!sub_devices_.count(message->subDevice))
    {
      response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
      return;
    }

  sub_devices_.at(message->subDevice)->set(message, response);
}


/**
 * @brief Responder::actionDiscoverUniqueBranch
 * @param message
 * @param response
 */
void Responder::actionDiscoverUniqueBranch(const MsgPtr message, MsgPtr response)
{
  if (!message->requiredLength(12, response))
    return;

  if (discovery_mute_flag_)
    {
      response->do_not_send = true;
      return;
    }

  UID lower, upper;
  lower.manufacturer = Message::readType<uint16_t>(*message->data(), 0);
  lower.device       = Message::readType<uint32_t>(*message->data(), 2);
  upper.manufacturer = Message::readType<uint16_t>(*message->data(), 6);
  upper.device       = Message::readType<uint32_t>(*message->data(), 8);

  if (id_.uid < lower.uid)
    {
      response->do_not_send = true;
      return;
    }
  if (id_.uid > upper.uid)
    {
      response->do_not_send = true;
      return;
    }

  response->appendData(id_.manufacturer);
  response->appendData(id_.device);
}


/**
 * @brief Responder::actionDiscoveryMute
 * @param message
 * @param response
 */
void Responder::actionDiscoveryMute(const MsgPtr message, MsgPtr response)
{
  if (!message->requiredLength(0, response))
    return;

  discovery_mute_flag_ = true;
  controller_uid_ = message->source;

  response->responseType = RESPONSE_TYPE_ACK;
  response->appendData(control_field);
}


/**
 * @brief Responder::actionDiscoveryUnmute
 * @param message
 * @param response
 */
void Responder::actionDiscoveryUnmute(const MsgPtr message, MsgPtr response)
{
  if (!message->requiredLength(0, response))
    return;

  discovery_mute_flag_ = false;

  response->responseType = RESPONSE_TYPE_ACK;
  response->appendData(control_field);
}


/**
 * @brief Responder::actionGetCommsStatus
 * @param message
 * @param response
 */
void Responder::actionGetCommsStatus(const MsgPtr message, MsgPtr response)
{
  if (!message->requiredLength(0, response))
    return;

  response->responseType = RESPONSE_TYPE_ACK;
  response->appendData(short_message_counter_);
  response->appendData(length_mismatch_counter_);
  response->appendData(checksum_fail_counter_);
}


/**
 * @brief Responder::actionSetCommsStatus
 * @param message
 * @param response
 */
void Responder::actionSetCommsStatus(const MsgPtr message, MsgPtr response)
{
  if (!message->requiredLength(0, response))
    return;

  short_message_counter_ = 0;
  length_mismatch_counter_ = 0;
  checksum_fail_counter_ = 0;

  response->responseType = RESPONSE_TYPE_ACK;
}


/**
 * @brief Responder::actionGetQueuedMessage
 * @param message
 * @param response
 */
void Responder::actionGetQueuedMessage(const MsgPtr message, MsgPtr response)
{
  if (!message->requiredLength(1, response))
    return;

  if (queued_messages_.empty())
    {
      response->parameterId = STATUS_MESSAGES;
      actionGetStatusMessages(message, response);
      return;
    }

  response->do_not_send = true;
  auto msg = queued_messages_.front();
  queued_messages_.pop_front();
  last_status_message_ = msg;
  send(msg);
}


/**
 * @brief Responder::actionGetStatusMessages
 * @param message
 * @param response
 */
void Responder::actionGetStatusMessages(const MsgPtr message, MsgPtr response)
{
  if (!message->requiredLength(1, response))
    return;

  uint8_t type = message->data()->front();

  if (type != STATUS_GET_LAST_MESSAGE &&
      type != STATUS_ERROR &&
      type != STATUS_WARNING &&
      type != STATUS_ADVISORY)
    {
      response->nak(NR_DATA_OUT_OF_RANGE);
      return;
    }

  if (type == STATUS_GET_LAST_MESSAGE)
    {
      response->do_not_send = true;
      send(last_status_message_);
      return;
    }

  int counter = 0;
  auto reportStatusQueue = [response, counter] (std::queue<StatusPtr> q) mutable
  {
    while(!q.empty() && counter < 25)
      {
        for (uint8_t& b : q.front()->bytes)
          response->appendData(b);
        counter++;
        q.pop();
      }
  };

  if (type == STATUS_ERROR ||
      type == STATUS_WARNING ||
      type == STATUS_ADVISORY)
    {
      reportStatusQueue(queued_statuses_.at(STATUS_ERROR));
      for (auto& [_, dev] : sub_devices_)
        reportStatusQueue(dev->queued_statuses_.at(STATUS_ERROR));
    }

  if (type == STATUS_WARNING ||
      type == STATUS_ADVISORY)
    {
      reportStatusQueue(queued_statuses_.at(STATUS_WARNING));
      for (auto& [_, dev] : sub_devices_)
        reportStatusQueue(dev->queued_statuses_.at(STATUS_WARNING));
    }

  if (type == STATUS_ADVISORY)
    {
      reportStatusQueue(queued_statuses_.at(STATUS_ADVISORY));
      for (auto& [_, dev] : sub_devices_)
        reportStatusQueue(dev->queued_statuses_.at(STATUS_ADVISORY));
    }

  if (counter == 25)
    response->responseType = RESPONSE_TYPE_ACK_OVERFLOW;
  else
    response->responseType = RESPONSE_TYPE_ACK;

  last_status_message_ = response;
}


/**
 * @brief Responder::actionGetStatusIdDescription
 * @param message
 * @param response
 */
void Responder::actionGetStatusIdDescription(const MsgPtr message, MsgPtr response)
{
  if (!message->requiredLength(2, response))
    return;

  uint16_t status = message->readType<uint16_t>(*message->data(), 0);

  response->responseType = RESPONSE_TYPE_ACK;
  std::string label = RDM::StatusMessageDescription(status);
  for (size_t i = 0; i < label.size(); i++)
    {
      if (i > 32)
        break;
      response->appendData(label.at(i));
    }
}

} // namespace RDM
