/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2017-2022, Regents of the University of California.
 *
 * This file is part of ndncert, a certificate management system based on NDN.
 *
 * ndncert 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.
 *
 * ndncert 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 copies of the GNU General Public License along with
 * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 *
 * See AUTHORS.md for complete list of ndncert authors and contributors.
 */

#include "detail/error-encoder.hpp"

NDN_LOG_INIT(ndncert.encode.error);

namespace ndncert {

Block
errortlv::encodeDataContent(ErrorCode errorCode, const std::string& description)
{
  Block response(ndn::tlv::Content);
  response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::ErrorCode, static_cast<size_t>(errorCode)));
  response.push_back(ndn::makeStringBlock(tlv::ErrorInfo, description));
  response.encode();
  return response;
}

std::tuple<ErrorCode, std::string>
errortlv::decodefromDataContent(const Block& block)
{
  try {
    block.parse();
    int codeCount = 0;
    int infoCount = 0;
    int otherCriticalCount = 0;
    ErrorCode error;
    std::string errorInfo;
    for (const auto& item : block.elements()) {
      if (item.type() == tlv::ErrorCode) {
        error = static_cast<ErrorCode>(readNonNegativeInteger(block.get(tlv::ErrorCode)));
        codeCount ++;
      }
      else if (item.type() == tlv::ErrorInfo) {
        errorInfo = readString(block.get(tlv::ErrorInfo));
        infoCount ++;
      }
      else if (ndn::tlv::isCriticalType(item.type())) {
        otherCriticalCount ++;
      }
      else {
        //ignore
      }
    }
    if (codeCount == 0 && infoCount == 0) {
      return std::make_tuple(ErrorCode::NO_ERROR, "");
    }
    if (codeCount != 1 || infoCount != 1) {
      NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(codeCount) + " error code(s) and " +
                                      std::to_string(infoCount) + "error info(s), instead of expected 1 times each."));
    }
    if (otherCriticalCount > 0) {
      NDN_THROW(std::runtime_error("Unknown Critical TLV type in error packet"));
    }
    return std::make_tuple(error, errorInfo);
  } catch (const std::exception& e) {
    NDN_LOG_ERROR("[errortlv::DecodeFromDataContent] Exception in error message decoding: " << e.what());
    return std::make_tuple(ErrorCode::NO_ERROR, "");
  }
}

} // namespace ndncert
