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

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.imaging.Source;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.State;
import java.awt.Color;
import java.awt.Point;

/**
 *
 */
public class Image extends Panel {
    public Image() {
        initComponents();
    }

     //Overridable callbacks
    @Override
    public void onInitialize(int runCount) {        

        final Overlay overlay = new Overlays.Rect(new Pen(Color.RED), new Point(10,10), new Point(50,50));
        renderer.addOverlay(overlay);
        overlay.setMovable(true);                
        overlay.setSolid(true);
        
        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();

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

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(renderer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addContainerGap(430, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(renderer, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
    }// </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

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