// 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_ISMCTS_H_
#define OPEN_SPIEL_ALGORITHMS_KRIEGSPIEL_ISMCTS_H_

#include <memory>

#include "open_spiel/algorithms/mcts.h"
#include "open_spiel/abseil-cpp/absl/container/flat_hash_map.h"
#include "open_spiel/algorithms/kriegspiel/infoset.h"
#include "open_spiel/abseil-cpp/absl/hash/hash.h"

namespace open_spiel {
namespace algorithms {


class Mat : public std::array<std::array<double, 8>, 8> {
 public:
  double At(const chess::Square& sq) { return (*this)[sq.x][sq.y]; }
  void Set(const chess::Square& sq, double val) {
    (*this)[sq.x][sq.y] = val;
  }
};

struct KriegspielStatistics {
  std::vector<Mat> pawn_probs;
  std::vector<Mat> piece_probs;
  std::vector<Mat> king_probs;
};

struct SimpleKriegspielUmpireMessage {

  bool illegal = false;
  kriegspiel::KriegspielCaptureType capture_type = kriegspiel::KriegspielCaptureType::kNoCapture;
  chess::Square square = chess::kInvalidSquare;
  bool check = false;

  bool operator==(const SimpleKriegspielUmpireMessage &other) const {
    return illegal == other.illegal &&
           capture_type == other.capture_type &&
           square == other.square &&
           check == other.check;
  }

  std::string ToString() const {
    if (illegal) {
      return "illegal";
    }
    std::string str = CaptureTypeToString(capture_type) + " " + SquareToString(square) + " " + std::to_string(check);
    return str;
  }
};

SimpleKriegspielUmpireMessage GetSimpleKriegspielUmpireMessage(const kriegspiel::KriegspielUmpireMessage& orig_msg);

// Hash function for chess move so that we can store it in a hash table (unordered_map)
template <typename H>
H AbslHashValue(H h, const SimpleKriegspielUmpireMessage& m) {
  return H::combine(std::move(h), m.illegal, m.capture_type, m.square.x, m.square.y, m.check);
}

struct SimpleUmpireMessageHash {
  size_t operator() (const SimpleKriegspielUmpireMessage &msg) const {
    return std::hash<std::string>{}(msg.ToString());
  }
};

class ProbabilityBoard {
 public:
  explicit ProbabilityBoard(chess::Color color, Stats* stats, int t = 0);
  ProbabilityBoard(const ProbabilityBoard&) = default;

  std::vector<std::pair<double, SimpleKriegspielUmpireMessage>> MyProbabilisticMessage(chess::Move move);
  std::vector<std::pair<double, SimpleKriegspielUmpireMessage>> OppProbabilisticMessage(const SimpleKriegspielUmpireMessage& my_msg, const chess::Square& checking_square);

  void Update(chess::Move move, SimpleKriegspielUmpireMessage msg);
  void Update(SimpleKriegspielUmpireMessage msg);

  std::vector<chess::Move> LegalMoves() {
    return board_.GenerateLegalMoveVector();
  }

  void Reset();

  //private:

  void MaybeGenerateLegalMoves();

  double Control(const chess::Square& sq);
  double Legal(const chess::Move& move);
  double CapturePiece(const chess::Move& move);
  double CapturePawn(const chess::Move& move);
  double Check(const chess::Move& move);

  void UpdateKingProbs();
  void UpdatePieceProbs();
  void UpdatePawnProbs();

  chess::Square ChooseVictim();
  void Normalize();

  Mat king_probs_ = {};
  Mat piece_probs_ = {};
  Mat pawn_probs_ = {};

  int time_;

  chess::ChessBoard board_;
  std::optional<std::vector<chess::Move>> legal_moves_;
  chess::Color color_;
  Stats* stats_;

  std::mt19937 rng_;
  int captured_pieces_ = 0;
  int captured_pawns_ = 0;

};

enum class ISMCTSFinalPolicyType {
  kMaxVisitCount,
  kMaxValue,
};

//struct OppTurnNode;

struct MyTurnNode {
  //ProbabilityBoard prob_board;
  //int t = 0;

  std::optional<size_t> parent = std::nullopt;
  std::unordered_map<chess::Move, size_t, ChessMoveHash> child_nodes;
};

struct MyMessageNode {
  int visits = 0;
  int available_count = 0;
  double return_sum = 0;

  std::optional<size_t> parent = std::nullopt;
  std::unordered_map<SimpleKriegspielUmpireMessage, size_t, SimpleUmpireMessageHash> child_nodes;
};

struct OppTurnNode {
  std::optional<size_t> parent = std::nullopt;
  std::unordered_map<SimpleKriegspielUmpireMessage, size_t, SimpleUmpireMessageHash> child_nodes;
};

class DarkboardBot : public Bot {
 public:
  DarkboardBot(int seed, int move_time,
               std::function<double(const ProbabilityBoard&, std::optional<SimpleKriegspielUmpireMessage>, chess::Square)> evaluator,
               double uct_c,
               ISMCTSFinalPolicyType final_policy_type,
               chess::Color color,
               Stats* stats);

  void Restart() override;
  void RestartAt(const State& state) override {
    SpielFatalError("Restarting at a specific state not supported");
  }

  Action Step(const State& state) override;
  void InformAction(const State &state, Player player_id, Action action) override;

 protected:
  chess::Move RunSearch(std::chrono::time_point<std::chrono::system_clock> step_end);
  void SetCurrentPlayer(const State &state);

  void Reset();

  chess::Move SelectMoveUCB(MyTurnNode* node, const std::vector<chess::Move>& legal_moves);
  chess::Move GetBestMove() const;

  // Run a simulation, returning the player returns.
  void RunSimulation();

  std::mt19937 rng_;
  std::function<double(ProbabilityBoard, std::optional<SimpleKriegspielUmpireMessage>, chess::Square)> evaluator_;
  std::vector<MyTurnNode> my_turn_node_pool_;
  std::vector<MyMessageNode> my_msg_turn_node_pool_;
  std::vector<OppTurnNode> opp_turn_node_pool_;
  int root_node_idx = 0;

  const double uct_c_;
  const ISMCTSFinalPolicyType final_policy_type_;

  int move_time_;
  std::optional<Player> player_ = std::nullopt;
  chess::Color color_;
  ProbabilityBoard prob_board_;
  std::optional<chess::ChessBoard> current_partial_board = std::nullopt;

  std::optional<chess::Move> last_move_ = {};
  std::optional<kriegspiel::KriegspielUmpireMessage> last_my_msg_ = {};
  std::optional<kriegspiel::KriegspielUmpireMessage> last_opp_msg_ = {};
  std::vector<Action> legal_actions;
};

}  // namespace algorithms
}  // namespace open_spiel


#endif  // OPEN_SPIEL_ALGORITHMS_KRIEGSPIEL_ISMCTS_H_
