/*
    This file is part of TON Blockchain Library.
    TON Blockchain Library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.
    TON Blockchain Library 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 Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License
    along with TON Blockchain Library.  If not, see .
*/
#pragma once
#include "td/actor/actor.h"
#include "adnl/adnl.h"
#include "rldp2/rldp.h"
#include "overlay/overlays.h"
#include "storage/PeerManager.h"
#include "storage/db.h"
#include "SpeedLimiter.h"
namespace ton {
class StorageManager : public td::actor::Actor {
 public:
  class Callback {
   public:
    virtual ~Callback() = default;
    virtual void on_ready() = 0;
  };
  StorageManager(adnl::AdnlNodeIdShort local_id, std::string db_root, td::unique_ptr callback,
                 bool client_mode, td::actor::ActorId adnl, td::actor::ActorId rldp,
                 td::actor::ActorId overlays);
  void start_up() override;
  void add_torrent(Torrent torrent, bool start_download, bool allow_upload, bool copy_inside,
                   td::Promise promise);
  void add_torrent_by_meta(TorrentMeta meta, std::string root_dir, bool start_download, bool allow_upload,
                           td::Promise promise);
  void add_torrent_by_hash(td::Bits256 hash, std::string root_dir, bool start_download, bool allow_upload,
                           td::Promise promise);
  void set_active_download(td::Bits256 hash, bool active, td::Promise promise);
  void set_active_upload(td::Bits256 hash, bool active, td::Promise promise);
  void with_torrent(td::Bits256 hash, td::Promise promise);
  void get_all_torrents(td::Promise> promise);
  void set_all_files_priority(td::Bits256 hash, td::uint8 priority, td::Promise promise);
  void set_file_priority_by_idx(td::Bits256 hash, size_t idx, td::uint8 priority, td::Promise promise);
  void set_file_priority_by_name(td::Bits256 hash, std::string name, td::uint8 priority, td::Promise promise);
  void remove_torrent(td::Bits256 hash, bool remove_files, td::Promise promise);
  void load_from(td::Bits256 hash, td::optional meta, std::string files_path,
                 td::Promise promise);
  void wait_for_completion(td::Bits256 hash, td::Promise promise);
  void get_peers_info(td::Bits256 hash, td::Promise> promise);
  void get_speed_limits(td::Promise> promise);  // Download, upload
  void set_download_speed_limit(double max_speed);
  void set_upload_speed_limit(double max_speed);
 private:
  adnl::AdnlNodeIdShort local_id_;
  std::string db_root_;
  td::unique_ptr callback_;
  bool client_mode_ = false;
  td::actor::ActorId adnl_;
  td::actor::ActorId rldp_;
  td::actor::ActorId overlays_;
  std::shared_ptr db_;
  struct TorrentEntry {
    td::Bits256 hash;
    td::actor::ActorOwn actor;
    td::actor::ActorOwn peer_manager;
    struct ClosingState {
      bool removing = false;
      td::Promise promise;
      bool remove_files = false;
    };
    std::shared_ptr closing_state = std::make_shared();
  };
  std::map torrents_;
  double download_speed_limit_ = -1.0;
  double upload_speed_limit_ = -1.0;
  td::actor::ActorOwn download_speed_limiter_ =
      td::actor::create_actor("DownloadRateLimitrer", -1.0);
  td::actor::ActorOwn upload_speed_limiter_ =
      td::actor::create_actor("DownloadRateLimitrer", -1.0);
  td::Status add_torrent_impl(Torrent torrent, bool start_download, bool allow_upload);
  td::Result get_torrent(td::Bits256 hash) {
    auto it = torrents_.find(hash);
    if (it == torrents_.end()) {
      return td::Status::Error("No such torrent");
    }
    return &it->second;
  }
  td::unique_ptr create_callback(td::Bits256 hash,
                                                      std::shared_ptr closing_state);
  void loaded_config_from_db(tl_object_ptr config);
  void load_torrents_from_db(std::vector torrents);
  void loaded_torrent_from_db(td::Bits256 hash, td::Result> R);
  void after_load_torrents_from_db();
  void db_store_config();
  void db_store_torrent_list();
  void on_torrent_closed(Torrent torrent, std::shared_ptr closing_state);
};
}  // namespace ton