/*
  device.h

  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.
*/
#pragma once

#include "dmx/device.h"
#include "parameter.h"
#include "rdm.h"
#include "sensor.h"
#include "status.h"
#include "uid.h"

#include <list>
#include <queue>
#include <string>
#include <unordered_map>

namespace RDM {

/**
 * @brief The RDM::Device class
 */
class Device
    : public DMX::Device
{
public:
  Device(UID id = UID(), Device* parent = nullptr);
  virtual ~Device();

  void addSubDevice(uint16_t number, Device* dev);
  Device* subDevice(uint16_t number);
  uint16_t subDeviceCount() const;
  void addProductDetailId(uint16_t);

  void get(const MsgPtr message, MsgPtr response);
  void set(const MsgPtr message, MsgPtr response);

  virtual void identify(bool state);
  virtual void reset(bool hard);

  uint16_t deviceModelID;                 //!< model ID number
  std::string deviceModelDescription;     //!< model description
  uint16_t deviceProductCategory;         //!< device category

protected:
  friend class Responder;
  UID id_;                                            //!< device UID
  std::unordered_map<uint16_t, Device*> sub_devices_; //!< sub devices
  std::unordered_map<PID, Parameter*> parameters_;    //!< parameters
  std::vector<Sensor*> sensors_;                      //!< sensors
  std::list<uint16_t> product_detail_list_;           //!< product detail list

  std::deque<MsgPtr> queued_messages_;                //!< outbound message queue
  void enqueueMessage(MsgPtr message, bool urgent = false);
  UID  controller_uid_;   //!< controller UID

  std::unordered_map<uint8_t, std::queue<StatusPtr>> queued_statuses_;  //!< outbound status queue
  void enqueueStatus(StatusPtr status);

  bool actionPrep_(const MsgPtr message, MsgPtr response);

  void actionGetSupportedParameters (const MsgPtr message, MsgPtr response);
  void actionSetClearStatusId       (const MsgPtr message, MsgPtr response);
  void actionGetSubdeviceThreshold  (const MsgPtr message, MsgPtr response);
  void actionSetSubdeviceThreshold  (const MsgPtr message, MsgPtr response);
  void actionGetDeviceInfo          (const MsgPtr message, MsgPtr response);
  void actionGetProductDetailIdList (const MsgPtr message, MsgPtr response);
  void actionGetDevModelDescription (const MsgPtr message, MsgPtr response);
  void actionGetManufacturerLabel   (const MsgPtr message, MsgPtr response);
  void actionGetLanguage            (const MsgPtr message, MsgPtr response);
  void actionSetLanguage            (const MsgPtr message, MsgPtr response);
  void actionGetSoftwareVersionLabel(const MsgPtr message, MsgPtr response);
  void actionGetDmxPersonality      (const MsgPtr message, MsgPtr response);
  void actionSetDmxPersonality      (const MsgPtr message, MsgPtr response);
  void actionGetDmxPersonalityDesc  (const MsgPtr message, MsgPtr response);
  void actionGetDmxStartAddress     (const MsgPtr message, MsgPtr response);
  void actionSetDmxStartAddress     (const MsgPtr message, MsgPtr response);
  void actionSensorDispatch         (const MsgPtr message, MsgPtr response);
  void actionGetIdentifyDevice      (const MsgPtr message, MsgPtr response);
  void actionSetIdentifyDevice      (const MsgPtr message, MsgPtr response);
  void actionSetResetDevice         (const MsgPtr message, MsgPtr response);

private:
  Device* parent_;
  PID last_rx_pid_ = 0;
  uint ack_overflow_page = 0;
  uint8_t status_reporting_threshold_;
  bool identifying_ = false;

};

} // namespace RDM

