package com.kduda.battleships.controllers;

import com.kduda.battleships.models.board.Board;
import com.kduda.battleships.models.board.Cell;
import com.kduda.battleships.models.board.Position;
import com.kduda.battleships.models.units.Unit;
import com.kduda.battleships.models.units.UnitFactory;
import javafx.fxml.Initializable;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;

import java.net.URL;
import java.util.ResourceBundle;

public class BattleshipsController implements Initializable {
    private VBox enemyBoardArea;
    private VBox playerBoardArea;
    private Board enemyBoard;
    private Board playerBoard;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initializeBoards();
    }

    private void initializeBoards() {
        enemyBoard = new Board(true, this::enemyBoardClick, this::enemyBoardEntered, this::enemyBoardExited);
        enemyBoardArea.getChildren().add(enemyBoard);

        playerBoard = new Board(false, this::playerBoardClick, this::playerBoardEntered, this::playerBoardExited);
        playerBoardArea.getChildren().addAll(playerBoard);


    }

    private void enemyBoardClick(MouseEvent event) {
        //TODO: click handler
        if (!BattleshipsConfig.INSTANCE.isGameRunning)
            return;
    }

    private void enemyBoardEntered(MouseEvent event) {
        //TODO: mouse entered handler
        return;
    }

    private void enemyBoardExited(MouseEvent event) {
        //TODO: mouse exited handler
    }

    private void playerBoardEntered(MouseEvent event) {
        if (BattleshipsConfig.INSTANCE.isGameRunning)
            return;

        //TODO: hint gdzie bedzie jednostka i czy ok
//        Cell cell = (Cell) event.getSource();

    }

    private void playerBoardExited(MouseEvent event) {
        //TODO: mouse exited handler
    }

    private void playerBoardClick(MouseEvent event) {
        if (BattleshipsConfig.INSTANCE.isGameRunning)
            return;

        Unit unit = UnitFactory.INSTANCE.getNextUnit();

        if (unit == null) {
            startGame();
        }

        Cell cell = (Cell) event.getSource();
        Position cellPosition = new Position(cell.POSITION.getX(), cell.POSITION.getY());
        boolean wasPlacementSuccessful = playerBoard.placeUnit(unit, cellPosition);

        if (!wasPlacementSuccessful)
            UnitFactory.INSTANCE.getPreviousUnit();
    }


    //HANDLERS

//        enemyBoard = new Board(true, event -> {
//            if (!running)
//                return;
//
//            Cell cell = (Cell) event.getSource();
//            if (cell.wasShot)
//                return;
//
//            enemyTurn = !cell.shootCell();
//
//            if (enemyBoard.ships == 0) {
//                System.out.println("YOU WIN");
//                System.exit(0);
//            }
//
//            if (enemyTurn)
//                enemyMove();
//        });


    private void startGame() {
        //TODO: place enemy ships
        BattleshipsConfig.INSTANCE.isGameRunning = true;
    }
}

@@ -513,30 +513,29 @@
 able %7B%0A    p
-rivate
+ublic
  VBox enemyB
@@ -549,22 +549,21 @@
 a;%0A    p
-rivate
+ublic
  VBox pl
@@ -573,24 +573,25 @@
 rBoardArea;%0A
+%0A
     private 
@@ -638,16 +638,46 @@
 erBoard;
+%0A    private Unit currentUnit;
 %0A%0A    @O
@@ -749,24 +749,82 @@
 esources) %7B%0A
+        currentUnit = UnitFactory.INSTANCE.getNextUnit();%0A
         init
@@ -1223,18 +1223,16 @@
 Board);%0A
-%0A%0A
     %7D%0A%0A 
@@ -1594,32 +1594,48 @@
  exited handler%0A
+        return;%0A
     %7D%0A%0A    priva
@@ -1809,18 +1809,16 @@
  czy ok%0A
-//
         
@@ -1854,16 +1854,59 @@
 ource();
+%0A//        playerBoard.highlightHint(unit);
 %0A%0A    %7D%0A
@@ -1994,24 +1994,40 @@
 ted handler%0A
+        return;%0A
     %7D%0A%0A    p
@@ -2162,70 +2162,20 @@
     
-Unit unit = UnitFactory.INSTANCE.getNextUnit();%0A%0A        if (u
+if (currentU
 nit 
@@ -2420,17 +2420,24 @@
 aceUnit(
-u
+currentU
 nit, cel
@@ -2461,17 +2461,16 @@
     if (
-!
 wasPlace
@@ -2496,16 +2496,35 @@
         
+ this.currentUnit =
  UnitFac
@@ -2544,16 +2544,12 @@
 .get
-Previous
+Next
 Unit
@@ -2574,17 +2574,34 @@
 HANDLERS
-%0A
+ enemy board click
 %0A//     

Add global current unit for placement