/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
 * Copyright (c) 2014-2017,  Regents of the University of California,
 *                           Arizona Board of Regents,
 *                           Colorado State University,
 *                           University Pierre & Marie Curie, Sorbonne University,
 *                           Washington University in St. Louis,
 *                           Beijing Institute of Technology,
 *                           The University of Memphis.
 *
 * This file is part of NFD (Named Data Networking Forwarding Daemon).
 * See AUTHORS.md for complete list of NFD authors and contributors.
 *
 * NFD 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.
 *
 * NFD 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
 * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "strategy-choice-module.hpp"
#include "format-helpers.hpp"

namespace nfd {
namespace tools {
namespace nfdc {

void
StrategyChoiceModule::registerCommands(CommandParser& parser)
{
  CommandDefinition defStrategyList("strategy", "list");
  defStrategyList
    .setTitle("print strategy choices");
  parser.addCommand(defStrategyList, &StrategyChoiceModule::list);

  CommandDefinition defStrategyShow("strategy", "show");
  defStrategyShow
    .setTitle("show strategy choice of an entry")
    .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES);
  parser.addCommand(defStrategyShow, &StrategyChoiceModule::show);
}

void
StrategyChoiceModule::list(ExecuteContext& ctx)
{
  ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
    [&] (const std::vector<StrategyChoice>& dataset) {
      for (const StrategyChoice& entry : dataset) {
        formatItemText(ctx.out, entry);
        ctx.out << '\n';
      }
    },
    ctx.makeDatasetFailureHandler("strategy choice dataset"),
    ctx.makeCommandOptions());

  ctx.face.processEvents();
}

void
StrategyChoiceModule::show(ExecuteContext& ctx)
{
  auto prefix = ctx.args.get<Name>("prefix");

  ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
    [&] (const std::vector<StrategyChoice>& dataset) {
      StrategyChoice match; // longest prefix match
      for (const StrategyChoice& entry : dataset) {
        if (entry.getName().isPrefixOf(prefix) &&
            entry.getName().size() >= match.getName().size()) {
          match = entry;
        }
      }
      formatItemText(ctx.out, match, true);
    },
    ctx.makeDatasetFailureHandler("strategy choice dataset"),
    ctx.makeCommandOptions());

  ctx.face.processEvents();
}

void
StrategyChoiceModule::fetchStatus(Controller& controller,
                                  const function<void()>& onSuccess,
                                  const Controller::DatasetFailCallback& onFailure,
                                  const CommandOptions& options)
{
  controller.fetch<ndn::nfd::StrategyChoiceDataset>(
    [this, onSuccess] (const std::vector<StrategyChoice>& result) {
      m_status = result;
      onSuccess();
    },
    onFailure, options);
}

void
StrategyChoiceModule::formatStatusXml(std::ostream& os) const
{
  os << "<strategyChoices>";
  for (const StrategyChoice& item : m_status) {
    this->formatItemXml(os, item);
  }
  os << "</strategyChoices>";
}

void
StrategyChoiceModule::formatItemXml(std::ostream& os, const StrategyChoice& item) const
{
  os << "<strategyChoice>";
  os << "<namespace>" << xml::Text{item.getName().toUri()} << "</namespace>";
  os << "<strategy><name>" << xml::Text{item.getStrategy().toUri()} << "</name></strategy>";
  os << "</strategyChoice>";
}

void
StrategyChoiceModule::formatStatusText(std::ostream& os) const
{
  os << "Strategy choices:\n";
  for (const StrategyChoice& item : m_status) {
    os << "  ";
    formatItemText(os, item);
    os << '\n';
  }
}

void
StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
{
  text::ItemAttributes ia(wantMultiLine, 8);
  os << ia("prefix") << item.getName()
     << ia("strategy") << item.getStrategy()
     << ia.end();
}

} // namespace nfdc
} // namespace tools
} // namespace nfd
