package gui;

import java.io.IOException;

import data.Filter;
import javafx.beans.binding.StringBinding;
import javafx.scene.layout.AnchorPane;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kordamp.ikonli.fontawesome.FontAwesome;
import org.kordamp.ikonli.javafx.FontIcon;

import data.Corpus;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;

import static nogui.NoGUIController.launch_no_gui;

public class GUIController extends Application {
	public final static Logger logger = LogManager.getLogger(GUIController.class);

	@FXML
	public AnchorPane gui;
	@FXML
	public Tab AboutTab;
	@FXML
	public Tab StringLevelTabNew2;
	@FXML
	public Tab OneWordAnalysisTab;
	@FXML
	public Tab CharacterLevelTabNew;
	@FXML
	public Tab corpusTab;
	public TabPane tabPane;
	@FXML
	private CharacterAnalysisTab catController;
	@FXML
	private static Parent sat;
	@FXML
	private StringAnalysisTabNew2 satNew2Controller;
	@FXML
	private static Parent satNew2;
	@FXML
	private OneWordAnalysisTab oneWordTabController;
	@FXML
	private AboutTab aboutTabController;
	@FXML
	private static Parent oneWordTab;
	@FXML
	private CorpusTab ctController;
	@FXML
	private WordLevelTab wlController;
	@FXML
	private FiltersForSolar ffsController;
	@FXML
	public Tab wordLevelTab;


	@FXML
	public Tab filterTab;
	public Stage stage;

	private Corpus corpus;


	@Override
	public void start(Stage primaryStage) throws IOException {
		Parent root = FXMLLoader.load(getClass().getResource("/GUI.fxml"));
		primaryStage.titleProperty().bind(I18N.createStringBinding("window.title"));
		Scene scene = new Scene(root, 800, 600);
		primaryStage.setScene(scene);
		stage = primaryStage;
		primaryStage.show();
	}

	public static void main(String[] args) {
		if (args.length > 0) {
			launch_no_gui(args);
			logger.info("Processing finalized!");
		} else {
			launch(args);
		}
		System.exit(0);
	}

	public void initialize() {
		// add CSS style
		gui.getStylesheets().add("style.css");
		gui.getStyleClass().add("root");

		corpus = new Corpus();
		manageTranslations();

		ctController.setCorpus(corpus);
		ctController.setFilterTab(filterTab);
		ctController.setAboutTab(AboutTab);
		ctController.setStringLevelTabNew2(StringLevelTabNew2);
		ctController.setOneWordAnalysisTab(OneWordAnalysisTab);
		ctController.setCharacterLevelTab(CharacterLevelTabNew);
		ctController.setSatNew2Controller(satNew2Controller);
		ctController.setOneWordTabController(oneWordTabController);
		ctController.setAboutTabController(aboutTabController);
		ctController.setCatController(catController);
		ctController.setWlController(wlController);
		ctController.setTabPane(tabPane);
		ctController.setFfsController(ffsController);
		ctController.setWordLevelTab(wordLevelTab);

		ctController.setHostServices(getHostServices());

		satNew2Controller.setCorpus(corpus);
		satNew2Controller.setHostServices(getHostServices());
		oneWordTabController.setCorpus(corpus);
		oneWordTabController.setHostServices(getHostServices());
		aboutTabController.setHostServices(getHostServices());
		catController.setCorpus(corpus);
		catController.setHostServices(getHostServices());
		wlController.setCorpus(corpus);
		wlController.setHostServices(getHostServices());
		ffsController.setSatNew2Controller(satNew2Controller);
		ffsController.setOneWordTabController(oneWordTabController);
		ffsController.setCatController(catController);
		ffsController.setWlController(wlController);
		ffsController.setHostServices(getHostServices());

		// set tab icons
		corpusTab.setGraphic(new FontIcon(FontAwesome.COG));
		filterTab.setGraphic(new FontIcon(FontAwesome.FILTER));

		// hide filter tab
		tabPane.getTabs().removeAll(filterTab);
	}

	private void manageTranslations(){
		corpusTab.textProperty().bind(I18N.createStringBinding("tab.corpusTab"));
		filterTab.textProperty().bind(I18N.createStringBinding("tab.filterTab"));
		CharacterLevelTabNew.textProperty().bind(I18N.createStringBinding("tab.characterLevelTabNew"));
		wordLevelTab.textProperty().bind(I18N.createStringBinding("tab.wordLevelTab"));
		OneWordAnalysisTab.textProperty().bind(I18N.createStringBinding("tab.oneWordAnalysisTab"));
		StringLevelTabNew2.textProperty().bind(I18N.createStringBinding("tab.stringLevelTabNew2"));
		AboutTab.textProperty().bind(I18N.createStringBinding("tab.aboutTab"));
	}

	static void showAlert(Alert.AlertType alertType, String headerText, String contentText) {
		Alert alert = new Alert(alertType);
		alert.setTitle(Messages.windowTitles.get(alertType));
		alert.setHeaderText(headerText != null ? headerText : "");
		alert.setContentText(contentText != null ? contentText : "");
		alert.showAndWait();
	}

	public static void showAlert(Alert.AlertType alertType, String headerText) {
		showAlert(alertType, headerText, null);
	}
}