import java.io.File;
import java.net.MalformedURLException;
import javafx.fxml.FXML;
import java.awt.Button;
import java.awt.TextField;

import javafx.event.ActionEvent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.application.Application;
import javafx.stage.Stage;


public class GUI {
	
	// CONSTRUCTOR (for nothing for now)
	public GUI () {
	}
	
	// SET VARIABLES
	 Image image;
	 Loader loader;
	 XMLSaver saver;
	 Stage stage;
	 String path = "D:\\Dokumenty\\Obrázky\\hraju_larpy_fotky.jpg";
	 String imgpath;
	 String histogram = "";
	 String dominantColor = "";
	 String fileStreamXML = "";
	 String StreamBegin = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n<Mpeg7 xmlns = \"http://www.mpeg7.org/2001/MPEG-7_Schema\" xmlns:xsi = \"http://www.w3.org/2000/10/XMLSchema-instance\">\n";
	
	 //LOAD IMAGE FROM DISK
	public void openFile(ActionEvent event) throws MalformedURLException {
		FileChooser fileChooser = new FileChooser();
		//fileChooser.setInitialDirectory("D:\Dokumenty");
		fileChooser.setInitialDirectory(new File(System.getProperty("user.home") + System.getProperty("file.separator") + "Pictures"));
		File file = fileChooser.showOpenDialog(null);

		if (file != null) {
			System.out.println(file);
			path = file.toURI().toURL().toString();
			imgpath = file.getPath().toString();
			loader = new Loader(imgpath);
			saver = new XMLSaver(loader);
			// Create Image and ImageView objects
			image = new Image (path); // there should be variable path, but doesnt work
	        
			// Preparation for image View
			ImageView imageView = new ImageView();
	        imageView.setImage(image);
	        fileStreamXML+=StreamBegin;
	        fileStreamXML+="<Description xsi:path=\""+imgpath+"\">\n";
		    
		}else{
			System.out.print("No file loaded");
		}

	}
	
	public void savetoXML(ActionEvent event) {
		String savePath = null;
		savePath = imgpath.substring(0, imgpath.length() -3);
		savePath+="xml";
		System.out.println(savePath);
		saver.saveXML(savePath, fileStreamXML);
	}
	
	//SCALABLE COLOR
	public void rgbHistogram(ActionEvent event) {
		histogram = saver.histogram(Main.RGB, 256);
		fileStreamXML+=histogram;
	}
	
	public void scButtonFired256(ActionEvent event) {
		histogram = saver.histogram(Main.HSV, 256);
		fileStreamXML+=histogram;
	}
	
	public void scButtonFired128(ActionEvent event) {
		histogram = saver.histogram(Main.HSV, 128);
		fileStreamXML+=histogram;
	}
	
	public void scButtonFired64(ActionEvent event) {
		histogram = saver.histogram(Main.HSV, 64);
		fileStreamXML+=histogram;
	}
	
	public void scButtonFired32(ActionEvent event) {
		histogram = saver.histogram(Main.HSV, 32);
		fileStreamXML+=histogram;
	}
	
	public void scButtonFired16(ActionEvent event) {
		histogram = saver.histogram(Main.HSV, 16);
		fileStreamXML+=histogram;
	}
	
	public void dcButtonFired1DC (ActionEvent event) {
		dominantColor = saver.dominantColor(DominantColor.MPEG7, 1);
		fileStreamXML+=dominantColor;
	}
	
	public void dcButtonFired4DC (ActionEvent event) {
		dominantColor = saver.dominantColor(DominantColor.MPEG7, 4);
		fileStreamXML+=dominantColor;
	}
	
	public void dcButtonFired8DC (ActionEvent event) {
		dominantColor = saver.dominantColor(DominantColor.MPEG7, 8);
		fileStreamXML+=dominantColor;
	}
	
	public void dcButtonFired1DCA (ActionEvent event) {
		dominantColor = saver.dominantColor(DominantColor.ALTERNATIVE, 1);
		fileStreamXML+=dominantColor;
	}
	
	public void dcButtonFired4DCA (ActionEvent event) {
		dominantColor = saver.dominantColor(DominantColor.ALTERNATIVE, 4);
		fileStreamXML+=dominantColor;
	}
	
	public void dcButtonFired8DCA (ActionEvent event) {
		dominantColor = saver.dominantColor(DominantColor.ALTERNATIVE, 8);
		fileStreamXML+=dominantColor;
	}
	
	
	/*private int slider = 4;
	private Slider slider;
	public void sliderMove(ActionEvent event) {
		sliderLabel.setText(Integer.toString(slider));
	}*/
	
	/*public void putInDbFired(ActionEvent event) throws XQException {
		Query que = Main.getQue();
		System.out.println("Zmáčkul jsem tlačítko");
		que.addPath(path); // Add path of file to DB
		que.addScalableColor(path); // Add path of file to DB
		que.addHistogram(histogram,path); // Add histogram + xml code to DB
		que.printDB("imagedb"); // Just another print of DB for testing purposes
	}*/
	
	public XMLSaver getSaver() {
		return saver;
	}

	public void setSaver(XMLSaver saver) {
		this.saver = saver;
	}
}