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

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

import fr.tlse.irap.pt.client.controller.common.Formatter;
import fr.tlse.irap.pt.client.controller.tools.IToolController.JMapTool;
import fr.tlse.irap.fit.Fit;
import fr.tlse.irap.tool.radial.CMEType;
import fr.tlse.irap.tool.radial.Radial;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class JMapRadialModel {

	protected static final Logger log = LoggerFactory.getLogger(JMapRadialModel.class);
	private static double EXTENT_SSE = 60.0;

	public JMapCompute jMapCompute = new JMapCompute();
	
	private StringProperty cmeType = new SimpleStringProperty(); 
	private StringProperty speed = new SimpleStringProperty();
	private StringProperty extent = new SimpleStringProperty();
	private StringProperty beta = new SimpleStringProperty();
	private StringProperty southPa = new SimpleStringProperty();
	private StringProperty northPa = new SimpleStringProperty();
	private StringProperty centralPa = new SimpleStringProperty();
	
	private JMapTool jMapTool;

	/**
	 * Update data from view 
	 */
	public void update() {
		jMapCompute.setSpeedSolarWind(Formatter.toDouble(speed.getValue()));
		jMapCompute.setBetaStart(Formatter.toDouble(beta.getValue()));
		jMapCompute.updateLongStart();
	}

	/** 
	 * clear view
	 */
	public void clearView() {
		setSpeed(null);
		setBeta(null);
	}

	/**
	 * Update Radial model from JMap Radial model 
	 */
	public void updateRadial(Radial radial) {
		radial.setLinkcatMode(jMapCompute.isLinkcatMode());
		radial.setObs(jMapCompute.getJMapPosition().getObsInSitu().getName());
		radial.setCmeType(jMapCompute.getCmeType());
		radial.setLongStartT(jMapCompute.getLongStart());
		if (jMapTool != JMapTool.INSITU) {
			radial.setSpeedSolarWind(jMapCompute.getSpeedSolarWind());
			radial.setStartTime(jMapCompute.getJMapPosition().getStartTime());
			if (jMapCompute.getCmeType().equals(CMEType.SSE)) {
				radial.setExtentCME(EXTENT_SSE);
			}
		}
	}

	/**
	 * Update view from Radial
	 * @param radial
	 */
	public void updateViewFrom(Radial radial) {
		// update data for computation
		jMapCompute.getSunTime().setTime(radial.getSunTime().getTimeInMillis());
		jMapCompute.getJMapPosition().getStartTime().setTime(radial.getStartTime().getTimeInMillis());
		jMapCompute.setLongStart(radial.getLongStartT());
		jMapCompute.setSpeedSolarWind(radial.getSpeedSolarWind());
		// set drag model
		if (radial.isDragModel()) {
			jMapCompute.setDragModel(radial.getInnerDragModel());
		}
		else {
			jMapCompute.setDragModel(null);
		}
		// update view
		setSpeed(Formatter.toDecimal1D(radial.getSpeedSolarWind()));
		setExtent(radial.getExtentCME());
	}

	/** 
	 * update view from a catalog fit 
	 * @param fitId
	 */
	public void updateViewFromFit(int fitId) {
		updateView();
		if (jMapCompute.getCmeType().equals(CMEType.SSE)) {
			setExtent(EXTENT_SSE);
		}
		if (fitId >= 0) {
			Fit fit = jMapCompute.getFitCatalog().get(fitId);
			setCentralPa(fit.getCentralPa());
			setNorthPa(fit.getNorthPa());
			setSouthPa(fit.getSouthPa());
		}
	}

	/**
	 * Update view fields after computing (click or select fit from catalog)
	 * Update HAE longStart from beta and longObs at time tStart
	 * Unbind startDate property 
	 */
	public void updateView() {
		setCmeType(jMapCompute.getCmeType().getName());
		setBeta(jMapCompute.getBetaStart());
		setSpeed(jMapCompute.getSpeedSolarWind());
		// update longStart
		jMapCompute.updateLongStart();
		jMapCompute.getJMapPosition().unbindStartDate();
	}

	/**
	 * Update view InSitu (beta)
	 */
	public void updateViewInSitu() {
		setCmeType(jMapCompute.getCmeType().getName());
		if (jMapCompute.isComputed()) {
			setBeta(jMapCompute.getBetaStart());
		}
	}

	public StringProperty getExtent() {
		return extent;
	}

	public void setExtent(String extent) {
		this.extent.set(extent);
	}

	public void setExtent(double extent) {
		this.extent.set(Formatter.toDecimal1D(extent));
	}

	public StringProperty getSpeed() {
		return speed;
	}

	public void setSpeed(String speed) {
		this.speed.set(speed);
	}

	public void setSpeed(double speed) {
		this.speed.set(Formatter.toDecimal1D(speed));
	}

	public StringProperty getBeta() {
		return beta;
	}

	public void setBeta(String beta) {
		this.beta.set(beta);
	}

	public void setBeta(double beta) {
		this.beta.set(Formatter.toDecimal1D(beta));
	}

	public JMapTool getJMapTool() {
		return jMapTool;
	}

	public void setJMapTool(JMapTool jMapTool) {
		this.jMapTool = jMapTool;
		setCmeType(CMEType.FIXED_POINT.getName());
	}

	public StringProperty getCmeType() {
		return cmeType;
	}

	public void setCmeType(String cmeType) {
		this.cmeType.set(cmeType);
	}

	public StringProperty getSouthPa() {
		return southPa;
	}

	public void setSouthPa(double southPa) {
		this.southPa.set(Formatter.toDecimal1D(southPa));
	}

	public StringProperty getNorthPa() {
		return northPa;
	}

	public void setNorthPa(double northPa) {
		this.northPa.set(Formatter.toDecimal1D(northPa));
	}

	public StringProperty getCentralPa() {
		return centralPa;
	}

	public void setCentralPa(double centralPa) {
		this.centralPa.set(Formatter.toDecimal1D(centralPa));
	}

}
