/*
    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 "interfaces/validator-manager.h"
namespace ton::validator {
class QueueSizeCounter : public td::actor::Actor {
 public:
  QueueSizeCounter(td::Ref last_masterchain_state, td::Ref opts,
                   td::actor::ActorId manager)
      : init_masterchain_state_(last_masterchain_state), opts_(std::move(opts)), manager_(std::move(manager)) {
  }
  void start_up() override;
  void get_queue_size(BlockIdExt block_id, td::Promise promise);
  void alarm() override;
  void update_options(td::Ref opts) {
    opts_ = std::move(opts);
  }
 private:
  td::Ref init_masterchain_state_;
  td::Ref opts_;
  td::actor::ActorId manager_;
  bool simple_mode_ = false;
  BlockSeqno current_seqno_ = 0;
  std::vector init_top_blocks_;
  std::vector last_top_blocks_;
  struct Entry {
    bool started_ = false;
    bool done_ = false;
    bool calc_whole_ = false;
    td::uint64 queue_size_ = 0;
    std::vector> promises_;
  };
  std::map results_;
  void get_queue_size_ex(BlockIdExt block_id, bool calc_whole, td::Promise promise);
  void get_queue_size_cont(BlockHandle handle, td::Ref state);
  void get_queue_size_cont2(td::Ref state, td::Ref prev_state, td::uint64 prev_size);
  void on_error(BlockIdExt block_id, td::Status error);
  void process_top_shard_blocks();
  void process_top_shard_blocks_cont(td::Ref state, bool init = false);
  void get_queue_size_ex_retry(BlockIdExt block_id, bool calc_whole, td::Promise promise);
  void process_top_shard_blocks_finish();
  void wait_shard_client();
  bool is_block_too_old(const BlockIdExt& block_id) const {
    for (const BlockIdExt& top_block : last_top_blocks_) {
      if (shard_intersects(block_id.shard_full(), top_block.shard_full())) {
        if (block_id.seqno() + 100 < top_block.seqno()) {
          return true;
        }
        break;
      }
    }
    for (const BlockIdExt& init_top_block : init_top_blocks_) {
      if (shard_intersects(block_id.shard_full(), init_top_block.shard_full())) {
        if (block_id.seqno() < init_top_block.seqno()) {
          return true;
        }
        break;
      }
    }
    return false;
  }
};
}  // namespace ton::validator