package pacman;

import java.util.ArrayList;
import java.awt.*;

public class ConnectedSet implements Drawable {
    // private ArrayList pixels;
    int x, y;
    int width, height;
    int fg; // the value of the FG pixels
    int xMin, xMax, yMin, yMax;
    int pTot;
    int tot;
    Color c;
    public int px, py;
    boolean valid = false;
    public ArrayList<Pair> list;

    int type;
    Board board;

 /*
     BLINKY = 1
     PINKY = 2
     INKY = 3
     SUE = 4
     EDIBLE = 5
     PILL = 6
     POWER-PILL = 7
     WALL = 8

*/

    static int upx=0,downx=10000,upy=0,downy=10000;

    public void draw(Graphics g, int w, int h) {
        // int div = 1;
        validate();
        g.setColor(c);
        if (ghostLike()) {

            int a;
            if(fg==MsPacInterface.blinky){
                a=1;
                type=1;
            }
            else if(fg==MsPacInterface.pinky){
                a=2;
                type=2;
            }
            else if(fg==MsPacInterface.inky){
                a=3;
                type=3;
            }
            else if(fg==MsPacInterface.sue){
                a=4;
                type=4;
            }
            else
                a=0;
            if(board.board[yMin/8][xMin/8]==0)
            board.board[yMin/8][xMin/8]=a;

            g.fillRect(xMin, yMin, width, height);
            // System.out.println(width + " : " + height);
        }
        else if (edibleGhost()) {
            if(board.board[yMin/8][xMin/8]==0)
            board.board[yMin/8][xMin/8]=5;
            type=5;
            g.fillRect(xMin, yMin, width, height);
            // System.out.println(width + " : " + height);
        }
        else if(isWall()){
            if(board.board[yMin/8][xMin/8]==0)
            board.board[yMin/8][xMin/8]=8;
            type=8;
           g.fillRect(xMin, yMin, width, height);
        }
        else {

            if (powerPill() || pill() ) {
                g.drawRect(xMin, yMin, width+1, height+1);

                if(powerPill()){
                    if(board.board[yMin/8+1][xMin/8]==0 || board.board[yMin/8+1][xMin/8]==6)
                    board.board[yMin/8+1][xMin/8]=7;
                    type=7;
                }
                
                if(pill()){
                    if(board.board[yMin/8][xMin/8]==0)
                    board.board[yMin/8][xMin/8]=6;
                    type=6;
                }
            }

            else
                board.board[yMin/8][xMin/8]=0;


        }


        // g.fillRect(x, y, 1, 1);
    }

    public void validate() {
        if (!valid) {
            width = xMax - xMin;
            height = yMax - yMin;
            // px = xMin + (xMax - xMin) / div;
            // py = yMin + (yMax - yMin) / div;
            valid = true;
        }
    }

    public boolean ghostLike() {
        validate();
        return ghostColor(fg) && width >= 10 && height >= 10  && width<50;
        // return width == 13 && height == 13; // fg == MsPacInterface.inky;
    }

    public boolean ghostColor(int c) {
        return c == MsPacInterface.blinky ||
                c == MsPacInterface.pinky ||
                c == MsPacInterface.inky ||
                c == MsPacInterface.sue;
    }

    public boolean edibleGhost() {
        validate();
        return fg==MsPacInterface.edible && width >= 10 && height >= 10;

    }

    public boolean isWall() {
        validate();
        return fg==MsPacInterface.wall; //&& width >= 10 && height >= 10;

    }

    public boolean isPacMan() {
        validate();
        return fg == MsPacInterface.pacMan && width >= 10 && height >= 10;
        // return width == 13 && height == 13; // fg == MsPacInterface.inky;
    }

    public boolean pill() {
        validate();
        return width == 1 && height == 1 && fg == MsPacInterface.pill; // between(width, 2, 3) && between(height, 2, 3);
    }

    public boolean powerPill() {
        validate();
        return width == 7 && height == 7; // between(width, 2, 3) && between(height, 2, 3);
    }

    public static boolean between(int x, int low, int high) {
        return x >= low && x <= high;
    }

    public int x() {
        return xMin + (xMax - xMin) / 2;
    }

    public int y() {
        return yMin + (yMax - yMin) / 2;
    }

    public void rescale(double fac) {
        // do nothing
    }

    public ConnectedSet(int x, int y, int fg) {
        // pixels = new ArrayList();
        this.x = x;
        this.y = y;
        xMin = x;
        xMax = x;
        yMin = y;
        yMax = y;
        this.fg = fg;
        c = new Color((fg & 0xFF0000) >> 16, (fg & 0xFF00) >> 8, (fg & 0xFF));
        list=new ArrayList<Pair>();
        list.add(new Pair(x,y));
        type=0;
        // System.out.println("Color: " + c + " : " + fg);
    }

 //MINE
    public ConnectedSet(int x, int y, int fg, Board board) {
        // pixels = new ArrayList();
        this.x = x;
        this.y = y;
        xMin = x;
        xMax = x;
        yMin = y;
        yMax = y;
        this.fg = fg;
        c = new Color((fg & 0xFF0000) >> 16, (fg & 0xFF00) >> 8, (fg & 0xFF));

        this.board=board;
        list=new ArrayList<Pair>();
   //     list.add(new Pair(x,y));
        type=0;
        // set type
            if (ghostLike()) {
                if(fg==MsPacInterface.blinky){
                    type=1;
                }
                else if(fg==MsPacInterface.pinky){
                    type=2;
                }
                else if(fg==MsPacInterface.inky){
                    type=3;
                }
                else if(fg==MsPacInterface.sue){
                    type=4;
                }
            }
            else if (edibleGhost()) {
                type=5;
            }
            else if(isWall()){
                type=8;
              }
            else {
                    if(powerPill()){
                        type=7;
                    }

                    if(pill()){
                         type=6;
                    }
                }

    }

    public void add(int px, int py, int pos, int val) {

        list.add(new Pair(px,py));
       // System.out.println(px+" "+py);
       // this.print();   BAD!!!

        xMin = Math.min(px, xMin);
        xMax = Math.max(px, xMax);
        yMin = Math.min(py, yMin);
        yMax = Math.max(py, yMax);
        pTot += (1 + px - x) * (1 + py - y);
        tot++;
        valid = false;
    }

    public int hashCode() {
        return pTot;
    }

    public boolean equals(Object obj) {
        ConnectedSet cs = (ConnectedSet) obj;
        return cs.pTot == pTot;
    }

    public String toString() {
        return x + " : " + y + " : " + pTot;
    }

    public int dist(Position p) {
        return (int) Math.sqrt(sqr(x - p.x) + sqr(y - p.y));
    }

    public static int sqr(int x) {
        return x * x;
    }

    public void print(){
        for(int i=0;i<list.size();i++){
            list.get(i).print();
        }
    }
}

