package gui;

import static data.Enums.solar.SolarFilters.*;

import java.util.*;

import javafx.application.HostServices;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TextArea;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.controlsfx.control.CheckComboBox;

import data.Corpus;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import util.Util;

public class FiltersForSolar {
	public final static Logger logger = LogManager.getLogger(OneWordAnalysisTab.class);

	@FXML
	public AnchorPane solarFiltersTabPane;
	@FXML
	public CheckComboBox<String> solarRegijaCCB;
	@FXML
	public CheckComboBox<String> solarPredmetCCB;
	@FXML
	public CheckComboBox<String> solarRazredCCB;
	@FXML
	public CheckComboBox<String> solarLetoCCB;
	@FXML
	public CheckComboBox<String> solarSolaCCB;
	@FXML
	public CheckComboBox<String> solarVrstaBesedilaCCB;
	@FXML
	public Label solarRegijaL;
	@FXML
	public Label solarPredmetL;
	@FXML
	public Label solarRazredL;
	@FXML
	public Label solarLetoL;
	@FXML
	public Label solarSolaL;
	@FXML
	public Label solarVrstaBesedilaL;
	@FXML
	public Label solarFilters;
	@FXML
	public TextArea selectedFiltersTextArea;
	@FXML
	private Button changeLanguageB;
//	@FXML
//	private Hyperlink helpH;

	private Corpus corpus;

	private StringAnalysisTabNew2 satNew2Controller;
	private OneWordAnalysisTab oneWordTabController;
	private CharacterAnalysisTab catController;
	private WordLevelTab wlController;
	private HostServices hostService;

	@SuppressWarnings("unchecked")
	public void initialize() {
		solarFiltersTabPane.getStylesheets().add("style.css");
		solarFiltersTabPane.getStyleClass().add("root");

		manageTranslations();

		solarRegijaCCB.getCheckModel().getCheckedItems().addListener((ListChangeListener) c -> {
			corpus.solarSelectedFilters.put(REGIJA, solarRegijaCCB.getCheckModel().getCheckedItems());
			updateSolarFilterLabel();
		});

		solarPredmetCCB.getCheckModel().getCheckedItems().addListener((ListChangeListener) c -> {
			corpus.solarSelectedFilters.put(PREDMET, solarPredmetCCB.getCheckModel().getCheckedItems());
			updateSolarFilterLabel();
		});

		solarRazredCCB.getCheckModel().getCheckedItems().addListener((ListChangeListener) c -> {
			corpus.solarSelectedFilters.put(RAZRED, solarRazredCCB.getCheckModel().getCheckedItems());
			updateSolarFilterLabel();
		});

		solarLetoCCB.getCheckModel().getCheckedItems().addListener((ListChangeListener) c -> {
			corpus.solarSelectedFilters.put(LETO, solarLetoCCB.getCheckModel().getCheckedItems());
			updateSolarFilterLabel();
		});

		solarSolaCCB.getCheckModel().getCheckedItems().addListener((ListChangeListener) c -> {
			corpus.solarSelectedFilters.put(SOLA, solarSolaCCB.getCheckModel().getCheckedItems());
			updateSolarFilterLabel();
		});

		solarVrstaBesedilaCCB.getCheckModel().getCheckedItems().addListener((ListChangeListener) c -> {
			corpus.solarSelectedFilters.put(TIP, solarVrstaBesedilaCCB.getCheckModel().getCheckedItems());
			updateSolarFilterLabel();
		});

		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();
			updateSolarFilterLabel();
			logger.info("change language");
		});

