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

import java.util.ArrayList;
import java.util.List;

import fr.tlse.irap.utils.CoordConverter;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;

public class CmeFixPoint implements IDrawingCme {

	private static double DELTA_RADIUS = 20.0;

	private Arc arc;
	private Arc complementArc;
	private double longitude = 0.0;
	private double extent; 
	private double radiusInKm = 0;
	
	public CmeFixPoint() {
		this.extent = DEFAULT_EXTENT;
		createCMEArc(0, 360, 0.0);
		createComplementArc(longitude, extent, DELTA_RADIUS);
	}

	public void updateViewWith(double longSource, double extent) {
		this.longitude = longSource;
		this.extent = extent;
		update();
	}

	public void updateViewWith(double radiusInKm) {
		this.radiusInKm = radiusInKm;
		update();
	}

	public void update(){
		double displayLongitude = DrawHelper.HAEToHEE(longitude);
		modifyCMEWith(displayLongitude, extent);
		double scale = DrawHelper.INIT_SCALE * DrawHelper.getScale();
		setRadius(CoordConverter.toEcliptic(radiusInKm * scale));
	}
	
	/**
	 * Create the default CME arc with raimbow colors
	 * @param angle
	 * @param length
	 * @param radius
	 * @return
	 */
	private void createCMEArc(double angle, double length, double radius) {
		arc = new Arc();
		arc.setType(ArcType.ROUND);
		arc.setId("CME");

		Stop[] stops = new Stop[] { new Stop(1, Color.RED),
				new Stop(0.90, Color.web("#ef504c")),
				new Stop(0.80, Color.web("#f8bd55")),
				new Stop(0.65, Color.web("#c0fe56")),
				new Stop(0.40, Color.web("#5dfbc1")),
				new Stop(0.25, Color.web("#64c2f8")),
				new Stop(0.1, Color.DARKBLUE) };
		RadialGradient lg = new RadialGradient(0, 0, 0.5, 0.5, 0.5, true, CycleMethod.NO_CYCLE, stops);

		arc.setRadiusX(radius);
		arc.setRadiusY(radius);
		arc.setStartAngle(angle - length / 2);
		arc.setLength(length);
		arc.setFill(lg);
		// CME not visible by default
		arc.setVisible(false);
	}

	/**
	 * Create the complementary arc
	 * @param angle
	 * @param length
	 * @param radius
	 * @return
	 */
	private void createComplementArc(double angle, double length, double radius) {
		complementArc = new Arc();

		complementArc.setType(ArcType.ROUND);
		complementArc.setId("CME");

		complementArc.setRadiusX(radius);
		complementArc.setRadiusY(radius);
		complementArc.setStartAngle(angle + length / 2);
		complementArc.setLength(360 - length);
		complementArc.setFill(Color.WHITE);
		//complementArc.setFill(Color.BLACK);

		// CME not visible by default
		complementArc.setVisible(false);
	}

	/**
	 * Modify with new parameters
	 * @param startAngle : angle between x-axis and bottom of the arcCME
	 * @param length : longitude extent of the arcCME
	 */
	private void modifyCMEWith(double angle, double length) {
		complementArc.setStartAngle(angle + length / 2);
		complementArc.setLength(360 - length);
	}

	public Arc getArc() {
		return arc;
	}
	public Arc getComplementArc() {
		return complementArc;
	}

	public void setArc(Arc arc) {
		this.arc = arc;
	}
	public void setComplementArc(Arc arc) {
		this.complementArc = arc;
	}

	public double getExtent() {
		return extent;
	}

	public void setExtent(double extent) {
		this.extent = extent;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public double getRadialDistKm() {
		return radiusInKm;
	}

	public void show() {
		if (!arc.isDisable()) {
			arc.setVisible(true);
			complementArc.setVisible(true);
		}
	}

	public void hide() {
		arc.setVisible(false);
		complementArc.setVisible(false);
	}

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

	public void setDisable(boolean disable) {
		arc.setDisable(disable);
		complementArc.setDisable(disable);
	}

	public List<Node> getNodes() {
		List<Node> nodes = new ArrayList<Node>();
		nodes.add(arc);
		nodes.add(complementArc);
		return nodes;
	}

	public double getRadius() {
		return arc.getRadiusX();
	}

	public void setRadius(double radius) {
		this.arc.setRadiusX(radius);
		this.arc.setRadiusY(radius);
		this.complementArc.setRadiusX(radius+DELTA_RADIUS);
		this.complementArc.setRadiusY(radius+DELTA_RADIUS);
	}

}
