package cz.vutbr.feec.project;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import java.util.Vector;

import cz.vutbr.feec.graph.Path;
import cz.vutbr.feec.graph.Router;
import cz.vutbr.feec.libraryClasses.GraphCreatingMethods;
import cz.vutbr.feec.libraryClasses.SearchingMethods;
import cz.vutbr.feec.stockElements.CoridorCell;
import cz.vutbr.feec.stockElements.RouterCell;
import cz.vutbr.feec.stockElements.Stock;
import cz.vutbr.feec.stockElements.routeInStockSearching;

public class TestSet {
	public static Vector<Float> casy = new Vector<>();
	private final Stock s = Stock.getInstance();
	routeInStockSearching route = new routeInStockSearching();

	public TestSet() {
		//generateAccidents(7);
		testTwoCells(500);
		//testRandomCells(500);
	}

	public TestSet(int counter) {
		try {

			switch (counter) {
			case 0:
				testSamePath(500);
				break;
			case 1:
				testTwoRouters(500);
				break;
			case 2:
				testTwoCells(500);
				break;
			case 3:
				testOneSameRouter(500);
				break;
			case 4:
				testRouterAndCell(500);
				break;
			default:
				System.out.println("Koniec testov");
				break;
			}
		} catch (Exception e) {
			System.out.println("chyba v testoch");
			System.out.println(e.getMessage());

		}

	}

	public void testSamePath(int max) {
		System.out.println("rovnaka cesta");

		int size = s.getPaths().size();
		int counter = 0;
		while (counter != max) {
			for (int i = 0; i < size; i++) {
				int cellSize = s.getPaths().get(i).getPath().size();
				if (cellSize < 2) {
					continue;
				} else {
					int repeat = cellSize * (cellSize - 1);
					for (int j = 0; j < repeat; j++) {
						Random rand = new Random();
						int rand1 = rand.nextInt(cellSize);
						int rand2 = rand.nextInt(cellSize);
						CoridorCell start = (CoridorCell) s.getPaths().get(i)
								.getPath().get(rand1);// random
						CoridorCell dst = (CoridorCell) s.getPaths().get(i)
								.getPath().get(rand2);// random
						if (rand1 != rand2) {
							// SearchingMethods.findPathFromCells(start, dst);
							route.add(start);
							route.add(dst);
							counter++;
							route.add(start);
							if (counter == max)
								return;
						}
					}
				}
			}
		}
	}

	public void testTwoRouters(int max) {
		System.out.println("dva routre");
		int size = s.getRouters().size();
		int counter = 0;
		while (counter != max) {
			Random rand = new Random();
			int rand1 = rand.nextInt(size);
			int rand2 = rand.nextInt(size);
			Router startRouter = s.getRouters().get(rand1);// random
			Router dstRouter = s.getRouters().get(rand2);// random
			RouterCell start = (RouterCell) s.getFieldIndex(
					startRouter.getxInField(), startRouter.getyInField());
			RouterCell dst = (RouterCell) s.getFieldIndex(
					dstRouter.getxInField(), dstRouter.getyInField());
			if (rand1 != rand2) {
				// SearchingMethods.findPathFromCells(start, dst);
				route.add(start);
				route.add(dst);
				counter++;
				route.add(start);
				if (counter == max)
					return;
			}
		}
	}

