package Pieces;

import Main.Board;

import java.util.ArrayList;

public class Bishop extends Piece {

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

    @Override
    public boolean isCanMove(int dx, int dy) {
        if(isOutside(dx, dy)) return false;
        int deltaX = Math.abs(dx-getX());
        int deltaY = Math.abs(dy-getY());
        if( deltaX==deltaY) return true;
        return false;
    }

    @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++;
        }
        return moves;
    }

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