package impl.actions;

import game.impl.Board;
import game.impl.BoardLocation;
import game.impl.GamePiece;
import game.impl.Player;
import impl.util.BoardParser;
import impl.util.GameRules;
import impl.State;

import java.util.List;


public class BoardUpdate {

    private Board board;
    private List<Player> players;
    private BoardLocation location;
    private BoardParser boardParser;

    public BoardUpdate(State state, BoardParser boardParser){
        this.players = state.getPlayers();
        this.board = state.getBoard();
        this.boardParser = boardParser;
        this.location = boardParser.getLocation();
    }

    public void execute(){
        updatePartial(boardParser.getRow());
        updatePartial(boardParser.getColumn());
        updatePartial(boardParser.getLeftToRightDiagonal());
        updatePartial(boardParser.getRightToLeftDiagonal());
    }


    private void updatePartial(List<BoardLocation> list){

        int locationIndex = list.indexOf(location);
        Player owner = getOwnerOfPiece(location.getPiece());

        for(int i = locationIndex-1; i >= 0; i-- ){
            if(listEnded(list.get(i), owner))
                break;

            changeOwnerOfPieceAtLocation(list.get(i));
        }

        for(int i = locationIndex+1; i < list.size(); i++){
            if(listEnded(list.get(i), owner))
                break;

            changeOwnerOfPieceAtLocation(list.get(i));
        }
    }

    private boolean listEnded(BoardLocation location, Player owner){
        return GameRules.isLocationEmpty(location) || getOwnerOfPiece(location.getPiece()) == owner;
    }

    private Player getOwnerOfPiece(GamePiece piece){
        if(GameRules.isPlayerOnePiece(piece))
            return players.get(0);

        return players.get(1);
    }

    private void changeOwnerOfPieceAtLocation(BoardLocation location){
        GamePiece piece = location.getPiece();
        Player newOwner, oldOwner = getOwnerOfPiece(piece);
        String newPieceId;

        if(oldOwner == players.get(0)){
            newOwner = players.get(1);
            newPieceId = "X";
        }
        else{
            newOwner = players.get(0);
            newPieceId = "O";
        }

        oldOwner.getPieces().remove(piece);
        piece = new GamePiece(newPieceId);
        newOwner.getPieces().add(piece);
        location.setPiece(piece);
    }
}

@@ -1091,17 +1091,19 @@
 ndex
--
+ - 
 1; i %3E= 
 0; i
@@ -1102,87 +1102,65 @@
  %3E= 
-0; i-- )%7B%0A            if(listEnded(list.get(i), owner))%0A                break;%0A
+getBackwardsUpdateIndex(owner, list, locationIndex); i--)
 %0A   
@@ -1211,25 +1211,16 @@
 et(i));%0A
-        %7D
 %0A%0A      
@@ -1250,346 +1250,131 @@
 ndex
-+
+ + 
 1; i %3C
- list.size(); i++)%7B%0A            if(listEnded(list.get(i), owner))%0A                break;%0A%0A            changeOwnerOfPieceAtLocation(list.get(i));%0A        %7D%0A    %7D%0A%0A    private boolean listEnded(BoardLocation location, Player owner)%7B%0A        return GameRules.isLocationEmpty(location) %7C%7C getOwnerOfPiece(location.getPiece()) == owner;
+= getForwardUpdateIndex(owner, list, locationIndex); i++)%0A            changeOwnerOfPieceAtLocation(list.get(i));%0A%0A
 %0A   
@@ -2137,10 +2137,769 @@
 ;%0A    %7D%0A
+%0A    private int getBackwardsUpdateIndex(Player owner, List%3CBoardLocation%3E list, int startIndex)%7B%0A%0A        for(int i = startIndex-1; i %3E= 0; i-- )%7B%0A            if(GameRules.isLocationEmpty(list.get(i)))%0A                return startIndex;%0A%0A            if(getOwnerOfPiece(list.get(i).getPiece()) == owner)%0A                return i;%0A        %7D%0A%0A%0A        return startIndex;%0A    %7D%0A%0A    private int getForwardUpdateIndex(Player owner, List%3CBoardLocation%3E list, int startIndex)%7B%0A%0A        for(int i = startIndex+1; i %3C list.size(); i++) %7B%0A            if (GameRules.isLocationEmpty(list.get(i)))%0A                return startIndex;%0A%0A            if (getOwnerOfPiece(list.get(i).getPiece()) == owner)%0A                return i;%0A        %7D%0A%0A        return startIndex;%0A    %7D%0A%0A
 %7D%0A

Fix boardupdate bug