/*
 * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
 */
import ch.psi.plot.MatrixPlotSeries;
import ch.psi.plot.Plot;
import ch.psi.plot.xyz.generator.Gauss2D;

//import ch.psi.sls.xasec.data.DataSet;
//import ch.psi.sls.xasec.data.DataSetUtils;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
 * Test case for MatrixPlot class
 *
 */
public class MatrixPlotTest {

    final static int sleepTime = 0;
    final static double startX = 10;
    final static double stepSizeX = 1;
    final static int stepsX = 100;
    final static double startY = 300;
    final static double stepSizeY = 2;
    final static int stepsY = 200;
    final static MatrixPlotSeries data = new MatrixPlotSeries("", startX, startX + stepsX * stepSizeX, stepsX + 1, startY, startY + stepsY * stepSizeY, stepsY + 1);
    final static Gauss2D generator = new Gauss2D(50, 500, 40, 800, 2);

    public static void main(final String[] args) throws InterruptedException {
        final ch.psi.plot.MatrixPlot plot = new ch.psi.plot.jzy3d.MatrixPlot();
        //final ch.psi.plot.MatrixPlot plot=new ch.psi.plot.jfree.MatrixPlot();

        //plot.getAxis(Plot.AxisId.Z).setRange(0.0, 4.0);
        //final XYZGenerator generator = new XYZGenerator(new Sinus2D(2, 4), startX, stepSizeX, stepsX, startY, stepSizeY, stepsY, false);        
        //final SphericalWave generator = new SphericalWave(50, 1000, 10, 200, 1);     
        plot.setTitle("Matrix Plot Test");
        //plot.getAxis(Plot.AxisId.Z).setRange(2.0, 3.0);
        //plot.addSeries(data);

        final JFrame frame = new JFrame("");
        frame.setContentPane(plot.getChartPanel());
        frame.pack();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // Close application if frame is closed

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(true);
                //runnable.run();
            }
        });
        //plot.getAxis(Plot.AxisId.Z).setRange(2.0, 4.0);
        //plot.getAxis(Plot.AxisId.X).setRange(40, 60);
        //plot.getAxis(Plot.AxisId.Y).setRange(400, 600);

        Thread.sleep(1000);
        plot.addSeries(data);

        Thread t = new Thread(runnable);
        t.start();

        /*
         plot.setUpdatesEnabled(false);
         for (int i=0;i<20;i++){
         Thread.sleep(1000);
         plot.requestUpdate();
         }        
         while(t.isAlive())
         Thread.sleep(10);
         plot.requestUpdate();
         */
        /*
         Thread.sleep(2000);
         //plot.getAxis(Plot.AxisId.Z).setRange(2.0, 3.0);
         plot.getAxis(Plot.AxisId.X).setRange(40, 60);
         plot.getAxis(Plot.AxisId.Y).setRange(400, 600);
         Thread.sleep(5000);
         plot.getAxis(Plot.AxisId.X).setAutoRange();
         plot.getAxis(Plot.AxisId.Y).setAutoRange();
         //plot.getAxis(Plot.AxisId.Z).setAutoRange();
        
         Thread.sleep(5000);
         //plot.removeSeries(data);
         plot.clear();
         */
        Thread.sleep(360000);

    }

    static Runnable runnable = new Runnable() {

        @Override
        public void run() {
            long start = System.currentTimeMillis();
            double[] xyz;
            //while ((xyz = generator.getNewPoint()) != null) {
            //      data.appendData(xyz[0], xyz[1], xyz[2]);

            for (double y = startY; y <= startY + stepsY * stepSizeY; y += stepSizeY) {
                for (double x = startX; x <= startX + stepsX * stepSizeX; x += stepSizeX) {
                    data.appendData(x, y, generator.generate(x, y) + 2);

                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                    }
                }
            }
            System.out.println("------" + (System.currentTimeMillis() - start));
//				}
        }
    };
}
