package gui;

import alg.XML_processing;
import data.*;
import javafx.application.HostServices;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.ReadOnlyDoubleWrapper;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.controlsfx.control.CheckComboBox;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.regex.Pattern;

import static gui.GUIController.showAlert;

@SuppressWarnings("Duplicates")
public class AboutTab {
	public final static Logger logger = LogManager.getLogger(AboutTab.class);

	private HostServices hostService;

	@FXML
	public AnchorPane aboutTabPane;

	@FXML
	public Pane paneSlo;

	@FXML
	public Pane paneEng;

	@FXML
	public Label headerL;

	@FXML
	public Label descriptionL;

	@FXML
	public Label signatureL;

	@FXML
	public Label footerL;

	@FXML
	public Label linksL;

	@FXML
    private Hyperlink cjvtH;

	@FXML
    private Hyperlink githubH;

	@FXML
	private Button changeLanguageB;

	@FXML
	public ImageView stringLengthI;

	// TODO: pass observables for taxonomy based on header scan
	// after header scan

	public void initialize() {
		aboutTabPane.getStylesheets().add("style.css");
		aboutTabPane.getStyleClass().add("root");

		Messages.setPane(paneSlo, paneEng);

		manageTranslations();

		changeLanguageB.setOnAction(e -> {
			if (I18N.getLocale() == new Locale.Builder().setLanguage("sl").setRegion("SI").build()){
				I18N.setLocale(Locale.ENGLISH);
			} else {
				I18N.setLocale(new Locale.Builder().setLanguage("sl").setRegion("SI").build());
			}
			Messages.reload();
			Messages.updateChooseCorpusL();
			logger.info("change language");
		});

		cjvtH.setOnAction(e -> openCjvtWebsite());

		githubH.setOnAction(e -> openGithubWebsite());
	}

	private void manageTranslations(){
		changeLanguageB.textProperty().bind(I18N.createStringBinding("button.language"));
		headerL.textProperty().bind(I18N.createStringBinding("about.header"));
		descriptionL.textProperty().bind(I18N.createStringBinding("about.description"));
		signatureL.textProperty().bind(I18N.createStringBinding("about.signature"));
		footerL.textProperty().bind(I18N.createStringBinding("about.footer"));
		linksL.textProperty().bind(I18N.createStringBinding("about.links"));
//		calculateForL.textProperty().bind(I18N.createStringBinding("label.calculateFor"));
//		displayTaxonomyL.textProperty().bind(I18N.createStringBinding("label.displayTaxonomy"));
//
//		addTooltipToImage(stringLengthI, I18N.createStringBinding("label.letter.stringLengthH"));
	}

	private void openCjvtWebsite(){
		hostService.showDocument(Messages.CJVT_URL);
	}

	private void openGithubWebsite(){
		hostService.showDocument(Messages.GITHUB_URL);
	}

	public void setHostServices(HostServices hostServices){
		this.hostService = hostServices;
	}
}
