/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2017-2023, 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/ca-request-state.hpp"

#include <ndn-cxx/util/indented-stream.hpp>

#include <boost/property_tree/json_parser.hpp>

namespace ndncert {

std::string statusToString(Status status)
{
  switch (status) {
  case Status::BEFORE_CHALLENGE:
    return "Before challenge";
  case Status::CHALLENGE:
    return "In challenge";
  case Status::PENDING:
    return "Pending after challenge";
  case Status::SUCCESS:
    return "Success";
  case Status::FAILURE:
    return "Failure";
  default:
    return "Unrecognized status";
  }
}

Status
statusFromBlock(const Block& block)
{
  auto status_int = readNonNegativeInteger(block);
  if (status_int > 6)
      NDN_THROW(std::runtime_error("Unrecognized Status"));
  return static_cast<Status>(status_int);
}

namespace ca {

ChallengeState::ChallengeState(const std::string& challengeStatus,
                               const time::system_clock::time_point& challengeTp,
                               size_t remainingTries, time::seconds remainingTime,
                               JsonSection&& challengeSecrets)
  : challengeStatus(challengeStatus)
  , timestamp(challengeTp)
  , remainingTries(remainingTries)
  , remainingTime(remainingTime)
  , secrets(std::move(challengeSecrets))
{
}

std::ostream&
operator<<(std::ostream& os, const RequestState& request)
{
  os << "Request's CA name: " << request.caPrefix << "\n";
  os << "Request's request ID: " << ndn::toHex(request.requestId) << "\n";
  os << "Request's status: " << statusToString(request.status) << "\n";
  os << "Request's challenge type: " << request.challengeType << "\n";
  if (request.challengeState) {
    os << "Challenge Status: " << request.challengeState->challengeStatus << "\n";
    os << "Challenge remaining tries:" << request.challengeState->remainingTries << " times\n";
    os << "Challenge remaining time: " << request.challengeState->remainingTime.count() << " seconds\n";
    os << "Challenge last update: " << time::toIsoString(request.challengeState->timestamp) << "\n";
    std::stringstream ss;
    boost::property_tree::write_json(ss, request.challengeState->secrets);
    os << "Challenge secret: " << ss.str() << "\n";
  }
  os << "Certificate:\n";
  ndn::util::IndentedStream os2(os, "  ");
  os2 << request.cert;
  return os;
}

} // namespace ca
} // namespace ndncert
