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


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class MouseData {

    private static int[][] mouseCoordinates = new int[1000][2];
    private static int counter = 0;
    
    

    public static int addNewCoordinate(int x, int y) {
        if (counter == mouseCoordinates.length) {
            return 0;
        }
        mouseCoordinates[counter][0] = x;
        mouseCoordinates[counter][1] = y;
        counter++;
        return 1;
    }
    
    public static void processData(){
        //show10data();//
        //derivation();
        //show10data();//
        derivation();
        saveDataToText();
        //show10data();//
    }
    
    private static void derivation(){
        for (int i = 0; i < counter-2; i++) {
            mouseCoordinates[i][0] = Math.abs(mouseCoordinates[i+2][0] - mouseCoordinates[i+1][0]) -
                                     Math.abs(mouseCoordinates[i+1][0] - mouseCoordinates[i][0]);
            mouseCoordinates[i][1] = Math.abs(mouseCoordinates[i+2][1] - mouseCoordinates[i+1][1]) -
                                     Math.abs(mouseCoordinates[i+1][1] - mouseCoordinates[i][1]);
        }
    }

    private static void saveDataToText() {
        
        File MouseData = new File("MouseData.txt");
        
        String s1,s2,s3;
         

        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(MouseData));
            for (int i = 0; i < counter-2; i++) {
                //s1 = String.format("%8s", Integer.toBinaryString(mouseCoordinates[i][0] & 0xFF)).replace(' ', '0');
                //s2 = s1.substring(7);
                //s1 = String.format("%8s", Integer.toBinaryString(mouseCoordinates[i][1] & 0xFF)).replace(' ', '0');
                //s3 = s1.substring(7);
                
                //writer.write(mouseCoordinates[i][0] + "/" + mouseCoordinates[i][1] + 
                        //"\t" + s2 + "/" + s3 + System.lineSeparator());
                writer.write(mouseCoordinates[i][0]+ System.lineSeparator() + mouseCoordinates[i][1] + System.lineSeparator());
                
            }
            writer.close();
        } catch (IOException ex) {
            System.out.println("cant write");
            ex.printStackTrace();
        }
        
        //save binary data separately for late processing
        /*File mouseDataBinary = new File("MDBinary.txt");
        
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(mouseDataBinary));
            for (int i = 0; i < counter; i++) {
                s1 = String.format("%8s", Integer.toBinaryString(mouseCoordinates[i][0] & 0xFF)).replace(' ', '0');
                s2 = s1.substring(7);
                s1 = String.format("%8s", Integer.toBinaryString(mouseCoordinates[i][1] & 0xFF)).replace(' ', '0');
                s3 = s1.substring(7);
                
                writer.write(s2 + s3 + System.lineSeparator());
                
            }
            writer.close();
        } catch (IOException ex) {
            System.out.println("cant write");
            ex.printStackTrace();
        }*/
        
    }
    
    private static void show10data(){
        /*for (int i = 0; i < 10; i++) {
            System.out.print(mouseCoordinates[i][0] + "/" + mouseCoordinates[i][1] + "\n");
        }*/
        System.out.print(mouseCoordinates[counter-2][0] + "/" + mouseCoordinates[counter-2][1] + "\n");
        System.out.print(mouseCoordinates[counter-1][0] + "/" + mouseCoordinates[counter-1][1] + "\n");
        System.out.print("\n\n");
    }

}
