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

import java.util.List;

import fr.tlse.irap.flare.Flare;
import fr.tlse.irap.flare.Flare.FlareType;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Node;

public class CarringtonFlares {

	private BooleanProperty hasChanged = new SimpleBooleanProperty();
	private BooleanProperty visibilityChanged = new SimpleBooleanProperty();
	private BooleanProperty visibilityAllChanged = new SimpleBooleanProperty();
	private FlareType flareClass;
	private List<Flare> flares;
	
	private Group flarePoints;
	
	public CarringtonFlares() {
		flarePoints = new Group();
		
		// Listen for property changed => draw flare points 
		hasChanged.addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> observedBoolean, Boolean oldValue, Boolean newValue) {
						clearNodes();
						addNodes(flares);
						visibilityAllChanged.set(false);
					}
				});

		// Listen for property visibility changed => swap visibility 
		visibilityChanged.addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> observedBoolean, Boolean oldValue, Boolean newValue) {
						for (Node node : flarePoints.getChildren()) {
							if (node.getId() != null)
								if (node.getId().contains("FLARE" + flareClass)) {
									node.setVisible(!node.isVisible());
								}
						}
					}
				});
		// Listen for property visibility All changed => swap All visibility 
		visibilityAllChanged.addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> observedBoolean, Boolean oldValue, Boolean newValue) {
						for (Node node : flarePoints.getChildren()) {
							if (node.getId() != null) {
								node.setVisible(visibilityAllChanged.get());
							}
						}
					}
				});
		
		visibilityAllChanged.set(false);
	}

	public void setFlares(List<Flare> flareList) {
		flares = flareList;
		hasChanged.set(!hasChanged.get());
	}

	public Group getNodes(){
		return flarePoints;
	}
	
	public void showHideBy(FlareType fc) {
		flareClass = fc;
		visibilityChanged.set(!visibilityChanged.get());
	}

	public void showHideAll() {
		visibilityAllChanged.set(!visibilityAllChanged.get());
	}

	private void addNodes(List<Flare> flares) {
		if (flares != null) {
			for (FlareType ft : FlareType.values()) {
				for (Flare flare : flares) {
					if (flare.getCategory().equals(ft)) {
						flarePoints.getChildren().add(CarringtonHelper.createFlare(flare));
					}
				}
			}
		}
	}

	private void clearNodes(){
		flarePoints.getChildren().clear();
	}

}
