/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
 * Copyright (c) 2014,  Regents of the University of California
 *
 * This file is part of NSL (NDN Signature Logger).
 * See AUTHORS.md for complete list of NSL authors and contributors.
 *
 * NSL is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * NSL, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 *
 * See AUTHORS.md for complete list of nsl authors and contributors.
 */
#include "node.hpp"

#include <ndn-cxx/util/digest.hpp>
#include <boost/lexical_cast.hpp>

namespace nsl {

ndn::ConstBufferPtr Node::EMPTY_HASH;

Node::Index::Index(const NonNegativeInteger& nodeSeq, size_t nodeLevel)
  : seqNo(nodeSeq)
  , level(nodeLevel)
  , range(1 << nodeLevel)
{
  if (seqNo % range != 0)
    throw Error("Index: index level and seqNo do not match: (" +
                boost::lexical_cast<std::string>(seqNo) + ", " +
                boost::lexical_cast<std::string>(level) + ")");
}

bool
Node::Index::operator<(const Index& other) const
{
  if (seqNo < other.seqNo) {
    return true;
  }
  else if (seqNo == other.seqNo) {
    return level < other.level;
  }
  else {
    return false;
  }
}

bool
Node::Index::operator==(const Index& other) const
{
  return equals(other);
}

bool
Node::Index::operator!=(const Index& other) const
{
  return !equals(other);
}

bool
Node::Index::equals(const Index& other) const
{
  if (seqNo == other.seqNo && level == other.level) {
    return true;
  }
  else {
    return false;
  }
}

Node::Node(const NonNegativeInteger& nodeSeqNo,
           size_t nodeLevel,
           const NonNegativeInteger& leafSeqNo,
           ndn::ConstBufferPtr hash)
  : m_index(nodeSeqNo, nodeLevel)
  , m_hash(hash)
{
  if (leafSeqNo == 0 && m_index.seqNo > leafSeqNo)
    m_leafSeqNo = m_index.seqNo;
  else
    setLeafSeqNo(leafSeqNo);
}

void
Node::setLeafSeqNo(const NonNegativeInteger& leafSeqNo)
{
  if (leafSeqNo > m_index.seqNo + m_index.range || leafSeqNo < m_index.seqNo)
    throw Error("Node: leaf seqNo is out of range");

  m_leafSeqNo = leafSeqNo;
}

void
Node::setHash(ndn::ConstBufferPtr hash)
{
  m_hash = hash;
}

bool
Node::isFull() const
{
  return m_index.seqNo + m_index.range == m_leafSeqNo;
}

ndn::ConstBufferPtr
Node::getEmptyHash()
{
  if (EMPTY_HASH == nullptr) {
    ndn::util::Sha256 sha256;
    EMPTY_HASH = sha256.computeDigest();
  }

  return EMPTY_HASH;
}

} // namespace nsl
