import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class BoardDisplay extends JPanel {
    private JButton[][] fields;
    private int gridSize;
    private List <Ship> ships;

    public BoardDisplay(int gridSize, Player player) {
        super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Splate
        this.fields = new JButton[gridSize][gridSize];
        this.ships = new ArrayList<>();
        // Erstellung von Spielfeld
        for (int i = 0; i <= gridSize; i++) {
            for (int j = 0; j <= gridSize; j++) {
                //final int x = i; // Temporäre Variable
               // final int y = j; // Temporäre Variable
                if (i == 0 && j == 0) {
                    add(new JLabel(" "));
                } else if (i == 0) {
                    JLabel colLabel = new JLabel(String.valueOf(j));
                    colLabel.setHorizontalAlignment(SwingConstants.CENTER);
                    colLabel.setFont(new Font("Arial", Font.BOLD, 14));
                    add(colLabel);
                } else if (j == 0) {
                    JLabel rowLabel = new JLabel(String.valueOf((char) ('A' + i - 1)));
                    rowLabel.setHorizontalAlignment(SwingConstants.CENTER);
                    rowLabel.setFont(new Font("Arial", Font.BOLD, 14));
                    add(rowLabel);
                } else {
                    // Spielfeld (interaktive Zellen)
                    JButton field = new JButton("");
                    field.setBackground(Color.LIGHT_GRAY);
                    field.setOpaque(true);
                    field.setBorderPainted(true);
                    fields[i - 1][j - 1] = field;
                    add(field);
                    //field.addMouseListener(new MouseAdapter() {
                       // @Override
                        //public void mouseClicked(MouseEvent e) {
                         //   if (SwingUtilities.isRightMouseButton(e)) {
                         //       handleFieldClick(field);
                         //   }
                       // }

                        //@Override
                        //public void mouseExited(MouseEvent e) {
                            // field.setBackground(Color.LIGHT_GRAY);
                       // }
                   // });
                    int finalI = i;
                    int finalJ = j;
                    field.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            Point o= new Point(finalI, finalJ);
                            System.out.println(o);
                            handleFieldClick(field, o,player);
                        }
                    });
                }
            }
        }

       // this.ships = new ArrayList<Ship>();
    }

    private boolean setShip(Ship ship, Point o, boolean horizontal,Player player) {
        //boolean a = true;
        if (placeable(ship, ship.getPosition(), horizontal)) {
           // ship.setPosition(new Point(o.getX(), o.getY()), player.getBoard().getShips(), gridSize);
            ship.setPosition(o, player.getBoard().getShips(), gridSize);


            ship.setHorizontal(horizontal);
            ships.add(ship);
            List<Point> occupied = ship.getOccupiedPoints();
            for(Point p: occupied) {
                fields[(int) p.getX()][(int)p.getY()].setBackground(Color.LIGHT_GRAY);
            }
            return true;
        }else{
            return false;
        }
    }

    /*private boolean placeable(Ship ship,Point o, boolean horizontal) {
        if (horizontal && (o.getX() + ship.getSize() > gridSize)) { // Fehler
            return false;
        }
        if (!horizontal && (o.getY() + ship.getSize() > gridSize)) {
            return false;
        }
        return true;
    }*/

    private boolean placeable(Ship ship, Point o, boolean horizontal) {
        if (horizontal && (o.getX() + ship.getSize() > gridSize)) {
            return false;
        }
        if (!horizontal && (o.getY() + ship.getSize() > gridSize)) {
            return false;
        }

        // Prüfen auf Kollision mit bestehenden Schiffen
        for (Ship other : ships) {
            for (Point p : other.getOccupiedPoints()) {
                for (Point newP : ship.getOccupiedPoints()) {
                    if (p.equals(newP)) {
                        return false; // Überschneidung gefunden
                    }
                }
            }
        }

        return true;
    }


    private void selectShip(MouseEvent e) {
        Ship current = (Ship) e.getSource();
    }

    private void handleFieldClick(JButton field, Point o,Player player) {
        // Beispiel: Setze ein Schiff bei einem Klick
        if (setShip(new Ship(3, "TestShip"), o, true,player)) {
            field.setBackground(Color.BLUE); // Visualisiere Schiff
        }
    }
}