package fr.iut_fbleau.HexGame;

import fr.iut_fbleau.GameAPI.*;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;

public class Arena {

    private List<AbstractGamePlayer> bots = new ArrayList<>();
    private FileWriter csvWriter;
    private int board_size;

    public Arena(int size) {
        try {
            csvWriter = new FileWriter("arena_results.csv");
            csvWriter.append("Bot 1, Bot 2, Winner\n");
            this.board_size = size;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addBot(AbstractGamePlayer bot) {
        bots.add(bot);
    }

    public void run() {
        for (int i = 0; i < bots.size(); i++) {
            for (int j = i + 1; j < bots.size(); j++) {
                AbstractGamePlayer bot1 = bots.get(i);
                AbstractGamePlayer bot2 = bots.get(j);

                System.out.println("Running match: " + bot1.getClass().getSimpleName() + " vs " + bot2.getClass().getSimpleName());
                Result result = playMatch(bot1, bot2);

                try {
                    csvWriter.append(bot1.getClass().getSimpleName() + "," + bot2.getClass().getSimpleName() + "," + result + "\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            csvWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Result playMatch(AbstractGamePlayer bot1, AbstractGamePlayer bot2) {
        IBoard board = new HexBoard(this.board_size);
        EnumMap<Player, AbstractGamePlayer> players = new EnumMap<>(Player.class);
        players.put(Player.PLAYER1, bot1);
        players.put(Player.PLAYER2, bot2);

        Simulation simulation = new Simulation(board, players);
        return simulation.runForArena();
    }
}
