/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
 * Copyright (c) 2014-2016,  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 "command-parser.hpp"
#include <ndn-cxx/util/logger.hpp>

namespace nfd {
namespace tools {
namespace nfdc {

NDN_LOG_INIT(CommandParser);

std::ostream&
operator<<(std::ostream& os, AvailableIn modes)
{
  int count = 0;

#define PRINT_BIT(bit, str) \
  if ((modes & bit) != 0) { \
    if (++count > 1) { \
      os << '|'; \
    } \
    os << str; \
  }

  PRINT_BIT(AVAILABLE_IN_ONE_SHOT, "one-shot")
  PRINT_BIT(AVAILABLE_IN_BATCH, "batch")

#undef PRINT_BIT

  if (count == 0) {
    os << "none";
  }
  return os;
}

std::ostream&
operator<<(std::ostream& os, ParseMode mode)
{
  switch (mode) {
    case ParseMode::ONE_SHOT:
      return os << "one-shot";
    case ParseMode::BATCH:
      return os << "batch";
  }
  return os << static_cast<int>(mode);
}

CommandParser&
CommandParser::addCommand(const CommandDefinition& def, const ExecuteCommand& execute, AvailableIn modes)
{
  BOOST_ASSERT(modes != AVAILABLE_IN_NONE);
  m_commands[{def.getNoun(), def.getVerb()}].reset(new Command{def, execute, modes});
  return *this;
}

CommandParser&
CommandParser::addAlias(const std::string& noun, const std::string& verb, const std::string& verb2)
{
  m_commands[{noun, verb2}] = m_commands.at({noun, verb});
  return *this;
}

std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
CommandParser::parse(const std::vector<std::string>& tokens, ParseMode mode) const
{
  BOOST_ASSERT(mode == ParseMode::ONE_SHOT);

  const std::string& noun = tokens.size() > 0 ? tokens[0] : "";
  const std::string& verb = tokens.size() > 1 ? tokens[1] : "";
  size_t nameLen = std::min<size_t>(2, tokens.size());

  auto i = m_commands.find({noun, verb});
  if (i == m_commands.end()) {
    if (verb.empty()) {
      i = m_commands.find({noun, "list"});
    }
    else {
      // help, exit, quit, legacy nfdc commands
      i = m_commands.find({noun, ""});
    }
    nameLen = std::min<size_t>(1, tokens.size());
  }
  if (i == m_commands.end() || (i->second->modes & static_cast<AvailableIn>(mode)) == 0) {
    BOOST_THROW_EXCEPTION(Error("no such command: " + noun + " " + verb));
  }

  const CommandDefinition& def = i->second->def;
  NDN_LOG_TRACE("found command " << def.getNoun() << " " << def.getVerb());

  return std::make_tuple(def.getNoun(), def.getVerb(), def.parse(tokens, nameLen), i->second->execute);
}

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