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

import ch.psi.pshell.plot.LinePlotSeries;
import ch.psi.pshell.plot.Plot;
import ch.psi.pshell.scan.Scan;
import ch.psi.pshell.scan.ScanListener;
import ch.psi.pshell.scan.ScanRecord;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.Arr;
import ch.psi.utils.ArrayProperties;
import ch.psi.utils.Convert;
import ch.psi.utils.State;
import java.awt.Color;
import java.util.ArrayList;

/**
 *
 */
public class TestPlot extends Panel {

    LinePlotSeries series = new LinePlotSeries("Values");    
    public TestPlot() {
        initComponents();    
        plot.addSeries(series);
        plot.getAxis(Plot.AxisId.X).setLabel("sample");
        plot.getAxis(Plot.AxisId.Y).setLabel("value");
        
                
    }
    
    @Override
    public void onStart() {
        super.onStart();
        getContext().addScanListener(scanListener);
    }

    @Override
    public void onStop() {        
        getContext().removeScanListener(scanListener);
        super.onStop();
        
    }
    

    
    
    ScanListener scanListener = new ScanListener() {
        @Override
        public void onScanStarted(Scan scan, String plotTitle) {        
            if (getScriptName().equals(getContext().getExecutionPars().getName())){                
                plot.removeMarker(null);
                series.clear();                
            }            
        }

        @Override
        public void onNewRecord(Scan scan, ScanRecord record) {
            if (getScriptName().equals(getContext().getExecutionPars().getName())){                
                series.appendData((Double)record.getPositions()[0], (Double)record.getValues()[0]);
            }
        }

        @Override
        public void onScanEnded(Scan scan, Exception ex) {
        }
    };
    
     //Overridable callbacks
    @Override
    public void onInitialize(int runCount) {

    }

    @Override
    public void onStateChange(State state, State former) {

    }

    @Override
    public void onExecutedFile(String fileName, Object result) {

    }

    
    //Callback to perform update - in event thread
    @Override
    protected void doUpdate() {
    }
    
    String getScriptName(){
        return "TestPlot";
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        panelPlot = new javax.swing.JPanel();
        plot = new ch.psi.pshell.plot.LinePlotJFree();
        spinner = new javax.swing.JSpinner();

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        panelPlot.setLayout(new java.awt.BorderLayout());

        plot.setTitle("");
        panelPlot.add(plot, java.awt.BorderLayout.CENTER);

        spinner.setModel(new javax.swing.SpinnerNumberModel(0.0d, null, 10.0d, 1.0d));
        spinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(spinner))
                .addGap(18, 18, 18)
                .addComponent(panelPlot, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelPlot, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(86, 86, 86)))
                .addGap(0, 0, 0))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            ArrayList args = new ArrayList();
            
            spinner.setValue(0.0);
            runAsync(getScriptName(), args).handle((ret, ex) -> {
                if (ex != null) {
                    ex.printStackTrace();
                } else {  
                    spinner.setValue(ret);                                        
                }       
                return ret;
            });                         
        } catch (Exception ex) {
            showException(ex);
        }            
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void spinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerStateChanged
        try {
            plot.removeMarker(null);
            plot.addMarker((Double)spinner.getValue(), Plot.AxisId.X, "Selection",Color.BLUE);
        } catch (Exception ex) {
            showException(ex);
        }   
    }//GEN-LAST:event_spinnerStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel panelPlot;
    private ch.psi.pshell.plot.LinePlotJFree plot;
    private javax.swing.JSpinner spinner;
    // End of variables declaration//GEN-END:variables
}
