
package my10;
/**
 *
 * @author Viliam Krizan
 */

import java.awt.BorderLayout;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class MouseES {
    
    private JFrame frame;
    private JPanel mousePanel;
    private JLabel statusBar;
    private Robot robot;
    int offsetX;
    int offsetY;
    boolean mouseMoveFlag;
    private MouseData mouseData = new MouseData();
    File txtData = new File("MouseDataRobot.txt");
    BufferedWriter writer;
    int counter = 0;
    int[][] entropy = new int[2000][2];
    int c1,c2;
    

    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(500, 500);
        frame.setVisible(true);
        
        offsetX = -(mousePanel.getSize().width/2);
        offsetY = -(mousePanel.getSize().height/2);
        mouseMoveFlag = false;
        
        try {
            writer = new BufferedWriter(new FileWriter(txtData));
        } catch (IOException ex) {
        }
    }
    
    
    public static void main(String[] args) {
        MouseES m = new MouseES();
    }
    
    
    
   
    
    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;
            
            if(counter == 2000){
                try {
                    writer.close();
                } catch (IOException ex) {
                }
                analysis();
                System.exit(0);
            }
            counter = counter + 1;
            
            statusBar.setText(String.format("coordinations:  %d, %d", x + offsetX , y + offsetY ));
            //acumulator.saveToPool(mouseData.processData(x + offsetX, y + offsetY),actualPool);
            //int[] d = mouseData.processData(x + offsetX, y + offsetY);
            //entropy[0] = mouseData.processData(x + offsetX, y + offsetY);
            
            saveDataToText(mouseData.processData(x + offsetX, y + offsetY));
            //System.out.print("x: " + (x + offsetX) + " derv: " + d[0] + "\n");
            
            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);
    }
    
    private void saveDataToText(int[] data) {

        try {
            writer.write(data[0]+ System.lineSeparator() + data[1] + System.lineSeparator());
        } catch (IOException ex) {
        }
        
    }
    
    private void analysis(){
        int[] result = new int[9];
        for (int i = 0; i < 1998; i++) {
            if (isBetween(entropy[i][0], -160, -120)) {
                result[0]++;
            } else if (isBetween(entropy[i][0], -121, -80)) {
                result[1]++;
            } else if (isBetween(entropy[i][0], -81, -40)) {
                result[2]++;
            } else if (isBetween(entropy[i][0], -41, 0)) {
                result[3]++;
            } else if (isBetween(entropy[i][0], -1, 40)) {
                result[4]++;
            } else if (isBetween(entropy[i][0], 39, 80)) {
                result[5]++;
            } else if (isBetween(entropy[i][0], 79, 120)) {
                result[6]++;
            } else if (isBetween(entropy[i][0], 119, 160)) {
                result[7]++;
            } else  {
                result[8]++;
            }
        }
        
        for (int i = 0; i < 1998; i++) {
            if (isBetween(entropy[i][1], -160, -120)) {
                result[0]++;
            } else if (isBetween(entropy[i][1], -121, -80)) {
                result[1]++;
            } else if (isBetween(entropy[i][1], -81, -40)) {
                result[2]++;
            } else if (isBetween(entropy[i][1], -41, 0)) {
                result[3]++;
            } else if (isBetween(entropy[i][1], -1, 40)) {
                result[4]++;
            } else if (isBetween(entropy[i][1], 39, 80)) {
                result[5]++;
            } else if (isBetween(entropy[i][1], 79, 120)) {
                result[6]++;
            } else if (isBetween(entropy[i][1], 119, 160)) {
                result[7]++;
            } else  {
                result[8]++;
            }
        }
        
        for (int i = 0; i < 9; i++) {
            System.out.println("result: " + i + "\t" + result[i]);
            
        }
    }
    
    public static boolean isBetween(int x, int lower, int upper) {
        return lower <= x && x <= upper;
    }
        
        
}
