/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Version : 05/05/2021 : LBE
 * DM-ID : Redmine#7043 : 05/05/2021 : Fermi-LAT units corrected
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.plot;

import java.util.List;

import fr.tlse.irap.flare.Flare;
import fr.tlse.irap.flare.Flare.FlareType;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;

public class FluxPlotFlares {

	//private static FlareType[] fc = { FlareType.A, FlareType.B, FlareType.C, FlareType.M, FlareType.X };
	
	private BooleanProperty hasChanged = new SimpleBooleanProperty();
	private BooleanProperty visibilityChanged = new SimpleBooleanProperty();
	private BooleanProperty visibilityAllChanged = new SimpleBooleanProperty();
	private FlareType flareClass;
	private List<Flare> flares;
	
	private LineChart<Number, Number> flareChart;
	
	public FluxPlotFlares(LineChart<Number, Number> fChart) {
		this.flareChart = fChart;
		// Listen for property changed 
		hasChanged.addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> observedBoolean, Boolean oldValue, Boolean newValue) {
						clearNodes();
						addNodes(flares);
					}
				});

		// Listen for property visibility changed => swap visibility 
		visibilityChanged.addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> observedBoolean, Boolean oldValue, Boolean newValue) {
						for (int i=0; i<FluxPlotModel.fc.length; i++) {
							if (flareClass.equals(FluxPlotModel.fc[i])) {
								Node node = flareChart.getData().get(i).getNode();
								node.setVisible(!node.isVisible());
							}
						}
					}
				});
		// Listen for property visibility All changed => swap All visibility 
		visibilityAllChanged.addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> observedBoolean, Boolean oldValue, Boolean newValue) {
						for (int i=0; i<FluxPlotModel.fc.length; i++) {
							if (flareClass.equals(FluxPlotModel.fc[i])) {
								Node node = flareChart.getData().get(i).getNode();
								node.setVisible(!node.isVisible());
							}
						}
					}
				});
		
		visibilityAllChanged.set(false);
	}

	public void setFlares(List<Flare> flareList) {
		flares = flareList;
		hasChanged.set(!hasChanged.get());
	}

	public void showHideBy(FlareType fc) {
		flareClass = fc;
		visibilityChanged.set(!visibilityChanged.get());
	}

	public void showHideAll() {
		visibilityAllChanged.set(!visibilityAllChanged.get());
	}

    private void addNodes(List<Flare> flares) {
        if (flares != null) {
            double yMin = ((LogarithmicAxis) flareChart.getYAxis()).getLowerBound() * 0.1;
            for (int i = 0; i < FluxPlotModel.fc.length; i++) {
                Series<Number, Number> series = new Series<Number, Number>();
                for (Flare flare : flares) {
                    if (flare.getCategory().equals(FluxPlotModel.fc[i])) {
                        series.getData().add(new Data<Number, Number>(flare.getEventTime().getTime(), yMin));
                        series.getData().add(new Data<Number, Number>(flare.getEventTime().getTime(), flare.getRealIntensity()));
                        series.getData().add(new Data<Number, Number>(flare.getEventTime().getTime(), yMin));
                    }
                }
                flareChart.getData().add(series);
            }
        }
    }

	private void clearNodes(){
		flareChart.getData().clear();
	}

}
