package Objects;

import java.util.HashSet;
import java.util.Vector;

import org.opencv.core.Point;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import project.Controller;


public class DetectionAPP {

	private static Vector<DetectionLine> lines = new Vector<>();
	private static Vector<DetectionArea> areas = new Vector<>();
	private static Point[][] platno = new Point[640][480];
	public Controller controller;
    public static GraphicsContext gc;
    WritableImage canvasImage;
    SnapshotParameters parameter;
    public static ImageView imageView;
    

	public DetectionAPP(){
		for (int i = 0; i < 640; i++) {
			for (int j = 0; j < 480; j++) {
				platno[i][j] = new Point(i,j);
			}
			
		}
	}
	public static void addLine(DetectionLine dl) {
		lines.add(dl);
	}
	
	public Vector<DetectionLine> getLines() {
		return lines;
	}

	public void setLines(Vector<DetectionLine> lines) {
		this.lines = lines;
	}
	
	public static void addArea(DetectionArea da){
		areas.add(0, da);
		
	}

	public Vector<DetectionArea> getAreas() {
		return areas;
	}

	public void setAreas(Vector<DetectionArea> da) {
		this.areas = da;
	}
	



	
	
	public HashSet<Point> vytvorOblast(Point clickVoblasti) {
        HashSet<Point> open = new HashSet<Point>();
//        LinkedList<Point> tmp = new LinkedList<Point>(); 
        HashSet<Point> closed = new HashSet<Point>();
        //open.addFirst(clickVoblasti);
        Point p = platno[(int) clickVoblasti.x][(int) clickVoblasti.y];
        open.add(clickVoblasti);
//        tmp.add(clickVoblasti);
//        closed.add(clickVoblasti);

//        Point novyBod = new Point();
        Vector<Point> sousedniBody = new Vector<Point>();
        parameter = new SnapshotParameters();
        //parameter.setTransform(new Translate(0, 0));   		
		canvasImage = gc.getCanvas().snapshot(parameter, null);
		//BLUE = 0x0000ffff
		//RED = 0xff0000ff
		Color red = Color.web("0xff0000ff");
		Color pixel;
        while (!open.isEmpty()) {
        				
                        //Point tmpPoint = open.removeFirst();
                        Point tmpPoint = open.iterator().next();
                        open.remove(tmpPoint);
                        closed.add(tmpPoint);
        	
        	
        		sousedniBody.clear();
        			
				if (tmpPoint.x+1<640) {
				sousedniBody.add(platno[(int) (tmpPoint.x+1)][(int) tmpPoint.y]);
				}
				if (tmpPoint.y+1<480) {
        			sousedniBody.add(platno[(int) (tmpPoint.x)][(int) tmpPoint.y+1]);
				}
				if (tmpPoint.x-1>0) {
				sousedniBody.add(platno[(int) (tmpPoint.x-1)][(int) tmpPoint.y]);
				}
				if (tmpPoint.y-1>0) {
					sousedniBody.add(platno[(int) (tmpPoint.x)][(int) tmpPoint.y-1]);
				}
        		
					
        		for (int j = 0; j < sousedniBody.size(); j++) {
        			pixel = canvasImage.getPixelReader().getColor((int) sousedniBody.get(j).x,(int) sousedniBody.get(j).y);
        			
        			if (!closed.contains(sousedniBody.get(j)) && pixel.getRed() != red.getRed()) {
            			open.add(sousedniBody.get(j));

            			
    				}
        		}
        		
        }


        controller.textArea.appendText("Size of the area" + " " + closed.size() + " pixels" + "\n");
        	return closed;
}


}