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

import ch.psi.pshell.epics.Epics;
import ch.psi.pshell.imaging.Overlay;
import ch.psi.pshell.imaging.Overlays;
import ch.psi.pshell.imaging.Pen;
import ch.psi.pshell.imaging.Renderer;
import ch.psi.pshell.imaging.RendererListener;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.State;
import java.awt.Color;
import java.awt.Point;
import java.util.logging.Level;

/**
 *
 */
public class Microscope extends Panel {
    public Microscope() {
        initComponents();
    }
    
     //Overridable callbacks
    @Override
    public void onInitialize(int runCount) {        

        final Overlay overlay = new Overlays.Ellipse(new Pen(Color.RED), new Point(10,10), new Point(50,50));
        renderer.addOverlay(overlay);
        overlay.setMovable(true);                
        overlay.setSolid(false);
        
        renderer.addListener(new RendererListener() {
            public void onMouseDragged(Renderer renderer, Point p) {
                //sendOutput(p.toString());                
                System.out.println((p.x - renderer.getImageSize().width/2) + "  -  " + (p.y -  renderer.getImageSize().height/2));
            }        
                
            public void onMoveFinished(Renderer renderer, Overlay ov) {
                if (ov == overlay){
                    System.out.println(ov.getPosition().toString());
                }
            }
        } );     
        
        
    }

    @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() {
    }

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

        renderer = new ch.psi.pshell.imaging.Renderer();
        jButton1 = new javax.swing.JButton();
        motorPanel1 = new ch.psi.pshell.swing.MotorPanel();
        processVariablePanel1 = new ch.psi.pshell.swing.ProcessVariablePanel();
        buttonDefineBeam = new javax.swing.JButton();
        processVariablePanel2 = new ch.psi.pshell.swing.ProcessVariablePanel();
        processVariablePanel3 = new ch.psi.pshell.swing.ProcessVariablePanel();

        renderer.setAutoscrolls(true);
        renderer.setDeviceName("src1");
        renderer.setMode(ch.psi.pshell.imaging.RendererMode.Fixed);
        renderer.setShowStatus(false);

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

        motorPanel1.setDecimals(2);
        motorPanel1.setDeviceName("motor");
        motorPanel1.setDoubleBuffered(false);
        motorPanel1.setShowTitle(true);

        processVariablePanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("pe"));
        processVariablePanel1.setDeviceName("pe");
        processVariablePanel1.setName(""); // NOI18N
        processVariablePanel1.setShowSlider(false);
        processVariablePanel1.setShowTitle(true);

        buttonDefineBeam.setText("Define Beam");
        buttonDefineBeam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDefineBeamActionPerformed(evt);
            }
        });

        processVariablePanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("pv"));
        processVariablePanel2.setDecimals(2);
        processVariablePanel2.setDeviceName("pv");
        processVariablePanel2.setName(""); // NOI18N
        processVariablePanel2.setShowSlider(false);
        processVariablePanel2.setShowTitle(true);

        processVariablePanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("cv"));
        processVariablePanel3.setDecimals(2);
        processVariablePanel3.setDeviceName("cv");
        processVariablePanel3.setName(""); // NOI18N
        processVariablePanel3.setShowSlider(false);
        processVariablePanel3.setShowTitle(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(renderer, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(processVariablePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(processVariablePanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(processVariablePanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(motorPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(123, 123, 123)
                        .addComponent(buttonDefineBeam)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(renderer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(5, 5, 5))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(motorPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(processVariablePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(processVariablePanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(processVariablePanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(buttonDefineBeam)
                .addGap(26, 26, 26))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try{
            new Thread(()->{  
                try {
                    Point p = renderer.waitClick(10000);
                    System.out.println(p);
                } catch (Exception ex) {                    
                }
                jButton1.setEnabled(true);
                
            }).start();
            jButton1.setEnabled(false);
        } catch (Exception ex){            
            showException(ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    
    Overlay beam;
    
    private void buttonDefineBeamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDefineBeamActionPerformed
        try{
            Pen pen = new Pen(Color.RED);
            final Overlay selection  = new Overlays.Rect(pen);
            
            renderer.addListener(new RendererListener() {
                @Override
                public void onSelectionFinished(Renderer renderer, Overlay overlay) {
                    if (overlay == selection){
                        try {
                            if (overlay.getLength() > 0) {
                                beam = overlay.copy();
                                beam.setPen(pen);
                                beam.setSolid(false);
                                beam.setMovable(true);
                                //renderer.addOverlay(beam);                            
                            }
                        } catch (Exception ex) {
                            getLogger().log(Level.WARNING, null, ex);
                        } finally {
                            renderer.removeListener(this);
                        }
                    }
                }
                @Override
                public void onSelectionAborted(Renderer renderer, Overlay overlay) {
                    if (overlay == selection){
                        renderer.removeListener(this);
                    }
                }
            });
            renderer.removeOverlay(beam);
            renderer.startSelection(selection);
            renderer.requestFocus();
            
        } catch (Exception ex){
            showException(ex);
        }
        
    }//GEN-LAST:event_buttonDefineBeamActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonDefineBeam;
    private javax.swing.JButton jButton1;
    private ch.psi.pshell.swing.MotorPanel motorPanel1;
    private ch.psi.pshell.swing.ProcessVariablePanel processVariablePanel1;
    private ch.psi.pshell.swing.ProcessVariablePanel processVariablePanel2;
    private ch.psi.pshell.swing.ProcessVariablePanel processVariablePanel3;
    private ch.psi.pshell.imaging.Renderer renderer;
    // End of variables declaration//GEN-END:variables
}
