package project.ilyagorban.model.figures;

import java.util.ArrayList;

import project.ilyagorban.model.Board;
import project.ilyagorban.model.Rank;
import project.ilyagorban.model.XY;
import static project.ilyagorban.model.ChessModel.*;

// ♚♔ figures
public class King extends Figure {
	
	public King(XY p, Rank r) {
		super(p, r);
		setDirections(directionsOfQueen);
		setMoveLen(1);
	}
	
	@Override
	public ArrayList<XY> getPossibleMoves(Board board) {
		ArrayList<XY> output = super.getPossibleMoves(board);
		
		boolean isUntouchedKing = this.getRank().getIndex().equals("k") && this.isTouched() == false;
		if (isUntouchedKing == true) {
			// left and right castling check:
			for (int x = 0; x <= 7; x = x + 7) {
				int stepsX = x - this.getXY().getX();
				int direction = stepsX / Math.abs(stepsX);
				Figure rook = board.getFigure(x, this.getXY().getY());
				if (rook != null && rook.isTouched() == false) {
					boolean isAbleToCastle = true;
					for (int i = 1; i < Math.abs(stepsX); i++) {
						int newX = this.getXY().getX() + i * direction;
						Figure fig = board.getFigure(newX, this.getXY().getY());
						boolean isEmpty = (fig == null);
						if (isEmpty) {
							continue;
						} else {
							isAbleToCastle = false;
							break;
						}
					}
					if (isAbleToCastle == true)
						output.add(XY.getNewXY(this.getXY().getX() + direction * 2, this.getXY().getY()));
				}
			}
		}
		
		return output;
	}
	
	@Override
	public int checkMove(Board board, XY to) {
		int output = super.checkMove(board, to);
		if (output < CORRECT_MOVE)
			return output;
		else {
			if (Math.abs(this.getXY().getX() - to.getX()) == 2)
				output = CASTLING;
		}
		return output;
	}
	
	public boolean isUnderAttack(Board board) {
		boolean output = false;
		for (int i = 0; i < 3; i++) {
			if (output == true)
				break;
			this.setDirections(fourFigureDirections[i]);
			this.setMoveLen(fourFigureMoveLen[i]);
			ArrayList<XY> pm = this.getPossibleMoves(board);
			if (pm.size() != 0)
				for (XY xy : pm) {
					Figure fig = board.getFigure(xy);
					if (fig != null) {
						for (String s : fourFigureIndex[i]) {
							boolean isThreat = fig.isEnemy(this) && fig.getRank().getIndex().equals(s);
							if (isThreat) {
								output = true;
								break;
							}
						}
					}
				}
		}
		if (output == false) {
			ArrayList<XY> ppa = this.getPawnPossibleAttack(board);
			for (XY xy : ppa) {
				Figure fig = board.getFigure(xy);
				if (this.isEnemy(fig) && fig.getRank().getIndex() == "p") {
					output = true;
					break;
				}
			}
		}
		this.setDirections(directionsOfQueen);
		this.setMoveLen(1);
		return output;
		
	}
	
}

@@ -506,19 +506,89 @@
 board);%0A
+%09%09int kingX = this.getXY().getX();%0A%09%09int kingY = this.getXY().getY();%0A
 %09%09%0A
-
 %09%09boolea
@@ -806,35 +806,21 @@
 X = x - 
-this.getXY().getX()
+kingX
 ;%0A%09%09%09%09in
@@ -897,35 +897,21 @@
 gure(x, 
-this.getXY().getY()
+kingY
 );%0A%09%09%09%09i
@@ -1061,35 +1061,21 @@
  newX = 
-this.getXY().getX()
+kingX
  + i * d
@@ -1125,35 +1125,21 @@
 e(newX, 
-this.getXY().getY()
+kingY
 );%0A%09%09%09%09%09
@@ -1351,64 +1351,36 @@
 wXY(
-this.getXY().getX() + direction * 2, this.getXY().getY()
+kingX + direction * 2, kingY
 ));%0A
@@ -1576,20 +1576,41 @@
 se %7B%0A%09%09%09
-if (
+boolean isAbleToCastle = 
 Math.abs
@@ -1647,16 +1647,39 @@
 ()) == 2
+;%0A%09%09%09if (isAbleToCastle
 )%0A%09%09%09%09ou
@@ -1948,16 +1948,16 @@
 en%5Bi%5D);%0A
-
 %09%09%09Array
@@ -2000,16 +2000,91 @@
 board);%0A
+%09%09%09if (pm == null) %7B%0A%09%09%09%09System.out.println(this.toLog());%0A%09%09%09%09break;%0A%09%09%09%7D%0A
 %09%09%09if (p

fix Exception(on castling)