package com.ke2g.cued_recall;

import android.util.Log;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * Created on 18/04/2015.
 */
public class CuedRecallIntent {

    public static final String TAG = LoginActivity.class.getSimpleName();

    private static boolean isPointEqual(Point source, Point target, int tolerance) {
        boolean isEqual = false;
        //Log.d(TAG,"Point (x,y): " + source.getX() + ","+ source.getY() + " compared with: "+target.getX()+","+target.getY());
        //Log.d(TAG,"distance: "+((target.getX() - source.getX())*(target.getX() - source.getX()) + (target.getY() - source.getY())*(target.getY() - source.getY())) + " tolerance " + (tolerance*tolerance));
        //(x - center_x)^2 + (y - center_y)^2 < radius^2.
        if ((target.getX() - source.getX()) * (target.getX() - source.getX()) + (target.getY() - source.getY()) * (target.getY() - source.getY()) < (tolerance * tolerance)) {
            isEqual = true;
        }
        return isEqual;
    }

    public static boolean arePointsEqual(ArrayList<Point> source, ArrayList<Point> target, int tolerance) {
        Log.d(TAG, "tolerance: " + tolerance);
        boolean isEqual = true;
        if (source != null && target != null) {
            if (source.size() == target.size()) {
                for (int i = 0; i < source.size(); i++) {
                    if (!isPointEqual(source.get(i), target.get(i), tolerance)) {
                        isEqual = false;
                    }
                    //Log.d(MainActivity.TAG, "Point " + i + " is equal? " + isPointEqual(source.get(i), target.get(i), tolerance));
                }
            } else {
                isEqual = false;
            }
        } else {
            isEqual = false;
        }
        return isEqual;
    }

    public static String createHash(ArrayList<Point> points) {
        String hash = "";
        String grids = getGrid(points);
        hash = BCrypt.hashpw(grids, BCrypt.gensalt());
        Log.d(TAG, "Grid text " + hash);

        return hash;
    }

    private static String getGrid(ArrayList<Point> points) {
        String grids = "";
        DecimalFormat df = new DecimalFormat("##");
        df.setRoundingMode(RoundingMode.DOWN);

        for (int i = 0; i < points.size(); i++) {
            int x = points.get(i).getX();
            int y = points.get(i).getY();
            Log.d(TAG, "Point " + i + " grid: " + df.format(x / 8) + "" + df.format(y / 8));
            grids += df.format(x / 8) + "" + df.format(y / 8);
        }

        Log.d(TAG, "Grid text " + grids);
        return grids;
    }

    public static boolean areHashEqual(ArrayList<Point> points, String userHash, int tolerance) {
        String newGrid = getGrid(points);
        Log.d(TAG, "Grid compare: " + newGrid);
        if (BCrypt.checkpw(newGrid, userHash)){
            return true;
        }else{
            return false;
        }
    }


}

@@ -1884,32 +1884,46 @@
 st%3CPoint%3E points
+,int tolerance
 ) %7B%0A        Stri
@@ -1929,21 +1929,16 @@
 ing hash
- = %22%22
 ;%0A      
@@ -1960,32 +1960,43 @@
 = getGrid(points
+, tolerance
 );%0A        hash 
@@ -2164,16 +2164,31 @@
 %3E points
+, int tolerance
 ) %7B%0A    
@@ -2564,33 +2564,41 @@
 = df.format(x / 
-8
+tolerance
 ) + %22%22 + df.form
@@ -2604,17 +2604,25 @@
 mat(y / 
-8
+tolerance
 );%0A     
@@ -2835,16 +2835,27 @@
 d(points
+, tolerance
 );%0A     
@@ -2905,20 +2905,23 @@
         
-if (
+return 
 BCrypt.c
@@ -2949,86 +2949,9 @@
 ash)
-)%7B%0A            return true;%0A        %7Delse%7B%0A            return false;%0A        %7D
+;
 %0A   

Fix some bugs