package chess2;
import java.util.ArrayList;
public class Bishop extends ChessPiece {
    public Bishop(Side SIDE, int XPOS, int YPOS, ChessBoard YOURBOARD, ChessGame GAME){
        super(SIDE, XPOS, YPOS, YOURBOARD, GAME);
    }
    public boolean canEnterTankAt(int XPOS, int YPOS){
        if (side==ChessApplet.yourSide&&!ChessApplet.entertank){
            return false;
        }
        if (yourBoard.getTank(XPOS, YPOS)==null){
            return false;
        }
        if (yourBoard.pieceOccupyingSquare(XPOS, YPOS)!=Piece.TANK){
            return false;
        }
        ChessBoard simulaBoard = new ChessBoard (yourBoard);
        Tank targetTank = simulaBoard.getTank(XPOS, YPOS);
        simulaBoard.clearTank(targetTank);
        simulaBoard.tanks.remove(targetTank);
        if (simulaBoard.getBishop(xPos, yPos).canMoveToMinusTank(XPOS, YPOS)){
            return true;
        }
        return false;
    }
    @Override
    public ArrayList<Decision> possibleDecisions(){
        ArrayList<Decision> possibleDecisions = new ArrayList();
        int XPOS = xPos+100;
        int YPOS = yPos+100;
        while(inRangeOfBoard(XPOS, YPOS)){
            if (canMoveDiagonallyTo(XPOS, YPOS)&&(!kingWouldBeInCheck(XPOS, YPOS))){
                if (yourBoard.level(XPOS, YPOS)<yourBoard.level(XPOS-100, YPOS-100)-1){
                    possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.SUICIDE));
                }
                else{
                    possibleDecisions.add(new Decision(XPOS, YPOS));
                }
            }
            else if (canEnterTankAt(XPOS, YPOS)){
                possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.ENTERS));
            }
            else if (canAttackDiagonallyTo(XPOS, YPOS)&&(!kingWouldBeInCheck(XPOS, YPOS))){
                possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.ATTACKS));
                break;
            }

            XPOS+=100;
            YPOS+=100;
        }
        XPOS = xPos+100;
        YPOS = yPos-100;
        while(inRangeOfBoard(XPOS, YPOS)){
            if (canMoveDiagonallyTo(XPOS, YPOS)&&(!kingWouldBeInCheck(XPOS, YPOS))){
                if (yourBoard.level(XPOS, YPOS)<yourBoard.level(XPOS-100, YPOS+100)-1){
                    possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.SUICIDE));
                }
                else{
                    possibleDecisions.add(new Decision(XPOS, YPOS));
                }
            }
            else if (canEnterTankAt(XPOS, YPOS)){
                possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.ENTERS));
            }
            else if (canAttackDiagonallyTo(XPOS, YPOS)&&(!kingWouldBeInCheck(XPOS, YPOS))){
                possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.ATTACKS));
                break;
            }

            XPOS+=100;
            YPOS-=100;
        }
        XPOS = xPos-100;
        YPOS = yPos+100;
        while(inRangeOfBoard(XPOS, YPOS)){
            if (canMoveDiagonallyTo(XPOS, YPOS)&&(!kingWouldBeInCheck(XPOS, YPOS))){
                possibleDecisions.add(new Decision(XPOS, YPOS));
            }
            else if (canEnterTankAt(XPOS, YPOS)){
                possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.ENTERS));
            }
            else if (canAttackDiagonallyTo(XPOS, YPOS)&&(!kingWouldBeInCheck(XPOS, YPOS))){
                possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.ATTACKS));
                break;
            }

            XPOS-=100;
            YPOS+=100;
        }
        XPOS = xPos-100;
        YPOS = yPos-100;
        while(inRangeOfBoard(XPOS, YPOS)){
            if (canMoveDiagonallyTo(XPOS, YPOS)&&(!kingWouldBeInCheck(XPOS, YPOS))){
                if (yourBoard.level(XPOS, YPOS)<yourBoard.level(XPOS+100, YPOS+100)-1){
                    possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.SUICIDE));
                }
                else{
                    possibleDecisions.add(new Decision(XPOS, YPOS));
                }
            }
            else if (canEnterTankAt(XPOS, YPOS)){
                possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.ENTERS));
            }
            else if (canAttackDiagonallyTo(XPOS, YPOS)&&(!kingWouldBeInCheck(XPOS, YPOS))){
                possibleDecisions.add(new Decision(XPOS, YPOS, DeciType.ATTACKS));
                break;
            }

            XPOS-=100;
            YPOS-=100;
        }

        int [] xPosits2 = {xPos, xPos, xPos+100, xPos-100, xPos+100, xPos+100, xPos-100, xPos-100};
        int [] yPosits2 = {yPos+100, yPos-100, yPos, yPos, yPos+100, yPos-100, yPos+100, yPos-100};

       for (int n=0;n<8;n++){
           if (canMarry(xPosits2[n], yPosits2[n])){
               possibleDecisions.add(new Decision(xPosits2[n], yPosits2[n], DeciType.WEDS));
           }
       }

        return possibleDecisions;
    }
    @Override
    public boolean canMoveTo(int XPOS, int YPOS){
        ArrayList<Decision> possibleDecisions = possibleDecisions();
        if (possibleDecisions.size()==0){
            return false;
        }
        for (int i=0;i<possibleDecisions.size();i++){
            if (possibleDecisions.get(i).xPos==XPOS&&possibleDecisions.get(i).yPos==YPOS){
                break;
            }
            if (i==possibleDecisions.size()-1){
                return false;
            }
        }
        if (kingWouldBeInCheck(XPOS, YPOS)){
            return false;
        }
        return canMoveToMinusChecking(XPOS, YPOS);
    }
    public boolean canMoveToMinusTank(int XPOS, int YPOS){
        if (kingWouldBeInCheck(XPOS, YPOS)){
            return false;
        }
        if (canMoveDiagonallyTo(XPOS, YPOS)){
            return true;
        }
        return false;
    }
    public boolean canMoveToMinusChecking(int XPOS, int YPOS){
        if (canMoveDiagonallyTo(XPOS, YPOS)){
            return true;
        }
        if (canAttackDiagonallyTo(XPOS, YPOS)){
            return true;
        }
        if (canEnterTankAt(XPOS, YPOS)){
            return true;
        }
        return false;
    }
    public void controlMind(){
        if (numberOfChecks()>=3){
            if (side==Side.WHITE){
                side=Side.BLACK;
                opponentSide = Side.WHITE;
            }
            else{
                side = side.WHITE;
                opponentSide = Side.BLACK;
            }
        }
        if (numberOfChecks()>=3){
            yourBoard.bishops.remove(this);
        }
    }
    public void move(int XPOS, int YPOS){
            boolean killedTank = false;
            if (yourBoard.sideOccupyingSquare(XPOS, YPOS)==opponentSide&&yourBoard.pieceOccupyingSquare(XPOS, YPOS)==Piece.TANK){
                killedTank = true;
            }
            if (ChessApplet.wed&&canMarry(XPOS, YPOS)){
                 yourBoard.marry(XPOS, YPOS, side);
                 return;
             }
            if (canAttackDiagonallyTo(XPOS, YPOS)){
                yourBoard.kill(this, XPOS, YPOS);
            }
            yourBoard.addWall(xPos, yPos);
            if (yourBoard.level(XPOS, YPOS)<yourBoard.level(xPos, yPos)-1){
                for (int i=0;i<yourBoard.bishops.size();i++){
                    if (yourBoard.bishops.get(i).xPos==xPos&&yourBoard.bishops.get(i).yPos==yPos){
                        yourBoard.bishops.remove(i);
                        break;
                    }
                }
            }
            else{
                if (inDisco(XPOS, YPOS)&&!inDisco(xPos, yPos)){yourBoard.wildCards.add(new WildCard(side, XPOS, YPOS, yourBoard, game, Piece.BISHOP));yourBoard.bishops.remove(this);}
                else{
                    if (!killedTank){
                        xPos=XPOS;
                        yPos=YPOS;
                        if (game==ChessGame.ADULTERY){
                            if (yourBoard.blackKing!=null&&xPos==yourBoard.blackKing.xPos&&yPos==yourBoard.blackKing.yPos){
                                yourBoard.bishops.remove(this);
                            }
                            if (yourBoard.whiteKing!=null&&xPos==yourBoard.whiteKing.xPos&&yPos==yourBoard.whiteKing.yPos){
                                yourBoard.bishops.remove(this);
                            }
                            for (int i=0;i<yourBoard.queens.size();i++){
                                if (xPos==yourBoard.queens.get(i).xPos&&yPos==yourBoard.queens.get(i).yPos){
                                    yourBoard.bishops.remove(this);
                                }
                            }
                        }
                    }
                }
            }
       }

}
