/**
 *Board and movement of pieces
 *@author arghavan soleymanpour
 *@version 2.0
 */
package Pieces;

import Main.Board;

import java.util.ArrayList;

public class Queen extends Piece {


    public Queen(int x, int y, boolean isWhite, Board board) {
        super(x, y, isWhite, board);
    }


    @Override
    public boolean isValidMove(int dx, int dy) {
//        if(!isCanMove(dx, dy))return false;
//        for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){
//            for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){
//                if(getBoard().getBoard()[i][j]!=null) return false;
//            }
//        }
        ArrayList<int[]> moves = moves();
        for (int m[] : moves){
            if(m[0]==dx && m[1] == dy){
                return true;
//                break;
            }
        }
        return false;
    }

    @Override
    public ArrayList<int[]> moves() {
        ArrayList<int[]> moves = new ArrayList<>();
        moves.clear();
        int tempx=getX()+1,tempy=getY()-1;
        while(tempx<8&&tempy>=0)
        {
            if(getBoard().isEmpty(tempx,tempy))
            {
                moves.add(new int[]{tempx,tempy});
            }
            else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
                break;
            else
            {
                moves.add(new int[]{tempx,tempy});
                break;
            }
            tempx++;
            tempy--;
        }
        tempx=getX()-1;tempy=getY()+1;
        while(tempx>=0&&tempy<8)
        {
            if(getBoard().isEmpty(tempx,tempy))
            {
                moves.add(new int[]{tempx,tempy});
            }
            else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
                break;
            else
            {
                moves.add(new int[]{tempx,tempy});
                break;
            }
            tempx--;
            tempy++;
        }
        tempx=getX()-1;tempy=getY()-1;
        while(tempx>=0&&tempy>=0)
        {
            if(getBoard().isEmpty(tempx,tempy))
            {
                moves.add(new int[]{tempx,tempy});
            }
            else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
                break;
            else
            {
                moves.add(new int[]{tempx,tempy});
                break;
            }
            tempx--;
            tempy--;
        }
        tempx=getX()+1;tempy=getY()+1;
        while(tempx<8&&tempy<8)
        {
            if(getBoard().isEmpty(tempx,tempy))
            {
                moves.add(new int[]{tempx,tempy});
            }
            else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
                break;
            else
            {
                moves.add(new int[]{tempx,tempy});
                break;
            }
            tempx++;
            tempy++;
        }

        int x = getX();
        int y = getY();
        tempx=x-1;
        while(tempx>=0)
        {
            if(getBoard().isEmpty(tempx,y))
                moves.add(new int[]{tempx,y});
            else if((getBoard().getColor(tempx,y)==1 && this.isWhite()) || (getBoard().getColor(tempx,y)==2 && this.isBlack()))
                break;
            else
            {
                moves.add(new int[]{tempx,y});
                break;
            }
            tempx--;
        }
        tempx=x+1;
        while(tempx<8)
        {
            if(getBoard().isEmpty(tempx,y))
                moves.add(new int[]{tempx,y});
            else if((getBoard().getColor(tempx,y)==1 && this.isWhite()) || (getBoard().getColor(tempx,y)==2 && this.isBlack()))
                break;
            else
            {
                moves.add(new int[]{tempx,y});
                break;
            }
            tempx++;
        }
        tempy=y-1;
        while(tempy>=0)
        {
            if(getBoard().isEmpty(x,tempy))
                moves.add(new int[]{x,tempy});
            else if((getBoard().getColor(x,tempy)==1 && this.isWhite()) || (getBoard().getColor(x,tempy)==2 && this.isBlack()))
                break;
            else
            {
                moves.add(new int[]{x,tempy});
                break;
            }
            tempy--;
        }
        tempy=y+1;
        while(tempy<8)
        {
            if(getBoard().isEmpty(x,tempy))
                moves.add(new int[]{x,tempy});
            else if((getBoard().getColor(x,tempy)==1 && this.isWhite()) || (getBoard().getColor(x,tempy)==2 && this.isBlack()))
                break;
            else
            {
                moves.add(new int[]{x,tempy});
                break;
            }
            tempy++;
        }
        return moves;
    }

    @Override
    public String toString() {
        return "Q";
    }
}