/* -*- 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::errortlv {

Block
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>
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 {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 time each."));
    }
    if (otherCriticalCount > 0) {
      NDN_THROW(std::runtime_error("Unknown Critical TLV type in error packet"));
    }
    return {error, errorInfo};
  }
  catch (const std::exception& e) {
    NDN_LOG_ERROR("Exception in error message decoding: " << e.what());
    return {ErrorCode::NO_ERROR, ""};
  }
}

} // namespace ndncert::errortlv
