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

import ch.psi.pshell.plot.LinePlotSeries;
import ch.psi.pshell.plot.RangeSelectionPlot.RangeSelection;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.Convert;
import ch.psi.utils.IO;
import ch.psi.utils.State;
import ch.psi.utils.swing.SwingUtils;
import ch.psi.psaf.Task;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 */
public class Pearl extends Panel {

    public Pearl() {
        initComponents();
        getApp().addTask(scan1);
        getApp().addTask(scan2);
    }

    Task scan1 = new Task() {
        @Override
        protected Object execute() throws Exception {
            return true;
        }
    };

    Task scan2 = new Task() {
        @Override
        protected Object execute() throws Exception {
            return true;
        }
    };

    @Override
    public void onStateChange(State state, State former) {
        setEnabled(state == State.Ready);
    }

    @Override
    public void onExecutedFile(String fileName, Object result) {
        String script = IO.getPrefix(fileName);
        switch (script) {
            case "InitialScan":
                if (result instanceof Exception) {
                    //SwingUtils.showMessage(getComponent(), "Error in " + fileName, exception.getMessage());
                } else {
                    double[] data = (double[]) Convert.toDouble(result);
                    //double[] data = new double[]{1.0,1.0,1.0,1.0,1.0};
                    LinePlotSeries series = new LinePlotSeries("Data");
                    rangeSelectionPanel.setSeries(series);
                    series.setData(data);
                }
                break;
            case "DetailedScan":
                break;
        }
        updateButtons();
    }

    @Override
    public void setEnabled(boolean value) {
        super.setEnabled(value);
        rangeSelectionPanel.setEnabled(value);
        updateButtons();
    }

    void updateButtons() {
        buttonScan1.setEnabled(isEnabled());
        buttonScan2.setEnabled(isEnabled() && rangeSelectionPanel.getPlot().getSelectedRanges().length > 0);
    }

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

        buttonAbort = new javax.swing.JButton();
        buttonScan1 = new javax.swing.JButton();
        rangeSelectionPanel = new ch.psi.pshell.swing.RangeSelectionPanel() {
            protected void onSeriesChanged() {
                updateButtons();
            }
            protected void onSelectionChanged() {
                updateButtons();
            }
        }
        ;
        buttonScan2 = new javax.swing.JButton();

        buttonAbort.setText("Abort");
        buttonAbort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAbortActionPerformed(evt);
            }
        });

        buttonScan1.setText("Initial Scan");
        buttonScan1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonScan1ActionPerformed(evt);
            }
        });

        buttonScan2.setText("Detailed Scan");
        buttonScan2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonScan2ActionPerformed(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()
                .addContainerGap()
                .addComponent(rangeSelectionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buttonScan2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonScan1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(buttonAbort)
                .addGap(54, 54, 54))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonAbort, buttonScan1, buttonScan2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonScan1)
                    .addComponent(buttonAbort))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonScan2)
                .addGap(18, 18, 18)
                .addComponent(rangeSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAbortActionPerformed
        abort();
    }//GEN-LAST:event_buttonAbortActionPerformed

    private void buttonScan1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonScan1ActionPerformed
        try {
            //getApp().startTask(task);            
            rangeSelectionPanel.clear();
            runAsync("InitialScan");
        } catch (Exception ex) {
            SwingUtils.showException(this, ex);
        }

    }//GEN-LAST:event_buttonScan1ActionPerformed

    private void buttonScan2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonScan2ActionPerformed
        try {
            RangeSelection[] ranges = rangeSelectionPanel.getRangesOrdered();
            HashMap args = new HashMap();
            args.put("ranges", ranges);
            runAsync("DetailedScan", args);
        } catch (Exception ex) {
            SwingUtils.showException(this, ex);
        }
    }//GEN-LAST:event_buttonScan2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAbort;
    private javax.swing.JButton buttonScan1;
    private javax.swing.JButton buttonScan2;
    private ch.psi.pshell.swing.RangeSelectionPanel rangeSelectionPanel;
    // End of variables declaration//GEN-END:variables
}