	public void testTwoCells(int max) {
		System.out.println("dve bunky");
		CoridorCell[] cells = (CoridorCell[]) GlobalParameters.allCoridorCells
				.toArray(new CoridorCell[GlobalParameters.allCoridorCells
						.size()]);

		int size = cells.length;
		System.out.println("size of field= " + size);
		int counter = 0;
		while (counter != max) {
			Random rand = new Random();
			int rand1 = rand.nextInt(size);
			int rand2 = rand.nextInt(size);
			CoridorCell start = cells[rand1];// random
			CoridorCell dst = cells[rand2];// random
			if (rand1 != rand2) {
				// nesmu byt na rovnakej ceste
				int path1 = GraphCreatingMethods.getPathNumberByClick(
						start.getX(), start.getY());
				int path2 = GraphCreatingMethods.getPathNumberByClick(
						dst.getX(), dst.getY());
				if (path1 != path2) {
					// nesmu mat ani spolocny router
					try {
					Path p1 = s.getPaths().get(path1);
					Path p2 = s.getPaths().get(path2);
					if (p1.getFirstRouter() != p2.getFirstRouter()
							&& p1.getFirstRouter() != p2.getSecondRouter()
							&& p1.getSecondRouter() != p2.getFirstRouter()
							&& p1.getSecondRouter() != p2.getSecondRouter()) {
						route.add(start);
						route.add(dst);
						counter++;
						route.add(start);
						if (counter == max)
							return;
					}
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
					

			}
		}
	}

	public void testOneSameRouter(int max) {
		System.out.println("jeden spolocny router");
		CoridorCell[] cells = (CoridorCell[]) GlobalParameters.allCoridorCells
				.toArray(new CoridorCell[GlobalParameters.allCoridorCells
						.size()]);

		int size = cells.length;
		System.out.println("size of field= " + size);
		int counter = 0;
		while (counter != max) {
			Random rand = new Random();
			int rand1 = rand.nextInt(size);
			int rand2 = rand.nextInt(size);
			CoridorCell start = cells[rand1];// random
			CoridorCell dst = cells[rand2];// random
			if (rand1 != rand2) {
				// nesmu byt na rovnakej ceste
				int path1 = GraphCreatingMethods.getPathNumberByClick(
						start.getX(), start.getY());
				int path2 = GraphCreatingMethods.getPathNumberByClick(
						dst.getX(), dst.getY());
				if (path1 != path2) {
					Path p1 = s.getPaths().get(path1);
					Path p2 = s.getPaths().get(path2);
					if (p1.getFirstRouter() == p2.getFirstRouter()
							| p1.getFirstRouter() == p2.getSecondRouter()
							|| p1.getSecondRouter() == p2.getFirstRouter()
							|| p1.getSecondRouter() == p2.getSecondRouter()) {
						route.add(start);
						route.add(dst);
						counter++;
						route.add(start);
						if (counter == max)
							return;
					}
				}

			}
		}
	}

	public void testRouterAndCell(int max) {
		System.out.println("bunka a router");
		int sizeRouters = s.getRouters().size();
		CoridorCell[] cells = (CoridorCell[]) GlobalParameters.allCoridorCells
				.toArray(new CoridorCell[GlobalParameters.allCoridorCells
						.size()]);

		int sizeCells = cells.length;
		int counter = 0;
		while (counter != max) {
			Random rand = new Random();
			int randRouter = rand.nextInt(sizeRouters);
			int randCell = rand.nextInt(sizeCells);
			Router startRouter = s.getRouters().get(randRouter);// random
			RouterCell start = (RouterCell) s.getFieldIndex(
					startRouter.getxInField(), startRouter.getyInField());
			CoridorCell dst = cells[randCell];// random
			if (counter > max / 2) {
				route.add(dst);
				route.add(start);
				counter++;
				route.add(start);
				if (counter == max)
					return;
			} else {
				route.add(start);
				route.add(dst);
				counter++;
				route.add(start);
				if (counter == max)
					return;
			}
		}
	}

	public void testRandomCells(int max) {
		System.out.println("randomBunky");
		CoridorCell[] cells = (CoridorCell[]) GlobalParameters.allCoridorCells
				.toArray(new CoridorCell[GlobalParameters.allCoridorCells
						.size()]);

		int size = cells.length;
		System.out.println("size of field= " + size);
		int counter = 0;
		while (counter != max) {
			Random rand = new Random();
			int rand1 = rand.nextInt(size);
			int rand2 = rand.nextInt(size);
			CoridorCell start = cells[rand1];// random
			CoridorCell dst = cells[rand2];// random
			if (rand1 != rand2) {

				route.add(start);
				route.add(dst);
				counter++;
				route.add(start);
				if (counter == max)
					return;

			}
		}
	}

	public static void printResult() {
		float avg = 0;
		float max = 0;
		float min = 999999999;
		for (int i = 0; i < casy.size(); i++) {
			avg = avg + casy.get(i);
			if (casy.get(i) > max)
				max = casy.get(i);
			if (casy.get(i) < min)
				min = casy.get(i);
		}
		avg = avg / casy.size();
		float median;
		Collections.sort(casy);
		int middle = casy.size() / 2;
		if (casy.size() % 2 == 1) {
			median = casy.get(middle);
		} else {
			median = (float) ((casy.get(middle - 1) + casy.get(middle)) / 2.0);
		}
		System.out.println("avg = "+avg);
		System.out.println("median = "+median);
		System.out.println("max = "+max);
		System.out.println("min = "+min);
		}
	public void generateAccidents(int number){
		number ++;
		CoridorCell[] cells = (CoridorCell[]) GlobalParameters.allCoridorCells
				.toArray(new CoridorCell[GlobalParameters.allCoridorCells
						.size()]);

		int size = cells.length;
		System.out.println("size of field= " + size);
		int counter = 0;
		while (counter != number) {
			Random rand = new Random();
			int rand1 = rand.nextInt(size);
			
			CoridorCell start = cells[rand1];// random
			
				counter++;
				s.getAccidentCells().add(start);
				if (counter == number)
					return;

			}
		}
		
	}


