package pacman;

import utilities.JEasyFrame;
import utilities.ElapsedTimer;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashSet;
//import stats.StatisticalSummary;
/**
 *  This is done with a OneD array, is to have offsets of +/- 1 and +/- width
 *  too close in any particular direction, then that would be detected
 */

public class MsPacInterface {

    static int delay =50;

    static int left = 505;             //500
    static int top = 350;              //300
    //static int left = 530;
    //static int top = 274;
   // public static int width = 223;
    //public static int height = 275;
    public static int width = 208;      //200
    public static int height = 231;     //300
    int[] pixels;
    Robot robot;
    SimpleExtractor ce;
    SimpleDisplay sd;

    static Board board=new Board();

    // these are the integers corresponding to the pixel colours for each game object
    /*static int blinky = -65536;
    static int pinky = -18689;
    static int inky = -16711681;
    static int sue = -18859;
    static int pacMan = -256;
    static int edible = -14408449;
    static int pill = -2434305;   */

    static int blinky = -65536;    //
    static int pinky = -18210;     //
    static int inky = -16711714;   //
    static int sue = -18361;       //
    static int pacMan = -256;       //
    static int edible = -14605858;
    static int pill = -2171170;     // jahat kharab mishe

    static int wall=-18281;

    static HashSet<Integer> colors = new HashSet<Integer>();
    static {
        colors.add(blinky);
        colors.add(pinky);
        colors.add(inky);
        colors.add(sue);
        colors.add(pacMan);
        colors.add(edible);
        colors.add(pill);

        colors.add(wall);
    }

    public MsPacInterface() throws Exception {
        robot = new Robot();
        pixels = new int[width * height];
        ce = new SimpleExtractor(width, height, board);
        sd = new SimpleDisplay(width, height);
        new JEasyFrame(sd, "Extracted", true);
    }

    public static void main(String[] args) throws Exception {

        MsPacInterface ms = new MsPacInterface();

        PacMover pm = new PacMover();
        DirectionComponent dc = DirectionComponent.easyUse();

        while(true) {
            ElapsedTimer t = new ElapsedTimer();

            for(int i=0;i<29;i++){
                for(int j=0;j<26;j++){
                    board.board[i][j]=0;
                }
            }

            int[] pix = ms.getPixels();
            ms.analyseComponents(pix);
          //  Board board=new Board(ce.ux,ce.dx,ce.uy,ce.dy);
            for(int i=0;i<29;i++){
                for(int j=0;j<26;j++){
                    if(i>1 && board.board[i-1][j]!=8)
                        board.neighs[i][j].add(new Pair(i-1,j));
                    if(i<28 && board.board[i+1][j]!=8)
                        board.neighs[i][j].add(new Pair(i+1,j));
                    if(j>1 && board.board[i][j-1]!=8)
                        board.neighs[i][j].add(new Pair(i,j-1));
                    if(j<25 && board.board[i][j+1]!=8)
                        board.neighs[i][j].add(new Pair(i,j+1));
                }
            }

            for(int i=0;i<29;i++){
                for(int j=0;j<26;j++){
                    System.out.print(board.board[i][j]+" ");
                }
                System.out.println();
            }
            System.out.println();

            int action = ms.ce.gs.agent.move(ms.ce.gs);
            pm.move(action);
            dc.update(action);        
            Thread.sleep(delay);

        }
    }

    public void analyseComponents(int[] pix) {
        ce.gs.reset();
        ArrayList<Drawable> al = ce.consume(pix, colors);

        sd.updateObjects(al);
    }

    public int[] getPixels() {
        BufferedImage im = robot.createScreenCapture(new Rectangle(left, top, width, height));
        im.getRGB(0, 0, width, height, pixels, 0, width);
        return pixels;
    }
}

