/* ----------------------------------------
 * 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 fr.tlse.irap.pt.client.controller.drawingPlane.DrawHelper.DrawingCmeType;
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 CarringtonCME {

	private CarringtonCMEModel CMEModel;

	private Path cme;
	private Group nodes = new Group();
	private DrawingCmeType type;
	
	public CarringtonCME() {
		cme = createCme();
		nodes.getChildren().add(cme);
	}

	public void setColor(Color c) {
		((Path)nodes.getChildren().get(0)).setFill(c);
		((Path)nodes.getChildren().get(0)).setStroke(c);
	}
	
	public void setModel(CarringtonCMEModel cMEModel){
		this.CMEModel = cMEModel;
		
		// Listen for CME property changed 
		CMEModel.getHasChanged().addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> observedBoolean, Boolean oldValue, Boolean newValue) {
						if (newValue) {
							if (CMEModel.isValid()){
								drawCme();
							}
							else {
								clearCme();
							}
						}
					}

				});
	}

	public void setDrawingType(DrawingCmeType type) {
		this.type = type;
		if (type.equals(DrawingCmeType.CME)) {
			setColor(Color.ORANGE);
		}
		else {
			setColor(Color.web("48ACFA"));
		}
	}

	public DrawingCmeType getDrawingType() {
		return type;
	}
	
	public CarringtonCMEModel getModel(){
		return this.CMEModel;
	}
	
	protected void drawCme() {
		updateCme(cme, CMEModel.getPositions());
		cme.setVisible(true);
	}

	protected void clearCme() {
		cme.getElements().clear();
		cme.setVisible(false);
	}

	public void show() {
		cme.setVisible(true);
	}

	public void hide() {
		cme.setVisible(false);
	}

	public boolean isVisible() {
		return cme.isVisible();
	}
	
	public Group getNodes(){
		return nodes;
	}

	/**
	 * Create the CME 
	 */
	public static Path createCme() {

		final Path cme = new Path();
		cme.setFill(Color.ORANGE);
		cme.setStroke(Color.ORANGE);
		cme.setStrokeWidth(2);
		cme.setStrokeLineCap(StrokeLineCap.ROUND);
		cme.setId("CME");
		cme.setVisible(false);
		return cme;
	}

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

		cme.getElements().clear();

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