package cz.vutbr.feec.stockElements;

import ij.gui.GUI;

import java.util.Vector;

import cz.vutbr.feec.libraryClasses.GraphCreatingMethods;
import cz.vutbr.feec.libraryClasses.GuiMethods;
import cz.vutbr.feec.libraryClasses.GuiPainting;
import cz.vutbr.feec.libraryClasses.SearchingMethods;
import cz.vutbr.feec.project.GlobalParameters;
import cz.vutbr.feec.project.TestSet;
import cz.vutbr.feec.transportObjects.DrawnPathCrate;

public class routeInStockSearching {

	private AbstractStockCell start = null;
	private AbstractStockCell dst = null;
	private Vector<AbstractStockCell> cellsInPath = new Vector<AbstractStockCell>();
	private static Stock s = Stock.getInstance();

	public AbstractStockCell getStart() {
		return start;
	}

	public void setStart(CoridorCell start) {
		this.start = start;
	}

	public AbstractStockCell getDst() {
		return dst;
	}

	public void setDst(CoridorCell dst) {
		this.dst = dst;
	}

	public void add(AbstractStockCell c) {
		if (start == null) {
			start = c;
			// zobraz do canvasu
			GuiPainting.drawImage(GlobalParameters.startFlag,
					GuiMethods.getFieldFromIndexX(c.getX()),
					GuiMethods.getFieldFromIndexY(c.getY()));
		} else {
			if (dst == null) {
				GuiPainting.drawImage(GlobalParameters.dstFlag,
						GuiMethods.getFieldFromIndexX(c.getX()),
						GuiMethods.getFieldFromIndexY(c.getY()));
				dst = c;
				long startTime = System.nanoTime();

				// najdem cestu
				DrawnPathCrate drawnPath;
				drawnPath = SearchingMethods.findPathFromCells(start, dst);
				//long endTime = System.nanoTime();
				
			//	System.out.println("Took " + (endTime - startTime) + " ns");
				
				
				
				//pred vykreslenim sorting a check nehody
				SearchingMethods.sortAndAccidentCheck(drawnPath);
				long endTime = System.nanoTime();
				//vykreslim cestu
				GuiPainting.drawFoundPath(drawnPath);
				//System.out.println("computing time: "+(endTime - startTime)+ " ns");
				System.out.println(endTime - startTime);
				TestSet.casy.add((float) (endTime - startTime));
				
			/*	System.out.println(" --- vzdialenost z vyslednej cesty= "
						+ drawnPath.route.getCellsInPath().size()
						* s.getDelimiter() / GlobalParameters.meterPerSecond);
				
				*/
				
			} else {
				start = null;
				dst = null;
				GuiPainting.createZoomedCanvas();
			}
		}
		
	}

	public void clear() {
		start = null;
		dst = null;
		GuiPainting.createZoomedCanvas();
		// TODO Auto-generated method stub

	}

	public Vector<AbstractStockCell> getCellsInPath() {
		return cellsInPath;
	}

	public void setCellsInPath(Vector<AbstractStockCell> cellsInPath) {
		this.cellsInPath = cellsInPath;
	}

}
