/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Creation 
 * Version : 24/03/2016 : SCA : V2.2 (PSWS-H2020)
 * Version : 09/04/2018 : SCA : V2.2.4
 *  Redmine DMPT4
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.carrington;

import java.util.List;

import fr.tlse.irap.pt.client.controller.common.Position2D;
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.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.StrokeLineCap;

public class CarringtonShocks {

	private CarringtonShockModel shockModel;
	private BooleanProperty visibilityChanged = new SimpleBooleanProperty();

	private Path shock;
	private Group nodes = new Group();
	
	public CarringtonShocks() {
		shock = createShock();
		nodes.getChildren().add(shock);
		visibilityChanged.addListener(
			new ChangeListener<Boolean>() {
				@Override
				public void changed(ObservableValue<? extends Boolean> observedBoolean, Boolean oldValue, Boolean newValue) {
					if (shockModel.isValid()){
						shock.setVisible(visibilityChanged.get());
					}
					else {
						shock.setVisible(false);
					}
				}
			});
		
		visibilityChanged.set(false);
		
	}

	public void setColor(Color c) {
		((Path)nodes.getChildren().get(0)).setFill(c);
		((Path)nodes.getChildren().get(0)).setStroke(c);
	}
	
	public void setModel(CarringtonShockModel cShockModel){
		this.shockModel = cShockModel;
		
		// Listen for shock property changed 
		shockModel.getHasChanged().addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> observedBoolean, Boolean oldValue, Boolean newValue) {
						if (newValue) {
							if (shockModel.isValid()){
								drawShock();
							}
							else {
								clearShock();
							}
						}
					}

				});
	}

	public CarringtonShockModel getModel(){
		return this.shockModel;
	}
	
	protected void drawShock() {
		updateShock(shock, shockModel.getPositions());
		if (visibilityChanged.get() && shockModel.getPositions().size() > 0) {
			shock.setVisible(true);
		}
	}

	protected void clearShock() {
		shock.getElements().clear();
		shock.setVisible(false);
	}

	public void setVisibility(boolean visibility) {
		visibilityChanged.set(visibility);
	}

	public boolean isVisible() {
		return shock.isVisible();
	}

	public void showHide() {
		visibilityChanged.set(!visibilityChanged.get());
	}

	public Group getNodes(){
		return nodes;
	}

	/**
	 * Create the Shock 
	 */
	public static Path createShock() {

		final Path shock = new Path();
		Color color = Color.LIGHTGREEN;
		shock.setFill(color);
		shock.setStroke(color);
		shock.setStrokeWidth(2);
		shock.setStrokeLineCap(StrokeLineCap.ROUND);
		shock.setId("SHOCK");
		shock.setVisible(false);
		//shock.setVisible(true);
		return shock;
	}

	/**
	 * Update the Shock with a list of position
	 */
	public static void updateShock(Path shock, List<Position2D> positions) {
		MoveTo moveTo;
		LineTo lineTo;

		shock.getElements().clear();

		for (Position2D pos : positions) {
			moveTo = new MoveTo(pos.getX(), pos.getY());
			lineTo = new LineTo(pos.getX(), pos.getY());
			shock.getElements().add(moveTo);
			shock.getElements().add(lineTo);
		}
	}
}
