package chess2;
import java.util.ArrayList;
public class MysteryPiece extends ChessPiece {

      public MysteryPiece(Side SIDE, int XPOS, int YPOS, ChessBoard YOURBOARD, ChessGame GAME){
        super(SIDE, XPOS, YPOS, YOURBOARD, GAME);
    }
    @Override
    public ArrayList<Decision> possibleDecisions(){
        ArrayList<Decision> possibleDecisions = new ArrayList();
        if (canMoveMysteriedTo(xPos+300, yPos+100)&&(!kingWouldBeInCheck(xPos+300, yPos+100))){
            if (yourBoard.level(xPos+300, yPos+100)<yourBoard.level(xPos, yPos)-1){
                possibleDecisions.add(new Decision(xPos+300, yPos+100, DeciType.SUICIDE));
            }
            else{
                possibleDecisions.add(new Decision(xPos+300, yPos+100));
            }
        }
        if (canMoveMysteriedTo(xPos+300, yPos-100)&&(!kingWouldBeInCheck(xPos+300, yPos-100))){
            if (yourBoard.level(xPos+300, yPos-100)<yourBoard.level(xPos, yPos)-1){
                possibleDecisions.add(new Decision(xPos+300, yPos-100, DeciType.SUICIDE));
            }
            else{
                possibleDecisions.add(new Decision(xPos+300, yPos-100));
            }
        }
         if (canMoveMysteriedTo(xPos-300, yPos+100)&&(!kingWouldBeInCheck(xPos-300, yPos+100))){
            if (yourBoard.level(xPos-300, yPos+100)<yourBoard.level(xPos, yPos)-1){
                possibleDecisions.add(new Decision(xPos-300, yPos+100, DeciType.SUICIDE));
            }
            else{
                possibleDecisions.add(new Decision(xPos-300, yPos+100));
            }
        }
        if (canMoveMysteriedTo(xPos-300, yPos-100)&&(!kingWouldBeInCheck(xPos-300, yPos-100))){
            if (yourBoard.level(xPos-300, yPos-100)<yourBoard.level(xPos, yPos)-1){
                possibleDecisions.add(new Decision(xPos-300, yPos-100, DeciType.SUICIDE));
            }
            else{
                possibleDecisions.add(new Decision(xPos-300, yPos-100));
            }
        }
        if (canMoveMysteriedTo(xPos+100, yPos+300)&&(!kingWouldBeInCheck(xPos+100, yPos+300))){
            if (yourBoard.level(xPos+100, yPos+300)<yourBoard.level(xPos, yPos)-1){
                possibleDecisions.add(new Decision(xPos+100, yPos+300, DeciType.SUICIDE));
            }
            else{
                possibleDecisions.add(new Decision(xPos+100, yPos+300));
            }
        }
        if (canMoveMysteriedTo(xPos-100, yPos+300)&&(!kingWouldBeInCheck(xPos-100, yPos+300))){
            if (yourBoard.level(xPos-100, yPos+300)<yourBoard.level(xPos, yPos)-1){
                possibleDecisions.add(new Decision(xPos-100, yPos+300, DeciType.SUICIDE));
            }
            else{
                possibleDecisions.add(new Decision(xPos-100, yPos+300));
            }
        }
        if (canMoveMysteriedTo(xPos+100, yPos-300)&&(!kingWouldBeInCheck(xPos+100, yPos-300))){
            if (yourBoard.level(xPos+100, yPos-300)<yourBoard.level(xPos, yPos)-1){
                possibleDecisions.add(new Decision(xPos+100, yPos-300, DeciType.SUICIDE));
            }
            else{
                possibleDecisions.add(new Decision(xPos+100, yPos-300));
            }
        }
        if (canMoveMysteriedTo(xPos-100, yPos-300)&&(!kingWouldBeInCheck(xPos-100, yPos-300))){
            if (yourBoard.level(xPos-100, yPos-300)<yourBoard.level(xPos, yPos)-1){
                possibleDecisions.add(new Decision(xPos-100, yPos-300, DeciType.SUICIDE));
            }
            else{
                possibleDecisions.add(new Decision(xPos-100, yPos-300));
            }
        }
        if (canAttackMysteriedTo(xPos+300, yPos+100)&&(!kingWouldBeInCheck(xPos+300, yPos+100))){
            possibleDecisions.add(new Decision(xPos+300, yPos+100, DeciType.ATTACKS));
        }
        if (canAttackMysteriedTo(xPos+300, yPos-100)&&(!kingWouldBeInCheck(xPos+300, yPos-100))){
            possibleDecisions.add(new Decision(xPos+300, yPos-100, DeciType.ATTACKS));
        }
         if (canAttackMysteriedTo(xPos-300, yPos+100)&&(!kingWouldBeInCheck(xPos-300, yPos+100))){
            possibleDecisions.add(new Decision(xPos-300, yPos+100, DeciType.ATTACKS));
        }
        if (canAttackMysteriedTo(xPos-300, yPos-100)&&(!kingWouldBeInCheck(xPos-300, yPos-100))){
            possibleDecisions.add(new Decision(xPos-300, yPos-100, DeciType.ATTACKS));
        }
        if (canAttackMysteriedTo(xPos+100, yPos+300)&&(!kingWouldBeInCheck(xPos+100, yPos+300))){
            possibleDecisions.add(new Decision(xPos+100, yPos+300, DeciType.ATTACKS));
        }
        if (canAttackMysteriedTo(xPos-100, yPos+300)&&(!kingWouldBeInCheck(xPos-100, yPos+300))){
            possibleDecisions.add(new Decision(xPos-100, yPos+300, DeciType.ATTACKS));
        }
        if (canAttackMysteriedTo(xPos+100, yPos-300)&&(!kingWouldBeInCheck(xPos+100, yPos-300))){
            possibleDecisions.add(new Decision(xPos+100, yPos-300, DeciType.ATTACKS));
        }
        if (canAttackMysteriedTo(xPos-100, yPos-300)&&(!kingWouldBeInCheck(xPos-100, yPos-300))){
            possibleDecisions.add(new Decision(xPos-100, yPos-300, DeciType.ATTACKS));
        }
        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;
    }
    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.getMysteryPiece(xPos, yPos).canMoveToMinusTank(XPOS, YPOS)){
            return true;
        }
        return false;
    }
    @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 canMoveToMinusChecking(int XPOS, int YPOS){
        if (canMoveMysteriedTo(XPOS, YPOS)){
            return true;
        }
         if (canEnterTankAt(XPOS, YPOS)){
            return true;
        }
        if (canAttackMysteriedTo(XPOS, YPOS)){
            return true;
        }
        return false;
    }
    public boolean canMoveToMinusTank(int XPOS, int YPOS){
        if (kingWouldBeInCheck(XPOS, YPOS)){
            return false;
        }
         if (canMoveMysteriedTo(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.mysteryPieces.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 (canAttackMysteriedTo(XPOS, YPOS)){
                yourBoard.kill(this, XPOS, YPOS);
            if (game==ChessGame.MONKEY||game==ChessGame.PEASANT){
                    yourBoard.corpses.add(new Corpse(Side.WALL, XPOS, YPOS, yourBoard, game));
                }
            }
           yourBoard.addWall(xPos, yPos);
            if (yourBoard.level(XPOS, YPOS)<yourBoard.level(xPos, yPos)-1){
                for (int i=0;i<yourBoard.mysteryPieces.size();i++){
                    if (yourBoard.mysteryPieces.get(i).xPos==xPos&&yourBoard.mysteryPieces.get(i).yPos==yPos){
                        yourBoard.mysteryPieces.remove(i);
                        break;
                    }
                }
            }
            else{
                if (!killedTank){
                        xPos=XPOS;
                        yPos=YPOS;
                    }
            }
    }
}