/*
 * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
 */

import ch.psi.plot.LinePlot;
import ch.psi.plot.LinePlotSeries;
import ch.psi.plot.Plot;
import ch.psi.plot.utils.SwingUtils;
import ch.psi.plot.xy.generator.Sinus;
import ch.psi.plot.xy.generator.XYGenerator;
import javax.swing.JFrame;

/**
 * Test for the LinePlot class
 */
public class LinePlotTest {

    public static void main(final String[] args) throws InterruptedException {
        //for(int i=0;i<10;i++)
        {
            //LinePlot linePlot = ServiceLoader.load(LinePlot.class).iterator().next();                         
            //LinePlot linePlot = new ch.psi.plot.jlchart.LinePlot();
            //LinePlot linePlot = new ch.psi.plot.jfree.LinePlot();
            LinePlot linePlot = new ch.psi.plot.javafx.LinePlot() ;

            linePlot.setTitle("Title");

            //linePlot.setRangeX(0,10000);
            //linePlot.setRangeY(0,10);
            //linePlot.setData(getDataFromGenerator(1));
            addDataFromGenerator(0, 10, 1000, linePlot);

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(linePlot.getChartPanel());
            frame.pack();
            SwingUtils.centerOnScreen(frame);
            frame.setVisible(true);
            frame.requestFocus();

            // Thread.sleep(2000);
            //addDataFromGenerator(0, 10, 1000, linePlot);
            //linePlot.getAxis(Plot.AxisId.X).setRange(100,500);
            //linePlot.getAxis(Plot.AxisId.Y).setRange(0,5);
            //Thread.sleep(2000);
            //linePlot.getAxis(Plot.AxisId.X).setAutoRange();
            //linePlot.getAxis(Plot.AxisId.Y).setAutoRange();
        }

        Thread.sleep(3600000);
    }

    private static void addDataFromGenerator(final int sleep, final int series, final int size, final LinePlot plot) {
        final String seriesBaseName = "Generator Data";
        final XYGenerator generator = new XYGenerator(new Sinus(0.01), 0, 1, size, false);

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                long start = System.currentTimeMillis();
                for (int t = 0; t < series; t++) {
                    // Reset generator
                    generator.reset();

                    String seriesName = seriesBaseName + " - " + t;
                    LinePlotSeries series = new LinePlotSeries(seriesName);
                    plot.addSeries(series);

                    double[] xy;
                    while ((xy = generator.getNewPoint()) != null) {
                        series.appendData(xy[0], xy[1] + t); // Add t to the y value to see the plotted line
//						data.getSeries(seriesName).add(xy[0], xy[1]+t,false); // No notification send - Update of GUI will be performed periodically

                        try {
                            Thread.sleep(sleep);
                        } catch (InterruptedException e) {
                        }
                    }
                }

                System.out.println("DONE:  " + (System.currentTimeMillis() - start));
            }
        });
        t.start();
    }

}
