package pacman;

import java.util.*;

public class SimpleExtractor {

    // this needs to be modified in order to keep track of existing agents,
    // rather than making a new agent every time that consume() is called

    static int BG = 0;
    static int scoreColor = 14342911;
    static int powers = 0;

    int w, h;
    IntStack stack;
    HashSet uniques;
    // Agent agent;
    GameState gs;
    Board board;


    public SimpleExtractor(int w, int h, Board board) {
        this.w = w;
        this.h = h;
        int size = 4 * w * h;
        stack = new IntStack(size);
        uniques = new HashSet();
        // agent = new Agent();
        gs = new GameState();
        this.board=board;
    }

    public ArrayList<Drawable> consume(int[] pix, Set<Integer> colors) {
   //    HashSet set=new HashSet();
        ArrayList<Drawable> objects = new ArrayList<Drawable>();

        for (int p = 0; p < pix.length; p++) {
      //      set.add(pix[p]);
            if ((pix[p] & 0xFFFFFF) != BG && colors.contains(pix[p])) {
         //       System.out.println(p+"   "+pix[p]);
                ConnectedSet cs = consume(pix, p, pix[p]);
                objects.add(cs);
                for(int i=0;i<cs.list.size();i++){
                    fillBoard(cs.list.get(i).getI(),cs.list.get(i).getJ(),cs.type);
                }
                gs.update(cs, pix);
            }
        }
 /*       Iterator it=set.iterator();
        while(it.hasNext()){
            System.out.println("set: "+ it.next());
        }  */
        objects.add(gs);
        return objects;
    }


    public ConnectedSet consume(int[] pix, int p, int fg) {
        ConnectedSet cs = new ConnectedSet(p % w, p / w, fg,board);

// push the current pixel on the stack
        stack.reset();
// int p = x + y * w;
        stack.push(p);
// int count = 0;
// System.out.println( stack );
        while (!stack.isEmpty()) {
// count++;
            p = stack.pop();
            if (pix[p] == fg || (fg==MsPacInterface.wall && pix[p]==MsPacInterface.blinky)) {
// System.out.println(cx + " : " + cy + " : " + pix[p] );
// System.in.read();
                cs.add(p % w, p / w, p, pix[p]);
                pix[p] = 0;
                int cx = p % w;
                int cy = p / w;
                if (cx > 0) {
                    stack.push(p - 1);
                }
                if (cy > 0) {
                    stack.push(p - w);
                }
                if (cx < (w - 1)) {
                    stack.push(p + 1);
                }
                if (cy < (h - 1)) {
                    stack.push(p + w);
                }
            }
        }
        return cs;
    }

    public void fillBoard(int i,int j,int type){
        if(board.board[j/8][i/8]==0)
            board.board[j/8][i/8]=type;
    }
}

