// Copyright 2019 DeepMind Technologies Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef OPEN_SPIEL_ALGORITHMS_KRIEGSPIEL_INFOSET_H_
#define OPEN_SPIEL_ALGORITHMS_KRIEGSPIEL_INFOSET_H_

#include <cstdint>

#include "open_spiel/games/kriegspiel.h"
#include "open_spiel/algorithms/kriegspiel/common.h"
#include "open_spiel/algorithms/kriegspiel/stats_collector.h"

namespace open_spiel {
namespace algorithms {

class StatisticalSamplingInfoset {
 public:
  virtual chess::ChessBoard SampleState() = 0;

  virtual void Inform(const chess::Move& move, const kriegspiel::KriegspielUmpireMessage& observation) = 0;
  virtual void Inform(const kriegspiel::KriegspielUmpireMessage& observation, Player player) = 0;
  virtual void Update() = 0;
  virtual std::vector<chess::ChessBoard> Vector() = 0;
  virtual size_t Size() = 0;
  virtual bool Empty() = 0;

  virtual void Reset() = 0;
};

class AOSPInfoset : public StatisticalSamplingInfoset {
 public:
  AOSPInfoset(int seed, int pool_max_size);

  chess::ChessBoard SampleState() override;

  void Inform(const chess::Move& move, const kriegspiel::KriegspielUmpireMessage& message) override;
  void Inform(const kriegspiel::KriegspielUmpireMessage& message, Player player) override;
  void Update() override;
  size_t Size() override { return v_pool_.size(); }
  bool Empty() override { return v_pool_.empty(); }

  void Reset() override;
  std::vector<chess::ChessBoard> Vector() override { return v_pool_; }
  void AddState(chess::ChessBoard board);

  std::mt19937 rng_;

 private:

  void Update(const chess::Move& move, const kriegspiel::KriegspielUmpireMessage& message);
  void Update(const kriegspiel::KriegspielUmpireMessage& message, Player player);

  int pool_max_size_;
  std::unordered_set<chess::ChessBoard, ChessBoardHash> pool0_;
  std::unordered_set<chess::ChessBoard, ChessBoardHash> pool1_;
  int pool_;
  std::vector<chess::ChessBoard> v_pool_;

  std::optional<chess::Move> my_last_move_ = std::nullopt;
  std::optional<kriegspiel::KriegspielUmpireMessage> after_my_turn_observation_ = std::nullopt;
  std::vector<std::pair<Player, kriegspiel::KriegspielUmpireMessage>> after_opp_turn_observations_;
};

class LOSInfoset : public StatisticalSamplingInfoset {
 public:
  LOSInfoset(int seed, chess::Color color);

  chess::ChessBoard SampleState() override;

  void Inform(const chess::Move& move, const kriegspiel::KriegspielUmpireMessage& message) override;
  void Inform(const kriegspiel::KriegspielUmpireMessage& message, Player player) override;
  void Update() override { };
  size_t Size() override { return -1; }
  bool Empty() override { return false; }

  void Reset() override;
  std::vector<chess::ChessBoard> Vector() override { return {}; }

  std::mt19937 rng_;

// private:

  std::optional<chess::Move> my_last_move_ = std::nullopt;
  std::optional<kriegspiel::KriegspielUmpireMessage> after_my_turn_observation_ = std::nullopt;
  std::optional<kriegspiel::KriegspielUmpireMessage> after_opp_turn_observation_ = std::nullopt;

  Stats stats_;
  int ply_ = 0;
  int captured_pawns_ = 0;
  int captured_pieces_ = 0;
  chess::ChessBoard board_;
  chess::Color color_;
};

class HSInfoset : public StatisticalSamplingInfoset {
 public:
  HSInfoset(int seed, chess::Color color, int max_pool_size);

  chess::ChessBoard SampleState() override;

  void Inform(const chess::Move& move, const kriegspiel::KriegspielUmpireMessage& message) override;
  void Inform(const kriegspiel::KriegspielUmpireMessage& message, Player player) override;
  void Update() override;
  size_t Size() override { return aosp_infoset_.Size(); }
  bool Empty() override { return aosp_infoset_.Empty(); }

  void Reset() override;
  std::vector<chess::ChessBoard> Vector() override { return aosp_infoset_.Vector(); }

 private:

  LOSInfoset los_infoset_;
  AOSPInfoset aosp_infoset_;

  int max_pool_size;
  bool already_was_higher_;
  chess::Color color_;
};

}  // namespace algorithms
}  // namespace open_spiel


#endif  // OPEN_SPIEL_ALGORITHMS_KRIEGSPIEL_INFOSET_H_
