/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 11/12/2018 : SCA : V2.2.5
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.drawingPlane;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.tlse.irap.shock.Shock;
import fr.tlse.irap.utils.CoordConverter;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;

public class Shockwave {

	protected static final Logger log = LoggerFactory.getLogger(Shockwave.class);
	
	private Ellipse ellipse;
	private double HAELon = 0.0; // Ellipsoid HAE longitude direction
	// Projection of ellipsoide dimensional parameters in Ecliptic plane. 
	private double rx;
	private double ry;
	private double rdSun;
	
	static final String className = "Shock";
	private static double SHOCK_TRANSPARENCY = 0.2;
	private static double SHOCK_STROKEWIDTH = 1;

	public Shockwave(Shock shock) {
		createShock();
		updateView(shock);
	}

	private void createShock() {
		ellipse = new Ellipse();
		//ellipse.setFill(Color.TRANSPARENT);
		Color color = Color.LIMEGREEN;
		ellipse.setStroke(color);
		ellipse.setStrokeWidth(SHOCK_STROKEWIDTH);
		ellipse.setFill(new Color(color.getRed(), color.getGreen(), color.getBlue(), SHOCK_TRANSPARENCY));
		ellipse.setId(className);
		ellipse.setVisible(false);
	}

	/**
	 * 
	 * Update 2D view from 3D shock
	 * @param shock
	 */
	public void updateView(Shock shock) {
		double theta = Math.toRadians(shock.getTheta());
		this.rdSun = shock.getRdSun() * Math.cos(theta);
		this.HAELon = shock.getPhi();
		this.rx = shock.getRadiusX() * Math.abs(Math.cos(theta)) + shock.getRadiusZ() * Math.abs(Math.sin(theta));
//		this.rx = shock.getRadiusX() * Math.cos(theta) - shock.getRadiusZ() * Math.sin(theta);
		this.ry = shock.getRadiusY();
		update();
	}

	/**
	 * 
	 */
	public void update() {
		double scale = DrawHelper.INIT_SCALE * DrawHelper.getScale();
		
		ellipse.setRadiusX(CoordConverter.toEcliptic(rx * scale));
		ellipse.setRadiusY(CoordConverter.toEcliptic(ry * scale));
		
		double rShock = CoordConverter.toEcliptic(rdSun * scale);
		double angle = DrawHelper.HAEToHEE(HAELon);
		double phi = Math.toRadians(angle);
		ellipse.setCenterX(rShock * Math.cos(phi));
		ellipse.setCenterY(-rShock * Math.sin(phi));
		ellipse.setRotate(-angle);
	}

	public Ellipse getEllipse() {
		return ellipse;
	}

	public void setEllipse(Ellipse ellipse) {
		this.ellipse = ellipse;
	}

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

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

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

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