package edu.utdallas.robotchess.manager;

import java.util.ArrayList;

import edu.utdallas.robotchess.game.*;

public abstract class Manager
{
    private int boardRowCount;
    private int boardColumnCount;

    protected ChessGame game;
    protected ChessPiece currentlySelectedPiece;

    protected Manager()
    {
        boardRowCount = 8;
        boardColumnCount = 8;

        game = new ChessGame();
        currentlySelectedPiece = null;
    }

    public void handleSquareClick(int index)
    {
        if (isInitialPieceSelection(index))
            handleInitialPieceSelection(index);
        else
            handleMoveLocationSelection(index);
    }

    private boolean isInitialPieceSelection(int selectionIndex)
    {
        if (currentlySelectedPiece == null)
            return true;

        ArrayList<Integer> possibleMoveLocations = getValidMoveLocations();
        for (int i = 0; i < possibleMoveLocations.size(); i++)
            if (selectionIndex == possibleMoveLocations.get(i))
                return false;

        return true;
    }

    private void handleInitialPieceSelection(int selectionIndex)
    {
        if (isValidInitialPieceSelection(selectionIndex))
            makeUpdatesFromValidPieceSelection(selectionIndex);
        else
            currentlySelectedPiece = null;
    }

    protected abstract boolean isValidInitialPieceSelection(int selectionIndex);

    private void makeUpdatesFromValidPieceSelection(int selectionIndex)
    {
        Square selectedSquare = game.getBoardSquareAt(selectionIndex);
        currentlySelectedPiece = selectedSquare.getOccupant();
    }

    private void handleMoveLocationSelection(int selectionIndex)
    {
        if (isValidMoveLocationSelection(selectionIndex))
            makeUpdatesFromValidMoveSelection(selectionIndex);
    }

    public abstract ArrayList<Integer> getValidMoveLocations();
    protected abstract boolean isValidMoveLocationSelection(int selectionIndex);
    protected abstract void makeUpdatesFromValidMoveSelection(int selectionIndex);

    public ArrayList<ChessPiece> getActivePieces()
    {
        return game.getActivePieces();
    }

    public ChessPiece getCurrentlySelectedPiece()
    {
        return currentlySelectedPiece;
    }

    public int getBoardRowCount()
    {
        return boardRowCount;
    }

    public int getBoardColumnCount()
    {
        return boardColumnCount;
    }

    public void setBoardRowCount(int boardRowCount)
    {
        this.boardRowCount = boardRowCount;
    }

    public void setBoardColumnCount(int boardColumnCount)
    {
        this.boardColumnCount = boardColumnCount;
    }
}

@@ -513,16 +513,21 @@
   if (is
+Valid
 InitialP
@@ -544,24 +544,24 @@
 ion(index))%0A
-
             
@@ -601,32 +601,32 @@
 );%0A        else%0A
+
             hand
@@ -668,410 +668,8 @@
  %7D%0A%0A
-    private boolean isInitialPieceSelection(int selectionIndex)%0A    %7B%0A        if (currentlySelectedPiece == null)%0A            return true;%0A%0A        ArrayList%3CInteger%3E possibleMoveLocations = getValidMoveLocations();%0A        for (int i = 0; i %3C possibleMoveLocations.size(); i++)%0A            if (selectionIndex == possibleMoveLocations.get(i))%0A                return false;%0A%0A        return true;%0A    %7D%0A%0A
     

get rid of the method for determining if a selection is an initial piece selection