// 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_PIMC_H_
#define OPEN_SPIEL_ALGORITHMS_PIMC_H_

#include <cstdint>
#include <memory>
#include <random>
#include <string>
#include <utility>
#include <vector>

#include "open_spiel/spiel.h"
#include "open_spiel/spiel_bots.h"
#include "open_spiel/games/chess.h"
#include "open_spiel/games/kriegspiel.h"
#include "open_spiel/algorithms/kriegspiel/infoset.h"
#include "open_spiel/algorithms/kriegspiel/common.h"

namespace open_spiel {
namespace algorithms {

struct ActionStatsWeights {
  double min;
  double max;
  double average;
  double wins;
};

struct MoveStats {
  double min;
  double max;
  double sum;
  int count;
  int wins;

  MoveStats& operator+=(const MoveStats& other) {
    if (other.min < min) min = other.min;
    if (other.max > max) max = other.max;
    wins += other.wins;
    sum += other.sum;
    count += other.count;
    return *this;
  }
};

class PIMCBot : public Bot {
 public:
  PIMCBot(int move_time, int min_state_eval_time,
          StatisticalSamplingInfoset *information_set,
          std::function<std::unordered_map<chess::Move, double, ChessMoveHash>(const chess::ChessBoard&, int)> action_evaluator,
          ActionStatsWeights action_stats_weights);
  ~PIMCBot() override = default;

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

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

 private:

  void SetCurrentPlayer(const State &state);
  std::unordered_map<chess::Move, MoveStats, ChessMoveHash> ComputeMoveStatsMap(std::chrono::time_point<std::chrono::system_clock> step_end);
  chess::Move GetBestMove(const std::unordered_map<chess::Move, MoveStats, ChessMoveHash> &action_stats_map);

  int move_time_;
  int min_state_eval_time_;
  std::optional<Player> player = std::nullopt;
  StatisticalSamplingInfoset *information_set_;
  std::function<std::unordered_map<chess::Move, double, ChessMoveHash>(const chess::ChessBoard &, uint)> move_evaluator_;
  ActionStatsWeights action_stats_weights_;
};

}  // namespace algorithms
}  // namespace open_spiel


#endif  // OPEN_SPIEL_ALGORITHMS_PIMC_H_
