package de.htw_berlin.HoboOthello.KI;

import de.htw_berlin.HoboOthello.Core.*;

import java.util.ArrayList;
import java.util.List;


public class KI extends Player {

    private Color kiColor;
    private Level level;

    private Board board;
    private Field field;
    private GameRule gameRule;

    public KI(Color kiColor) {
        this.kiColor = kiColor;
    }

    /**
     * Method which will be used by the controller to let the KI pick a field
     *
     * @param board actual board, information for the KI to act upon
     * @return Field with coordinates where the KI wants to put down a stone
     */
    //TODO bestimmen was wir machen wenn das field null ist
    //rückgabe an controller ist ein field
    public Field setMove(Board board) {
        if (level == level.LEVEL1) {
            return pickRandomFieldFromList();
        }

        if (level == level.LEVEL2) {
            Field fieldToSetMove;
            fieldToSetMove = pickCornerOrSideFieldFromList();
            if (fieldToSetMove == null) {
                fieldToSetMove = pickRandomFieldFromList();
            }
            return fieldToSetMove;
        }

        if (level == level.LEVEL3) {
            //return Field fieldToSetMove;
            return null;

        } else {
            throw new IllegalArgumentException("Level of KI is off...!");
        }
    }

    /**
     * Method which lists all possible moves for the KI
     *
     * @return listOfPossibleMoves
     */
    //TODO: what do we do if there are no possible moves for the ki; aka list is empty
    private List<Field> listPossibleMoves() {
        List<Field> listOfPossibleMoves = new ArrayList<Field>();

        for (Field field : board.iterateThroughAllFields()) {
            if (gameRule.isMoveAllowed(field, kiColor)) {
                listOfPossibleMoves.add(field);
            }
        }
        return listOfPossibleMoves;
    }

    /**
     * Method which picks a random move from the list of all possible moves for the KI
     *
     * @return field which is randomly chosen by this method
     */
    private Field pickRandomFieldFromList() {
        List<Field> listOfPossibleMoves = listPossibleMoves();
        int randomNumber = (int) (Math.random() * listOfPossibleMoves.size()); // picks random index of field in list
        return listOfPossibleMoves.get(randomNumber);
    }

    /**
     * Method which picks the first possible corner field or the first possible side field for a turn
     * If there are no corner or side fields possible for this turn, this method returns null
     *
     * @return cornerOrSideField a possible corner or side field, null field if both are impossible
     */
    private Field pickCornerOrSideFieldFromList() {
        List<Field> listOfPossibleMoves = listPossibleMoves();
        Field cornerOrSideField = null;

        int cornerFieldIndex = 0;
        while (cornerFieldIndex < listOfPossibleMoves.size()) {
            Field field = listOfPossibleMoves.get(cornerFieldIndex);
            if (board.isCornerField(field)) {
                cornerOrSideField = field;
            } else {
                cornerFieldIndex++;
            }
        }

        int sideFieldIndex = 0;
        while (sideFieldIndex < listOfPossibleMoves.size()) {
            Field field = listOfPossibleMoves.get(sideFieldIndex);
            if (board.isSideField(field)) {
                cornerOrSideField = field;
            } else {
                sideFieldIndex++;
            }
        }
        return cornerOrSideField;
    }



}

@@ -617,67 +617,8 @@
  */%0A
-    //TODO bestimmen was wir machen wenn das field null ist
 %0A   
@@ -693,24 +693,62 @@
 rd board) %7B%0A
+        Field fieldToSetMove = null;%0A%0A
         if (
@@ -784,22 +784,32 @@
         
-return
+fieldToSetMove =
  pickRan
@@ -820,32 +820,67 @@
 ieldFromList();%0A
+            return fieldToSetMove;%0A
         %7D%0A%0A     
@@ -915,42 +915,8 @@
 ) %7B%0A
-            Field fieldToSetMove;%0A
     
@@ -1188,23 +1188,15 @@
     
-//
 return 
-Field 
 fiel
@@ -1211,32 +1211,8 @@
 ve;%0A
-            return null;
 %0A%0A  
@@ -1435,95 +1435,8 @@
  */%0A
-    //TODO: what do we do if there are no possible moves for the ki; aka list is empty%0A
     
@@ -1988,32 +1988,58 @@
 eldFromList() %7B%0A
+        Field fieldToSet;%0A
         List%3CFie
@@ -2203,38 +2203,44 @@
 in list%0A        
-return
+fieldToSet =
  listOfPossibleM
@@ -2263,16 +2263,43 @@
 umber);%0A
+        return fieldToSet;%0A
     %7D%0A%0A 
@@ -3479,11 +3479,10 @@
     %7D%0A%0A%0A
-%0A
 %7D%0A

update in KI; return null is move is impossible