package fr.iut_fbleau.HexGame;

import fr.iut_fbleau.GameAPI.Player;
import fr.iut_fbleau.GameAPI.Result;

import javax.swing.*;
import java.awt.*;

public class HexFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            int size = 11;
            if (args.length >= 1) {
                try { size = Integer.parseInt(args[0]); } catch (NumberFormatException ignored) {}
            }

            HexBoard board = new HexBoard(size);

            JFrame frame = new JFrame("Hex - " + size + "x" + size);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            JLabel statusLabel = new JLabel("", SwingConstants.CENTER);
            statusLabel.setFont(statusLabel.getFont().deriveFont(Font.BOLD, 18f));
            statusLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

            HexPanel panel = new HexPanel(board, statusLabel);

            frame.add(statusLabel, BorderLayout.NORTH);
            frame.add(panel, BorderLayout.CENTER);

            // Taille confortable
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            // Message initial
            updateStatus(board, statusLabel);
        });
    }

    static void updateStatus(HexBoard board, JLabel statusLabel) {
        if (board.isGameOver()) {
            Result r = board.getResult(); // résultat du point de vue PLAYER1
            Player winner = (r == Result.WIN) ? Player.PLAYER1 : Player.PLAYER2;
            statusLabel.setText("" + winner + " a gagné !");
        } else {
            statusLabel.setText("C'est à " + board.getCurrentPlayer() + " de jouer");
        }
    }
}
