#include "config.h"

#include <torrent/data/file.h>
#include <torrent/data/file_list.h>
#include <torrent/data/file_list_iterator.h>

#include "core/manager.h"

#include "globals.h"
#include "control.h"
#include "command_helpers.h"

void
apply_f_set_priority(torrent::File* file, uint32_t value) {
  if (value > torrent::PRIORITY_HIGH)
    throw torrent::input_error("Invalid value.");

  file->set_priority(static_cast<torrent::priority_enum>(value));
}

// TODO: Redundant.
torrent::Object
apply_f_path(torrent::File* file) {
  if (file->path()->empty())
    return std::string();

  torrent::Object result_raw(file->path()->begin()->str());
  auto&           result = result_raw.as_string();

  for (torrent::Path::const_iterator itr = ++file->path()->begin(), last = file->path()->end(); itr != last; itr++)
    result += '/' + itr->str();

  return result_raw;
}

torrent::Object
apply_f_path_components(torrent::File* file, int output_type) {
  auto  result_raw = torrent::Object::create_list();
  auto& result = result_raw.as_list();

  for (const auto& itr : *file->path()) {
    switch (output_type) {
    case 0: result.push_back(itr.str());                      break;
    case 1: result.push_back(itr.object_hex());               break;
    case 2: result.push_back(itr.object_base64());            break;
    case 3: result.push_back(itr.object_base64_as_binary());  break;
    case 4: result.push_back(itr.object_as_binary());         break;
    case 5: result.push_back(itr.object_utf8_or_base64());    break;
    case 6: result.push_back(itr.object_utf8_or_as_binary()); break;
    default: throw torrent::input_error("apply_f_path_components(): invalid output type");
    };
  }

  return result_raw;
}

torrent::Object
apply_f_path_depth(torrent::File* file) {
  return (int64_t)file->path()->size();
}

torrent::Object
apply_fi_filename_last(torrent::FileListIterator* itr) {
  if (itr->file()->path()->empty())
    return "EMPTY";

  if (itr->depth() >= itr->file()->path()->size())
    return "ERROR";

  return itr->file()->path()->at(itr->depth()).str();
}

