/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.menu;

import java.net.URL;
import java.util.ResourceBundle;

import fr.tlse.irap.pt.client.component.CustomComponent;
import fr.tlse.irap.pt.client.component.ICustomComponent;
import fr.tlse.irap.pt.client.controller.RootController;
import fr.tlse.irap.pt.client.controller.common.constant.TOOLTIP;
import fr.tlse.irap.pt.client.controller.tools.IToolController.JMapTool;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;

public class MenuController extends GridPane implements Initializable, ICustomComponent {

	@FXML private GridPane rootBtns;
	@FXML private ToggleGroup toolToggleGroup;
	@FXML private ToggleGroup jmapToggleGroup;

	@FXML private ToggleButton radialBtn; 
	@FXML private ToggleButton corotationBtn; 
	@FXML private ToggleButton sepBtn; 
	@FXML private ToggleButton inSituBtn;
	@FXML private ToggleButton fitBtn;
	@FXML private ToggleButton clickBtn;

	private RootController rootController;

	public MenuController() {
		super();
		CustomComponent.initCustomComponent(this);
	}

	@Override
	// This method is called by the FXMLLoader when initialization is complete
	public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
	}

	public void initialize(final RootController rootController) {
		this.rootController = rootController;
		bindToggleMenu();
		createToolTips();
	}

	private void createToolTips() {
		radialBtn.setTooltip(new Tooltip(TOOLTIP.MENU_RADIAL));
		corotationBtn.setTooltip(new Tooltip(TOOLTIP.MENU_COROTATION));
		sepBtn.setTooltip(new Tooltip(TOOLTIP.MENU_SEP));
	}

	public void setJMapMenu(JMapTool jMapTool) {
		if (jMapTool == JMapTool.INSITU) {
			jmapToggleGroup.selectToggle(inSituBtn);
		} else if (jMapTool == JMapTool.CLICK) {
			jmapToggleGroup.selectToggle(clickBtn);
		} else if (jMapTool == JMapTool.FIT) {
			jmapToggleGroup.selectToggle(fitBtn);
		} else {
			if (inSituBtn.isDisable()) {
				jmapToggleGroup.selectToggle(null);
			}
			else {
				// set default menu
				jmapToggleGroup.selectToggle(inSituBtn);
			}
		}
	}

	private void bindToggleMenu() {
	
		inSituBtn.setDisable(true);
		fitBtn.setDisable(true);
		clickBtn.setDisable(true);
		
		toolToggleGroup.selectedToggleProperty().addListener( new ChangeListener<Toggle>() {
			@Override
			public void changed (ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle newToggle) {
				if (newToggle != null) {
					String btn = ((ToggleButton) newToggle).getId();
					if (btn.equals("radialBtn")) {
						inSituBtn.setDisable(false);
						fitBtn.setDisable(false);
						clickBtn.setDisable(false);
						rootController.showRadial();
					} else if (btn.equals("corotationBtn")) {
						inSituBtn.setDisable(false);
						fitBtn.setDisable(false);
						clickBtn.setDisable(false);
						rootController.showCorotation();
					} else if (btn.equals("sepBtn")) {
						inSituBtn.setDisable(true);
						fitBtn.setDisable(true);
						clickBtn.setDisable(true);
						jmapToggleGroup.selectToggle(null);
						rootController.showSEP();
					} else {
						inSituBtn.setDisable(true);
						fitBtn.setDisable(true);
						clickBtn.setDisable(true);
						jmapToggleGroup.selectToggle(null);
						rootController.showWelcome();
					}
				} else {
					inSituBtn.setDisable(true);
					fitBtn.setDisable(true);
					clickBtn.setDisable(true);
					jmapToggleGroup.selectToggle(null);
					rootController.showWelcome();
				}
			}
		});

		jmapToggleGroup.selectedToggleProperty().addListener( new ChangeListener<Toggle>() {
			@Override
			public void changed (ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle newToggle) {
				if (newToggle != null) {
					//System.out.println("newToggle: " + newToggle);
					String btn = ((ToggleButton) newToggle).getId();
					if (btn.equals("inSituBtn")) {
						rootController.setJMapTool(JMapTool.INSITU);
					} else if (btn.equals("fitBtn")) {
						rootController.setJMapTool(JMapTool.FIT);
					} else if (btn.equals("clickBtn")) {
						rootController.setJMapTool(JMapTool.CLICK);
					} else {
						rootController.setJMapTool(null);
						rootController.showWelcome();
					}
				} else {
					if (!((ToggleButton) toggle).isDisable()) {
						jmapToggleGroup.selectToggle(toggle);
					}
				}
			}
		});
	}

	
}
