/*
  receiver.h

  Copyright (c) 2020 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 "rlp/component.h"
#include "data.h"
#include "extended.h"
#include "sacn.h"
#include "mergeproxyuniverse.h"

#include <unordered_map>
#include <vector>

namespace SACN {

/**
 * @brief The Receiver class
 */
class Receiver
  : public virtual ACN::RLP::Component
{
public:
  Receiver(UUID::uuid = UUID::uuid());
  ~Receiver();

  virtual void subscribe(const uint16_t);
  virtual void unsubscribe(const uint16_t);
  Universe * universe(const uint16_t);

  void discoveryStart();
  void discoveryStop();
  void onDiscovered(const EXTENDED::DISCOVERY::Watcher);

protected:
  // 5 - E1.31 use of the ACN Root Layer Protocol
  void dataReceiver(ACN::PDU::Message<ACN::RLP::Pdu>);
  void extendedReceiver(ACN::PDU::Message<ACN::RLP::Pdu>);

  // 6.2 E1.31 Data Packet Framing Layer
  void dataFrameHandler(ACN::PDU::Message<DATA::Pdu>);

  // 6.3 E1.31 Synchronization Packet Framing Layer
  void syncFrameHandler(ACN::PDU::Message<EXTENDED::Pdu>);

  // 6.4 E1.31 Universe Discovery Packet Framing Layer
  void discoveryFrameHandler(ACN::PDU::Message<EXTENDED::Pdu>);
  // 8 Universe Discovery Layer
  void discoveryListHanlder(ACN::PDU::Message<EXTENDED::DISCOVERY::Pdu>);

private:
  std::unordered_map<uint16_t, MergeProxyUniverse*> universes_;
  std::vector<EXTENDED::DISCOVERY::Watcher> discoveryCallbacks_;
};

};