//        helpH.setOnAction(e -> openHelpWebsite());
	}

	private void manageTranslations() {
//		helpH.textProperty().bind(I18N.createStringBinding("hyperlink.help"));
		changeLanguageB.textProperty().bind(I18N.createStringBinding("button.language"));

		solarRegijaL.textProperty().bind(I18N.createStringBinding("filter.solarRegijaL"));
		solarPredmetL.textProperty().bind(I18N.createStringBinding("filter.solarPredmetL"));
		solarRazredL.textProperty().bind(I18N.createStringBinding("filter.solarRazredL"));
		solarLetoL.textProperty().bind(I18N.createStringBinding("filter.solarLetoL"));
		solarSolaL.textProperty().bind(I18N.createStringBinding("filter.solarSolaL"));
		solarVrstaBesedilaL.textProperty().bind(I18N.createStringBinding("filter.solarVrstaBesedilaL"));
		solarFilters.textProperty().bind(I18N.createStringBinding("label.solarFilters"));

	}

	public void initFilters() {
		solarRegijaCCB.getCheckModel().clearChecks();
		solarRegijaCCB.getItems().removeAll();
		solarRegijaCCB.getItems().setAll(corpus.getSolarFilters().get(REGIJA));
		solarRegijaCCB.getItems().sorted();
		solarPredmetCCB.getCheckModel().clearChecks();
		solarPredmetCCB.getItems().removeAll();
		solarPredmetCCB.getItems().setAll(corpus.getSolarFilters().get(PREDMET));
		solarPredmetCCB.getItems().sorted();
		solarRazredCCB.getCheckModel().clearChecks();
		solarRazredCCB.getItems().removeAll();
		solarRazredCCB.getItems().setAll(corpus.getSolarFilters().get(RAZRED));
		solarRazredCCB.getItems().sorted();
		solarLetoCCB.getCheckModel().clearChecks();
		solarLetoCCB.getItems().removeAll();
		solarLetoCCB.getItems().setAll(corpus.getSolarFilters().get(LETO));
		solarLetoCCB.getItems().sorted();
		solarSolaCCB.getCheckModel().clearChecks();
		solarSolaCCB.getItems().removeAll();
		solarSolaCCB.getItems().setAll(corpus.getSolarFilters().get(SOLA));
		solarSolaCCB.getItems().sorted();
		solarVrstaBesedilaCCB.getCheckModel().clearChecks();
		solarVrstaBesedilaCCB.getItems().removeAll();
		solarVrstaBesedilaCCB.getItems().setAll(corpus.getSolarFilters().get(TIP));
		solarVrstaBesedilaCCB.getItems().sorted();
	}

	private void updateSolarFilterLabel() {
		if (Util.isMapEmpty(corpus.solarSelectedFilters)) {
			setSOlarFIlterLabelText("/");
		} else {
			StringBuilder allFilters = new StringBuilder();
			for (Map.Entry<String, ObservableList<String>> entry : corpus.solarSelectedFilters.entrySet()) {
				ArrayList<String> values = new ArrayList<>(entry.getValue());

				if (!values.isEmpty()) {
					allFilters.append(I18N.get(entry.getKey() + "L"))
							.append(": ");

					for (int i = 0; i < values.size(); i++) {
						allFilters.append(values.get(i));

						if (i < values.size() - 1) {
							// so we won't append a comma after the last element
							allFilters.append(", ");
						}
					}
					allFilters.append("\n\n");
				}
			}

			setSOlarFIlterLabelText(allFilters.toString());
		}

		HashMap<String, HashSet<String>> solarFiltersMap = new HashMap<>();
		for (Map.Entry<String, ObservableList<String>> e : corpus.solarSelectedFilters.entrySet()) {
			HashSet<String> values = new HashSet<>();
			values.addAll(e.getValue());

			solarFiltersMap.put(e.getKey(), values);
		}

		satNew2Controller.setSolarFiltersMap(solarFiltersMap);
		oneWordTabController.setSolarFiltersMap(solarFiltersMap);
		catController.setSolarFiltersMap(solarFiltersMap);
		wlController.setSolarFiltersMap(solarFiltersMap);
	}

	private void openHelpWebsite(){
		hostService.showDocument(Messages.HELP_URL);
	}

	private void setSOlarFIlterLabelText(String content) {
		selectedFiltersTextArea.setText(content);
		satNew2Controller.setSelectedFiltersLabel(content);
		oneWordTabController.setSelectedFiltersLabel(content);
		catController.setSelectedFiltersLabel(content);
		wlController.setSelectedFiltersLabel(content);
	}

	public void setCorpus(Corpus corpus) {
		this.corpus = corpus;
		this.corpus.solarSelectedFilters = new HashMap<>();
	}

	public void setSatNew2Controller(StringAnalysisTabNew2 satNew2Controller) { this.satNew2Controller = satNew2Controller; }

	public void setOneWordTabController(OneWordAnalysisTab oneWordTabController) { this.oneWordTabController = oneWordTabController; }

	public void setCatController(CharacterAnalysisTab catController) { this.catController = catController; }

	public void setWlController(WordLevelTab wlController) {
		this.wlController = wlController;
	}

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