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

import java.util.Date;

import fr.tlse.irap.spaceElement.Planet;
import fr.tlse.irap.spaceElement.Probe;
import fr.tlse.irap.spaceElement.SpaceElement;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class VPlotModel {

    public StringProperty sourceLabel = new SimpleStringProperty();

    public enum DataSource {
        REAL, SIMULATED;
    }

    public static int NB_MILLISEC_PER_DAY = 24 * 3600 * 1000;
    public static int NB_DAYS = 1;
    public static final int NB_TICS = 12;
    public static final int DELTA_V = 15;

    private Date startDate = new Date();
    private Date endDate = new Date();
    private Date date;
    private String source;
    private DataSource type = DataSource.REAL;
    private String catalog;
    private String catType;
    private double speed;
    private boolean valid;

    public static enum Catalog {
        CME("CMEs"),
        CIR("CIRs"),
        SEP("SEPs"),
        SHOCK("Shocks");

        private String name;

        Catalog(String name) {
            this.setName(name);
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static enum CatalogType {
        HELCATS("HELCATS list"),
        JIAN("Lan Jian list"),
        LEPPING("Lepping cloud list"),
        RICHARDSON("Richardson and Cane list"),
        KASPER("Kasper list");

        private String name;

        CatalogType(String name) {
            this.setName(name);
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public VPlotModel() {
    }

    /**
     * update dates
     */
    public void setDates(Date selectedDate) {
        date = selectedDate;
        startDate.setTime(date.getTime() - NB_DAYS * NB_MILLISEC_PER_DAY);
        endDate.setTime(date.getTime() + NB_DAYS * NB_MILLISEC_PER_DAY);
    }

    /**
     * update source label view from data
     */
    public void updateView() {
        if (source == null || !valid) {
            sourceLabel.set("No VPlot");
        } else if (type == DataSource.REAL) {
            sourceLabel.set(source);
        } else {
            sourceLabel.set(source + " (simulated)");
        }
    }

    /**
     * Update data model (source, type by default = REAL) from parameter
     *
     * @param e
     */
    public void update(SpaceElement e) {
        update(e, DataSource.REAL);
    }

    /**
     * Update data model (source, type) from parameters
     *
     * @param e
     * @param dataSource (REAL or SIMULATED)
     */
    public void update(SpaceElement e, DataSource dataSource) {
        if (e instanceof Probe) {
            // data provided by a spacecraft are real data
            this.type = DataSource.REAL;
            this.source = e.getName();
        } else if (e instanceof Planet) {
            this.type = dataSource;
            if (type == DataSource.SIMULATED) {
                // data provided by a planet are simulated data (result of 1-D MHD of Chihiro Tao model)  
                this.source = e.getName();
            } else {
                // real data provided by spacecraft near the planet
                if (e.getName().equals("EARTH")) {
                    this.source = "OMNI";
                } else if (e.getName().equals("MARS")) {
                    this.source = "MEX";
                } else if (e.getName().equals("VENUS")) {
                    this.source = "VEX";
                } else if (e.getName().equals("MERCURY")) {
                    this.source = "MESSENGER";
                } else {
                    // no spacecraft near the planet
                    this.source = null;
                }
            }
        } else {
            // case sun or comets
            this.source = null;
        }
    }

    public Date getStartDate() {
        return startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public Date getDate() {
        return date;
    }

    public long getT1() {
        return startDate.getTime();
    }

    public long getT2() {
        return endDate.getTime();
    }

    public long getTime() {
        return date.getTime();
    }

    public void setTime(long time) {
        // create a new date, so that property will be invalidated
        date = new Date();
        date.setTime(time);
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public DataSource getType() {
        return type;
    }

    public void setType(DataSource type) {
        this.type = type;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double currentSpeed) {
        this.speed = currentSpeed;
    }

    public boolean isValid() {
        return valid;
    }

    public void setValid(boolean valid) {
        this.valid = valid;
    }

    public String getSourceLabel() {
        return sourceLabel.get();
    }

    public void setSourceLabel(String sourceLabel) {
        this.sourceLabel.set(sourceLabel);
    }

    public String getCatalog() {
        return catalog;
    }

    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    public String getCatType() {
        return catType;
    }

    public void setCatType(String catType) {
        this.catType = catType;
    }

}
