import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
import org.jfree.chart.ChartFactory;
import org.jfree.data.xy.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

/**
  * Clase del panel contenedor de los graficos.
  */
public class GraphPanel extends JPanel {
   private JFreeChart chart;
   private double maxtime = 30000.0; //ms
   private TimeSeriesCollection datasets;
   
   /** 
   * Crea panel que contiene grafico 2D
   */   
   public GraphPanel(String s){

       chart = createChart(initDataset(), s); //datasets atribute is ready in this line
       ChartPanel chartPanel = new ChartPanel(chart);
       chartPanel.setFillZoomRectangle(true);
       chartPanel.setMouseWheelEnabled(true);
       this.setLayout(new BorderLayout());
       add(chartPanel);
    }
	
	/**
	* Crea Grafico de dos dimensiones.
	* @return grafico 2D.
	*/
   private JFreeChart createChart(XYDataset dataset, String s) {

        JFreeChart result = ChartFactory.createTimeSeriesChart(
            s,
            "Tiempo",
            "Valor",
            dataset,
            false,       //leyend
            false,       //tooltip
            false       //url
        );
        XYPlot plot = result.getXYPlot();
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 30.0));
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(maxtime);  // 60 seconds
        axis = plot.getRangeAxis();
        axis.setAutoRange(true);
        axis.setRange(0.0, 100.0);
        plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.5f));

        result.setBackgroundPaint(Color.white);
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinePaint(Color.lightGray); // lineas verticales
        plot.setRangeGridlinePaint(Color.lightGray);  // lineas horizontales

    return result;
   }
   
   /**
   * Crea serie de datos inicial
   */
   private TimeSeriesCollection initDataset() {
        TimeSeries serie = new TimeSeries("Battery Data");
        //otros si es deseable...
        datasets = new TimeSeriesCollection(serie);
        return datasets;
   }
   
   /**
   * Agrega valores a serie de datos.
   * @param period tiempo actual
   * @param value valor a actualizar
   */
   public void addValue(RegularTimePeriod period, double value){
        datasets.getSeries(0).add(period,value);
   }
   
   /**
   * Resetea los valores de serie de datos
   * @param t tiempo donde se realiza reset de valores 
   */
   public void reset(double t){
        datasets.removeSeries(0);
        datasets.addSeries(new TimeSeries("Battery Data"));
   }
   
   /**
   * Ajusta el tiempo maximo desplegado.
   */
   public void setMaxTime(double time){
      maxtime = time;
   }
   
   /**
   * Actualiza los valores de la serie de tiempo
   * @param value valor a desplegar en eje y
   */
   public void updateGraph(float value){
       Millisecond now = new Millisecond();
       // System.out.println("Now = " + now.toString());
       addValue(now, value);
   }
}