// 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_GAMES_CHESS_PGN_PARSER_H_
#define OPEN_SPIEL_GAMES_CHESS_PGN_PARSER_H_

#include <array>
#include <cstdint>
#include <functional>
#include <ostream>
#include <istream>
#include <fstream>
#include <string>
#include <utility>
#include <vector>

#include "open_spiel/abseil-cpp/absl/types/optional.h"
#include "open_spiel/games/chess/chess_common.h"
#include "open_spiel/games/chess/pgn.h"
#include "open_spiel/spiel_utils.h"
#include "open_spiel/games/kriegspiel.h"

namespace open_spiel {
namespace pgn {

struct Turn {
  int turn_number;
  std::string white_move;
  absl::optional<std::string> black_move;
};

class Parser
{
 public:

  static
  bool GetGames(std::string::const_iterator &itr1,
                         const std::string::const_iterator &itr2, std::vector<ChessPGN> &out);
  static
  absl::optional<ChessPGN> GetGame(std::string::const_iterator &itr1,
               const std::string::const_iterator &itr2);

  static
  bool GetTurns(std::string::const_iterator &itr1,
                   const std::string::const_iterator &itr2, std::vector<Turn> &out);
  static
  absl::optional<Turn> GetTurn(std::string::const_iterator &itr1,
               const std::string::const_iterator &itr2);
  static
  absl::optional<std::string> GetPly(std::string::const_iterator &itr1,
              const std::string::const_iterator &itr2);
  static
  bool GetTags(std::string::const_iterator &itr1,
                  const std::string::const_iterator &itr2, std::vector<std::pair<std::string, std::string>> &out);
  static
  absl::optional<std::pair<std::string, std::string>> GetTag(std::string::const_iterator &itr1,
              const std::string::const_iterator &itr2);
  static
  absl::optional<std::vector<double>> GetGameResult(std::string::const_iterator &itr1,
                     const std::string::const_iterator &itr2);
  static
  absl::optional<std::string> GetComment(std::string::const_iterator &itr1,
                  const std::string::const_iterator &itr2);
  static
  absl::optional<int> GetMoveNumber(std::string::const_iterator &itr1,
                     const std::string::const_iterator &itr2);

  static
  int GetGlyph(std::string::const_iterator &itr1, const std::string::const_iterator &itr2);

  static
  bool GetVariation(std::string::const_iterator &itr1,
                    const std::string::const_iterator &itr2, std::vector<Turn> &out);
};


}  // namespace pgn
}  // namespace open_spiel

#endif  // OPEN_SPIEL_GAMES_CHESS_PGN_PARSER_H_
