
package fortuna.entropySources.mouse;

/**
 *
 * @author Viliam Krizan
 */
import fortuna.entropySources.EntropySource;
import java.awt.BorderLayout;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class MouseES extends EntropySource{
    
    private JFrame frame;
    private JPanel mousePanel;
    private JLabel statusBar;
    private Robot robot;
    int offsetX;
    int offsetY;
    boolean mouseMoveFlag;
    private MouseData mouseData = new MouseData();

    public MouseES() {
        
        frame = new JFrame("Mouse Entropy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mousePanel = new JPanel();
        //setUndecorated(true);
        //setOpacity(0.5F);
        
        frame.add(mousePanel, BorderLayout.CENTER);
        
        statusBar = new JLabel("default");
        frame.add(statusBar, BorderLayout.NORTH);
        
        MouseES.HandlerClass handler = new MouseES.HandlerClass();
        
        mousePanel.addMouseListener(handler);
        mousePanel.addMouseMotionListener(handler);
        
        try {
            robot = new Robot();
        } catch (Exception e) {
        }
        frame.setSize(300, 300);
        frame.setVisible(true);
        
        offsetX = -(mousePanel.getSize().width/2);
        offsetY = -(mousePanel.getSize().height/2);
        mouseMoveFlag = false;
    }
    
    
    
    @Override
    public void run(){
        super.run();
        while(running){}
        System.exit(0);
    }
    
   
    
    private class HandlerClass implements  MouseListener, MouseMotionListener{
        
        @Override
        public void mouseClicked(MouseEvent e) {
            if(mouseMoveFlag == false){
                robot.mouseMove(mousePanel.getLocationOnScreen().x + mousePanel.getWidth()/2, 
                                mousePanel.getLocationOnScreen().y + mousePanel.getHeight()/2);
                mouseMoveFlag = true;
            } else{
                mouseMoveFlag = false;
            }
        }

        @Override
        public void mousePressed(MouseEvent e) { 
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            
        }

        @Override
        public void mouseExited(MouseEvent e) {
            
        }
        
        @Override
        public void mouseDragged(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            
            if(!mouseMoveFlag){
                return;
            }
            int x = e.getX();
            int y = e.getY();
            mouseMoveFlag = false;
            
            statusBar.setText(String.format("coordinations:  %d, %d", x + offsetX , y + offsetY ));
            acumulator.saveToPool(mouseData.processData(x + offsetX, y + offsetY),actualPool);
            poolCounterIncrement();
            
            offsetX = x - (mousePanel.getSize().width/2) + offsetX;
            offsetY = y - (mousePanel.getSize().height/2) + offsetY;
            
            centering();
            mouseMoveFlag = true;
        }
    }
    
    private void centering(){
        robot.mouseMove(mousePanel.getLocationOnScreen().x + mousePanel.getWidth()/2, 
                            mousePanel.getLocationOnScreen().y + mousePanel.getHeight()/2);
    }
        
}
