package com.madeorsk.smartnotes;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class SmartNotes extends Application
{
	public static final String version = "0.5";

	public static void main(String[] args)
	{
		System.out.println("Starting SmartNotes...");
		System.setProperty("prism.lcdtext", "false");
		launch();
		System.out.println("SmartNotes closed.");
	}

	public static SmartNotes instance;
	public static WebView webView;
	private Scene scene;
	private VBox root;
	private NotesExplorer explorer;

	@Override
	public void start(Stage stage) throws Exception
	{
		try
		{
			Logger.getLogger(GlobalScreen.class.getPackage().getName()).setLevel(Level.OFF);
			GlobalScreen.registerNativeHook();
			GlobalScreen.addNativeKeyListener(new GlobalKeyListener());
		}
		catch (NativeHookException ex)
		{
			System.err.println("There was a problem registering the native hook.");
			System.err.println(ex.getMessage());
		}

		Platform.setImplicitExit(false);

		instance = this;
		webView = new WebView();
		final String css = Utils.loadText("/com/madeorsk/smartnotes/webView.css");
		webView.getEngine().getLoadWorker().stateProperty().addListener((ObservableValue<? extends State> ov, State oldValue, State newValue) -> {
			if (newValue == State.SUCCEEDED)
			{
				Document doc = webView.getEngine().getDocument();
				Element styleNode = doc.createElement("style");
				Text styleContent = doc.createTextNode(css);
				styleNode.appendChild(styleContent);
				doc.getDocumentElement().getElementsByTagName("head").item(0).appendChild(styleNode);
			}
		});

		// Registering font files.
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime Sans.ttf"), 10);
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime Sans Bold.ttf"), 10);
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime Sans Italic.ttf"), 10);
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime Sans Bold Italic.ttf"), 10);
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime.ttf"), 10);
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime Bold.ttf"), 10);
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime Italic.ttf"), 10);
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime Bold Italic.ttf"), 10);
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime Code.ttf"), 10);
		Font.loadFont(this.getClass().getResourceAsStream("/com/madeorsk/smartnotes/res/Courier Prime Code Italic.ttf"), 10);

		this.root = new VBox();
		this.root.setStyle("-fx-background-color: #282828;");

		this.explorer = new NotesExplorer();
		ScrollPane scrollPane = new ScrollPane(this.explorer);
		scrollPane.setFitToHeight(true);
		scrollPane.setFitToWidth(true);
		this.root.getChildren().add(scrollPane);

		this.scene = new Scene(this.root, 800, 600);
		this.scene.getStylesheets().add("/com/madeorsk/smartnotes/style.css");

		stage.setOnCloseRequest((WindowEvent e) -> {
			SmartNotes.this.explorer.close();
		});

		stage.setScene(this.scene);
		stage.setTitle("SmartNotes " + version);
		stage.setOnCloseRequest((WindowEvent event) -> {
			SmartNotes.this.onStop();
		});
		stage.show();
	}

	public void setContent(Region region)
	{
		this.setContent(region, false);
	}

	public void setContent(Region region, boolean useScrollPane)
	{
		this.root.getChildren().clear();
		if (useScrollPane)
		{
			ScrollPane scrollPane = new ScrollPane(region);
			scrollPane.setFitToHeight(true);
			scrollPane.setFitToWidth(true);
			this.root.getChildren().add(scrollPane);
		}
		else
		{
			region.prefHeightProperty().bind(this.root.heightProperty());
			this.root.getChildren().add(region);
		}
	}

	private void onStop()
	{
		try
		{
			GlobalScreen.unregisterNativeHook();
		}
		catch (NativeHookException e)
		{
			e.printStackTrace();
		}
		Platform.exit();
	}

	public class GlobalKeyListener implements NativeKeyListener
	{ // CTRL : 29 | ALT : 56 | n : 49
		@Override
		public void nativeKeyPressed(NativeKeyEvent e)
		{
			//System.out.println("P == " + e.getKeyCode() + ":" + e.getKeyChar());
		}

		@Override
		public void nativeKeyReleased(NativeKeyEvent e)
		{
			//System.out.println("R == " + e.getKeyCode() + ":" + e.getKeyChar());
		}

		@Override
		public void nativeKeyTyped(NativeKeyEvent e)
		{
		}
	}
}
