/* ----------------------------------------
 * 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.tool.corotation.Corotation;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class JMapCorotationModel {

	protected static final Logger log = LoggerFactory.getLogger(JMapCorotationModel.class);

	public JMapCompute jMapCompute = new JMapCompute();

	private StringProperty speed = new SimpleStringProperty();
	private StringProperty rotPeriod = new SimpleStringProperty();
	private StringProperty blobsTimeInterval = new SimpleStringProperty(); 
	private StringProperty beta = 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.setBlobsInterval(Formatter.toDouble(blobsTimeInterval.getValue()).intValue());
		jMapCompute.updateLongStart();
	}

	/**
	 * Update only blobs interval
	 */
	public void updateBlobsInt() {
		jMapCompute.setBlobsInterval(Formatter.toDouble(blobsTimeInterval.getValue()).intValue());
	}

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

	/**
	 * Update Corotation model from JMap Corotation model 
	 */
	public void updateCorotation(Corotation corotation) {
		corotation.setLongStartT(jMapCompute.getLongStart());
		if (jMapTool != JMapTool.INSITU) {
			corotation.setSpeedSolarWind(jMapCompute.getSpeedSolarWind());
			corotation.setStartTime(jMapCompute.getJMapPosition().getStartTime());
		}
	}

	/**
	 * Update view from Corotation
	 * @param corotation
	 */
	public void updateViewFrom(Corotation corotation) {
		// update data for computation
		jMapCompute.getSunTime().setTime(corotation.getSunTime().getTimeInMillis());
		jMapCompute.getJMapPosition().getStartTime().setTime(corotation.getStartTime().getTimeInMillis());
		jMapCompute.setLongStart(corotation.getLongStartT());
		jMapCompute.setSpeedSolarWind(corotation.getSpeedSolarWind());
		jMapCompute.setRotationPeriod(corotation.getRotationPeriod());
		jMapCompute.setDistStart(corotation.getRadialDistanceStart());
		// update view
		setSpeed(Formatter.toDecimal1D(corotation.getSpeedSolarWind()));
		setRotPeriod(corotation.getRotationPeriod());
	}

	/**
	 * 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() {
		// update data for computation
		jMapCompute.updateLongStart();
		jMapCompute.getJMapPosition().unbindStartDate();
		// update view
		setBeta(jMapCompute.getBetaStart());
		setSpeed(jMapCompute.getSpeedSolarWind());
	}

	/**
	 * Update view InSitu (beta)
	 */
	public void updateViewInSitu() {
		if (jMapCompute.isComputed()) {
			setBeta(jMapCompute.getBetaStart());
		}
	}
	
	public StringProperty getRotPeriod() {
		return rotPeriod;
	}

	public void setRotPeriod(String rotPeriod) {
		this.rotPeriod.set(rotPeriod);
	}

	public void setRotPeriod(double rotPeriod) {
		this.rotPeriod.set(Formatter.toDecimal(rotPeriod));
	}

	public StringProperty getBlobsTimeInterval() {
		return blobsTimeInterval;
	}

	public void setBlobsTimeInterval(String blobsTimeInterval) {
		this.blobsTimeInterval.set(blobsTimeInterval);
	}

	public void setBlobsTimeInterval(double blobsTimeInterval) {
		this.blobsTimeInterval.set(Formatter.toDecimal1D(blobsTimeInterval));
	}

	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;
	}

}
