package cz.vutbr.feec.gui;

import java.util.Vector;

import cz.vutbr.feec.project.GlobalParameters;
import cz.vutbr.feec.transportObjects.PositionCrate;
import cz.vutbr.feec.transportObjects.ReckCrate;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class ReckCanvas {
	private Canvas canvas;
	private boolean isHorizontal;
	private Vector<ReckInCanvas> recks = new Vector<ReckInCanvas>();
	private int width = 120;
	private int height = 100;
	private int imageSize = 25;

	public int getReckDelimiterSize() {
		return imageSize;
	}

	private ReckInCanvas selectedReck;

	public ReckInCanvas getSelectedReck() {
		return selectedReck;
	}

	public void setSelectedReck(ReckInCanvas selectedReck) {
		this.selectedReck = selectedReck;
	}

	public ReckCanvas(Canvas canvas) {
		this.canvas = canvas;
		createInitialRecks();
		paintCanvas();
	}

	public Canvas getCanvas() {
		return canvas;
	}

	public void addReck(ReckInCanvas reck) {
		recks.add(reck);
	}

	public void removeReck(ReckCrate reck) {
		// regale ktore sa budu posuvat od vymazaneho regalu o poziciu naspat
		Vector<ReckInCanvas> recksForMove = new Vector<ReckInCanvas>();
		// vyhladam regal, ktory sa ma vymazat
		int size = recks.size();
		for (int i = 0; i < size; i++) {
			ReckCrate current = new ReckCrate(recks.get(i));
			if (current.posX == reck.posX && current.posY == reck.posY) {
				//pokial je to prvy regal, tak vraciam, ten nechavam
				if(i==0) return;
				System.out.println("removing number " + i);
				// potrebujem zmensit canvas, neviem ci to je najvacsi alebo aky
				// regal ktory sa maze, tak zmensim canvas na nejaku random
				// hodnotu a pri vykresleni
				// sa nastavi znova tak aby sa zmestili vsetky regale
				width = 120;
				height = 80;
				// na poziciu mazaneho regalu potrebujem posunut dalsie regale
				// preto davam do vektoru vsetky ostatne regale
				for (int j = i + 1; j < size; j++) {
					recksForMove.add(recks.get(j));
				}
				try {

					// zacyklim sa
					while (true) {
						// este potreba vymazat regale
						recks.remove(i);

					}
				} catch (Exception e) {
					System.out.println("haha");
				}
				break;
			}

		}
		System.out.println("size of recks " + recks.size());
		System.out.println("size of recksForMove " + recksForMove.size());
		// tu je prvok vymazany a mam vektor regalov ktore treba posunut

		for (int i = 0; i < recksForMove.size(); i++) {
			// najdem volnu poziciu
			PositionCrate position = GlobalParameters.reckCanvas
					.getFreePosition();
			// prepisem poziciu
			recksForMove.get(i).setPositionX(position.x);
			recksForMove.get(i).setPositionY(position.y);
			// a znova pridam do vektora, z ktoreho sa to vykresluje
			recks.add(recksForMove.get(i));
		}

	}

	public Vector<ReckInCanvas> getRecks() {
		return recks;
	}

	public void paintCanvas() {
		canvas.setWidth(width);
		canvas.setHeight(height);
		GraphicsContext gc = canvas.getGraphicsContext2D();
		gc.setFill(Color.WHITESMOKE);
		gc.fillRect(0, 0, width, height);
		for (int i = 0; i < recks.size(); i++) {
			ReckCrate reck = new ReckCrate(recks.get(i));
			paintReck(reck, gc);
		}

	}

	private void createInitialRecks() {
		ReckInCanvas reck1 = new ReckInCanvas(10, 10, 1, 1, this);
		ReckInCanvas reck2 = new ReckInCanvas(45, 10, 1, 2, this);
		ReckInCanvas reck3 = new ReckInCanvas(80, 10, 1, 3, this);
		addReck(reck1);
		addReck(reck2);
		addReck(reck3);
	}

	private void paintReck(ReckCrate reck, GraphicsContext gc) {
		int x = reck.sizeX * imageSize;
		int y = reck.sizeY * imageSize;
		int xCycle = x / imageSize;
		int yCycle = y / imageSize;
		int posX = reck.posX;
		int posY = reck.posY;
		// musim ceknut, ci sa zmesti do canvasu, ak nie, tak musim prisposobit
		// canvas
		if (x + posX > width) {
			width = width + x + posX + 10;
			paintCanvas();
		}
		// + 10 je tam kvoli odsadeniu od ostatnych
		if (y + posY > height) {
			height = height + y + posY + 10;
			paintCanvas();
		}

		for (int i = 0; i < xCycle; i++) {
			posY = reck.posY;
			if (i > 0) {
				posX = posX + imageSize;
			}
			for (int j = 0; j < yCycle; j++) {

				if (j > 0) {
					posY = posY + imageSize;
				}

				gc.drawImage(GlobalParameters.reckImage, posX, posY, imageSize,
						imageSize);
				// pokial je niektory reck vybrany vykreslim okolo neho cervene
				// ciary
				if (reck.reck == this.selectedReck) {
					gc.setLineWidth(1);
					gc.setStroke(Color.RED);
					gc.strokeRect(posX - 1, posY - 1, imageSize + 2,
							imageSize + 2);
				}

			}
		}
	}

	public boolean isHorizontal() {
		return isHorizontal;
	}

	public void setHorizontal(boolean isHorizontal) {
		this.isHorizontal = isHorizontal;
	}

	public PositionCrate getFreePosition() {
		// prepravka je tu v pripade, ze by sa v buducnu pridavalo nie len do
		// riadkov, ale aj do stlpcov
		PositionCrate pc = new PositionCrate();
		// budem pridavat iba do riadku, takze potrebujem poziciu X posledneho
		// regalu
		int last = recks.size() - 1;
		int freeX;
		// zoberem posledny regal ktory je na palete
		ReckCrate reck = new ReckCrate(recks.get(last));
		// mam jeho poziciu X kde je vykresleny a velkost
		freeX = reck.posX + (reck.sizeX * imageSize) + 10;
		// 10 na odsadenie
		pc.x = freeX;
		pc.y = 10;

		return pc;
	}
}