void
initialize_command_file() {
  CMD2_FILE("f.is_created",             std::bind(&torrent::File::is_created, std::placeholders::_1));
  CMD2_FILE("f.is_open",                std::bind(&torrent::File::is_open, std::placeholders::_1));

  CMD2_FILE("f.is_create_queued",       std::bind(&torrent::File::is_create_queued, std::placeholders::_1));
  CMD2_FILE("f.is_resize_queued",       std::bind(&torrent::File::is_resize_queued, std::placeholders::_1));

  CMD2_FILE_VALUE_V("f.set_create_queued",   std::bind(&torrent::File::set_flags,   std::placeholders::_1, torrent::File::flag_create_queued));
  CMD2_FILE_VALUE_V("f.set_resize_queued",   std::bind(&torrent::File::set_flags,   std::placeholders::_1, torrent::File::flag_resize_queued));
  CMD2_FILE_VALUE_V("f.unset_create_queued", std::bind(&torrent::File::unset_flags, std::placeholders::_1, torrent::File::flag_create_queued));
  CMD2_FILE_VALUE_V("f.unset_resize_queued", std::bind(&torrent::File::unset_flags, std::placeholders::_1, torrent::File::flag_resize_queued));

  CMD2_FILE  ("f.prioritize_first",         std::bind(&torrent::File::has_flags,   std::placeholders::_1, torrent::File::flag_prioritize_first));
  CMD2_FILE_V("f.prioritize_first.enable",  std::bind(&torrent::File::set_flags,   std::placeholders::_1, torrent::File::flag_prioritize_first));
  CMD2_FILE_V("f.prioritize_first.disable", std::bind(&torrent::File::unset_flags, std::placeholders::_1, torrent::File::flag_prioritize_first));
  CMD2_FILE  ("f.prioritize_last",          std::bind(&torrent::File::has_flags,   std::placeholders::_1, torrent::File::flag_prioritize_last));
  CMD2_FILE_V("f.prioritize_last.enable",   std::bind(&torrent::File::set_flags,   std::placeholders::_1, torrent::File::flag_prioritize_last));
  CMD2_FILE_V("f.prioritize_last.disable",  std::bind(&torrent::File::unset_flags, std::placeholders::_1, torrent::File::flag_prioritize_last));

  CMD2_FILE("f.size_bytes",                       [](auto* file, auto)   { return file->size_bytes(); });
  CMD2_FILE("f.size_chunks",                      [](auto* file, auto)   { return file->size_chunks(); });
  CMD2_FILE("f.completed_chunks",                 [](auto* file, auto)   { return file->completed_chunks(); });

  CMD2_FILE("f.offset",                           [](auto* file, auto)   { return file->offset(); });
  CMD2_FILE("f.range_first",                      [](auto* file, auto)   { return file->range_first(); });
  CMD2_FILE("f.range_second",                     [](auto* file, auto)   { return file->range_second(); });

  CMD2_FILE("f.priority",                         [](auto* file, auto)   { return file->priority(); });
  CMD2_FILE_VALUE_V("f.priority.set",             [](auto* file, auto v) { apply_f_set_priority(file, v); });

  CMD2_FILE("f.path",                             [](auto* file, auto)   { return apply_f_path(file); });
  CMD2_FILE("f.path_components",                  [](auto* file, auto)   { return apply_f_path_components(file, 0); });
  CMD2_FILE("f.path_components.hex",              [](auto* file, auto)   { return apply_f_path_components(file, 1); });
  CMD2_FILE("f.path_components.base64",           [](auto* file, auto)   { return apply_f_path_components(file, 2); });
  CMD2_FILE("f.path_components.base64_as_binary", [](auto* file, auto)   { return apply_f_path_components(file, 3); });
  CMD2_FILE("f.path_components.as_binary",        [](auto* file, auto)   { return apply_f_path_components(file, 4); });
  CMD2_FILE("f.path_components.or_base64",        [](auto* file, auto)   { return apply_f_path_components(file, 5); });
  CMD2_FILE("f.path_components.or_as_binary",     [](auto* file, auto)   { return apply_f_path_components(file, 6); });
  CMD2_FILE("f.path_depth",                       [](auto* file, auto)   { return apply_f_path_depth(file); });
  CMD2_FILE("f.frozen_path",                      [](auto* file, auto)   { return file->frozen_path().str(); });
  CMD2_FILE("f.frozen_path.hex",                  [](auto* file, auto)   { return file->frozen_path().object_hex(); });
  CMD2_FILE("f.frozen_path.base64",               [](auto* file, auto)   { return file->frozen_path().object_base64(); });
  CMD2_FILE("f.frozen_path.as_binary",            [](auto* file, auto)   { return file->frozen_path().object_as_binary(); });
  CMD2_FILE("f.frozen_path.or_base64",            [](auto* file, auto)   { return file->frozen_path().object_utf8_or_base64(); });
  CMD2_FILE("f.frozen_path.or_as_binary",         [](auto* file, auto)   { return file->frozen_path().object_utf8_or_as_binary(); });
  CMD2_FILE("f.frozen_path.realpath.or_empty",    [](auto* file, auto)   { return resolve_path(file->frozen_path().str()); });
  CMD2_FILE("f.frozen_path.realpath.or_throw",    [](auto* file, auto)   { return resolve_path_or_throw(file->frozen_path().str()); });

  CMD2_FILE("f.match_depth_prev",       std::bind(&torrent::File::match_depth_prev, std::placeholders::_1));
  CMD2_FILE("f.match_depth_next",       std::bind(&torrent::File::match_depth_next, std::placeholders::_1));

  CMD2_FILE("f.last_touched",           std::bind(&torrent::File::last_touched, std::placeholders::_1));

  CMD2_FILEITR("fi.filename_last",      std::bind(&apply_fi_filename_last, std::placeholders::_1));
  CMD2_FILEITR("fi.is_file",            std::bind(&torrent::FileListIterator::is_file, std::placeholders::_1));

  rpc::rpc.mark_safe("f.path");
  rpc::rpc.mark_safe("f.path_components");
  rpc::rpc.mark_safe("f.path_depth");
  rpc::rpc.mark_safe("f.frozen_path");
  rpc::rpc.mark_safe("f.frozen_path.realpath.or_empty");
  rpc::rpc.mark_safe("f.frozen_path.realpath.or_throw");
  rpc::rpc.mark_safe("f.frozen_path.hex");
  rpc::rpc.mark_safe("f.frozen_path.base64");
  rpc::rpc.mark_safe("f.frozen_path.base64_as_binary");
  rpc::rpc.mark_safe("f.frozen_path.as_binary");
  rpc::rpc.mark_safe("f.frozen_path.or_base64");
  rpc::rpc.mark_safe("f.frozen_path.or_as_binary");
  rpc::rpc.mark_safe("f.offset");
  rpc::rpc.mark_safe("f.size_bytes");
  rpc::rpc.mark_safe("f.size_chunks");
  rpc::rpc.mark_safe("f.completed_chunks");
  rpc::rpc.mark_safe("f.range_first");
  rpc::rpc.mark_safe("f.range_second");
  rpc::rpc.mark_safe("f.priority");
  rpc::rpc.mark_safe("f.priority.set");
  rpc::rpc.mark_safe("f.is_created");
  rpc::rpc.mark_safe("f.is_open");
  rpc::rpc.mark_safe("f.is_create_queued");
  rpc::rpc.mark_safe("f.is_resize_queued");
  rpc::rpc.mark_safe("f.prioritize_first");
  rpc::rpc.mark_safe("f.prioritize_first.enable");
  rpc::rpc.mark_safe("f.prioritize_first.disable");
  rpc::rpc.mark_safe("f.prioritize_last");
  rpc::rpc.mark_safe("f.prioritize_last.enable");
  rpc::rpc.mark_safe("f.prioritize_last.disable");
  rpc::rpc.mark_safe("f.last_touched");
  rpc::rpc.mark_safe("f.match_depth_prev");
  rpc::rpc.mark_safe("f.match_depth_next");
  rpc::rpc.mark_safe("fi.filename_last");
  rpc::rpc.mark_safe("fi.is_file");
}
