/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Version : 13/09/2016 : SCA : V2.2 (PSWS-H2020)
 *  Swap STEREO-A / STEREO-B FOV after 14/07/2015
 * VERSION : 04/06/2024 : V2.10
 * Redmine#11605 : 04/06/2024 : second swap date added for stereo
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.jmap;

import fr.tlse.irap.pt.client.controller.common.Formatter;
import fr.tlse.irap.map.JMapHeader;
import fr.tlse.irap.spaceElement.Probe;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class JMapModel {

	// dynamic properties
	private StringProperty startLabel = new SimpleStringProperty();
	private StringProperty sourceLabel = new SimpleStringProperty();
	private StringProperty typeLabel = new SimpleStringProperty();
	private StringProperty minElongLabel = new SimpleStringProperty();
	private StringProperty maxElongLabel = new SimpleStringProperty();
	private StringProperty posAngleLabel = new SimpleStringProperty();
	private StringProperty fitTypeLabel = new SimpleStringProperty();

	private String source;
	private String type;
	private double minElong;
	private double maxElong;
	private double posAngle;
	
	public static enum InstrumentFov {
		SECCHI_A("SECCHI-A", "STEREO-A"), 
		SECCHI_B("SECCHI-B", "STEREO-B"), 
		SOLOHI("SOLOHI", "SOLO"), 
		SOHO("SOHO", "SOHO"),
		WISPR("WISPR", "PSP"); 

		private String name;
		private String obs;

		InstrumentFov(String name, String obs) {
			this.name = name;
			this.obs = obs;
		}
		public String getName() {return name;}
		public String getObs() {return obs;}
	}
	public static enum SourceType {REAL, ENLIL;}

	public void reset(){
		// default
		source = InstrumentFov.SECCHI_A.getName();
		type = SourceType.REAL.name();
		minElong = 0;
		maxElong = 90.0;
		posAngle = 90.0;
	}

	public void restore(){
		source = sourceLabel.get();
		type = typeLabel.get();
		minElong = Formatter.toDouble(minElongLabel.get());
		maxElong = Formatter.toDouble(maxElongLabel().get());
		posAngle = Formatter.toDouble(posAngleLabel().get());
	}

	public void updateView() {
		sourceLabel.set(source);
		typeLabel.set(type);
		minElongLabel.set(Formatter.toDecimal(minElong));
		maxElongLabel.set(Formatter.toDecimal(maxElong));
		if (posAngle > 0) {
			posAngleLabel.set("PA="+Formatter.toDecimal1D(posAngle)+"°");
		}
		updateView(null);
	}

	/**
	 * Update from chosen catalog menu
	 * @param catalogType
	 */
	public void updateView(String catalogType) {
		if (catalogType != null) {
			fitTypeLabel.set("Catalogue="+catalogType);
		}
		else {
			fitTypeLabel.set("");
		}
	}

	/**
	 * Update from chosen map menu 
	 * @param source
	 * @param type
	 */
	public void update(String source, String type){
		this.source = source;
		this.type = type;
	}

	/**
	 * update from a new Jmap header 
	 */
	public void update(JMapHeader jMapHeader) {
		if (jMapHeader != null){
			minElong = jMapHeader.getMinElongation();
			maxElong = jMapHeader.getMaxElongation();
			posAngle = jMapHeader.getPositionAngle();
			if (posAngle == 0) {
				// set manually PA according to source 
				if (source.equals(InstrumentFov.SECCHI_A.getName())) {
					minElong = 0;
					maxElong = 90;
					//if (jMapHeader.getStartDate().before(Probe.getStereoSwapDate())) {
                                        if(Probe.isBackward("STEREO-A", jMapHeader.getStartDate())) {
						posAngle = 270;
					}
					else {
						posAngle = 90;
					}
				} 
				else if (source.equals(InstrumentFov.SECCHI_B.getName())) {
					minElong = 0;
					maxElong = 90;
					//if (jMapHeader.getStartDate().before(Probe.getStereoSwapDate())) {
                                        if(Probe.isBackward("STEREO-B", jMapHeader.getStartDate())) {
						posAngle = 270;
					}
					else {
						posAngle = 90;
					}
				}
				else if (source.equals(InstrumentFov.WISPR.getName())) {
					minElong = 13;
					maxElong = 108;
					posAngle = 252;
				}
				else if (source.equals(InstrumentFov.SOLOHI.getName())) {
					minElong = 0;
					maxElong = 20;
					posAngle = 20;
				}
			}
		}
	}

	public String getObserver() {
		for (InstrumentFov instr : InstrumentFov.values()) {
			if (instr.getName().equals(source)) {
				return instr.getObs();
			}
		}
		return null;
	}
//
	public StringProperty startLabel() {
		return startLabel;
	}

	public StringProperty sourceLabel() {
		return sourceLabel;
	}

	public StringProperty typeLabel() {
		return typeLabel;
	}

	public StringProperty minElongLabel() {
		return minElongLabel;
	}

	public StringProperty maxElongLabel() {
		return maxElongLabel;
	}

	public StringProperty posAngleLabel() {
		return posAngleLabel;
	}

	public StringProperty fitTypeLabel() {
		return fitTypeLabel;
	}

	public String getSource() {
		return source;
	}

	public String getType() {
		return type;
	}

	public double getMinElong() {
		return minElong;
	}

	public void setMinElong(double minElong) {
		this.minElong = minElong;
	}

	public double getMaxElong() {
		return maxElong;
	}

	public void setMaxElong(double maxElong) {
		this.maxElong = maxElong;
	}

	public double getPosAngle() {
		return posAngle;
	}

	public void setPosAngle(double posAngle) {
		this.posAngle = posAngle;
	}

}
