/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 *  Swap STEREO-A / STEREO-B FOV after 14/07/2015
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.drawingPlane;

import fr.tlse.irap.spaceElement.Camera;
import fr.tlse.irap.spaceElement.Probe;
import fr.tlse.irap.spaceElement.SpaceElement;
import fr.tlse.irap.utils.CoordConverter;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;

public class DrawingFOV {

	private Arc arc;
	private Color color = Color.RED;
	private double longStart = 0;
	private double startAngle = 0.0;
	private double extent = 90.0;
	private double radiusInKm;
	private double centerX = 0;
	private double centerY = 0;
	
	static final String className = "FOV";

	private static double DEFAULT_RADIUS = 170000000.0;
	private static double FOV_TRANSPARENCY = 0.2;

	public DrawingFOV() {
		radiusInKm = DEFAULT_RADIUS;
		createFOVArc();
		arc.setId(className);
	}

	/**
	 * @param spaceElement
	 * @param camera
	 */
	public DrawingFOV(Probe probe, Camera camera, boolean isBackward) {
		this.radiusInKm = DEFAULT_RADIUS;
			
		setOrientation(camera.getAngleFov() - camera.getExtentFov()/2, camera.getExtentFov(), isBackward);
		updateSource(probe);
		createFOVArc();
		
		arc.setId(className + probe.getName() + camera.getName());
	}

	/**
	 * 
	 */
	public void update() {
		double rad = getRadius();
		arc.setRadiusX(rad);
		arc.setRadiusY(rad);
		arc.setCenterX(centerX);
		arc.setCenterY(centerY);
		double phi = CoordConverter.normalizeLongitude(longStart + startAngle);
		arc.setStartAngle(phi);
		arc.setLength(extent);
		// arc.setStroke(Color.web(spaceElement.getColor()));
		arc.setStroke(color);
		if (color != null) {
			arc.setFill(new Color(color.getRed(), color.getGreen(), color.getBlue(), FOV_TRANSPARENCY));
		}
	}

	/**
	 * Update FOV according to new source
	 * @param source
	 */
	public void updateSource(SpaceElement source) {
		// FOV color = source color
		this.color = Color.web(source.getColor());
		this.centerX = source.getDisplayX();
		this.centerY = source.getDisplayY();
		// compute longitude source-sun (= long source + PI)
		this.longStart = CoordConverter.normalizeLongitude(Math.toDegrees(Math.atan2(-centerY, centerX) + Math.PI));
	}

	/**
	 * Create the default FOV arc
	 * 
	 * @param color
	 * @param centerX
	 * @param centerY
	 * @param startAngle
	 * @param length
	 * @param radius
	 */
	private void createFOVArc() {
		arc = new Arc();

		arc.setType(ArcType.ROUND);
		arc.setFill(Color.TRANSPARENT);
		update();
	}

	public Arc getArc() {
		return arc;
	}

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

	private double getRadius() {
		return CoordConverter.toEcliptic(radiusInKm * DrawHelper.INIT_SCALE * DrawHelper.getScale());
	}
	
	public void show() {
		if (!arc.isDisable()) {
			arc.setVisible(true);
		}
	}

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

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

	public void setOrientation(double phiStart, double extent, boolean isBackward) {
		this.extent = extent;
		if (isBackward) {
			this.startAngle = -(phiStart + extent);
		} else {
			this.startAngle = phiStart;
		}
	}
	
	public void setCenter(double x, double y) {
		this.centerX = x;
		this.centerY = y;
	}

	public void setDisable(boolean b) {
		arc.setDisable(b);
	}
	
	public boolean isDisable() {
		if (arc != null) {
			return arc.isDisable();
		}
		else {
			return true;
		}
	}
}
