/* -*- 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 Execute& 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<CommandParser::Execute*, CommandArguments>
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(&i->second->execute, def.parse(tokens, nameLen));
}

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