/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2020, Regents of the University of California
 *
 * BSD license, See the LICENSE file for more information
 *
 * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu>
 */

#include "conf.hpp"

namespace chronochat {

BOOST_CONCEPT_ASSERT((ndn::WireEncodable<Conf>));
BOOST_CONCEPT_ASSERT((ndn::WireDecodable<Conf>));

Conf::Conf()
{
}

Conf::Conf(const Block& confWire)
{
  this->wireDecode(confWire);
}

template<ndn::encoding::Tag T>
size_t
Conf::wireEncode(ndn::EncodingImpl<T>& block) const
{
  size_t totalLength = 0;

  // Conf := CONF-TYPE TLV-LENGTH
  //           Name
  //           Nick
  //
  // Nick := NICK-TYPE TLV-LENGTH
  //            String

  // Nick
  const uint8_t* nickWire = reinterpret_cast<const uint8_t*>(m_nick.c_str());
  totalLength += block.prependByteArrayBlock(tlv::Nick, nickWire, m_nick.length());

  // Identity
  totalLength += m_identity.wireEncode(block);

  // Conf
  totalLength += block.prependVarNumber(totalLength);
  totalLength += block.prependVarNumber(tlv::Conf);

  return totalLength;
}

const Block&
Conf::wireEncode() const
{
  ndn::EncodingEstimator estimator;
  size_t estimatedSize = wireEncode(estimator);

  ndn::EncodingBuffer buffer(estimatedSize, 0);
  wireEncode(buffer);

  m_wire = buffer.block();
  m_wire.parse();

  return m_wire;
}

void
Conf::wireDecode(const Block& confWire)
{
  m_wire = confWire;
  m_wire.parse();

  // Conf := CONF-TYPE TLV-LENGTH
  //           Name
  //           Nick
  //
  // Nick := NICK-TYPE TLV-LENGTH
  //            String

  if (m_wire.type() != tlv::Conf)
    NDN_THROW(Error("Unexpected TLV number when decoding conf packet"));

  // Identity
  Block::element_const_iterator i = m_wire.elements_begin();
  if (i == m_wire.elements_end())
    NDN_THROW(Error("Missing Identity"));
  if (i->type() != tlv::Name)
    NDN_THROW(Error("Expect Identity but get TLV Type " + std::to_string(i->type())));

  m_identity.wireDecode(*i);
  ++i;

  // Nick
  if (i == m_wire.elements_end())
    return;
  if (i->type() != tlv::Nick)
    NDN_THROW(Error("Expect Nick but get TLV Type " + std::to_string(i->type())));

  m_nick = std::string(reinterpret_cast<const char* >(i->value()),
                       i->value_size());
  ++i;

  if (i != m_wire.elements_end())
    NDN_THROW(Error("Unexpected element"));
}

void
Conf::setIdentity(const Name& identity)
{
  m_wire.reset();
  m_identity = identity;
}

void
Conf::setNick(const std::string& nick)
{
  m_wire.reset();
  m_nick = nick;
}

} // namespace chronochat
