package us.semanter.test.main.vision;

import android.net.Uri;
import android.test.AndroidTestCase;

import org.json.JSONException;
import org.json.JSONObject;
import org.opencv.core.Point;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import us.semanter.app.vision.result.FlattenerResult;
import us.semanter.app.vision.result.OutlineGuess;
import us.semanter.app.vision.util.Polygon;

/**
 * Test the JSON conversion of JSONable objects
 */
public class JSONTest extends AndroidTestCase {
    public void flattenResultJSONTest() {
        Uri testUri = Uri.parse("/test/some/file/path");
        List<OutlineGuess> testOutlines = randomOutlines();

        FlattenerResult result = new FlattenerResult(testUri, testOutlines);

        String json = "";
        try {
            json = result.toJSON().toString();
            FlattenerResult resultFromJSON = new FlattenerResult(new JSONObject(json));
            assertTrue(result.equals(resultFromJSON));
        } catch(JSONException e) {
            assertTrue(e.getMessage(), false);
        }
    }

    public void outlineGuessTest() {
        OutlineGuess guess = new OutlineGuess(randomPolygon(), (float)Math.random());

        String json = "";
        try {
            json = guess.toJSON().toString();
            OutlineGuess guessFromJSON = new OutlineGuess(new JSONObject(json));
            assertTrue(guess.equals(guessFromJSON));
        } catch(JSONException e) {
            assertTrue(e.getMessage(), false);
        }
    }

    public void polygonJSONTest() {

    }

    private Polygon randomPolygon() {
        return new Polygon(randomPoints());
    }

    private OutlineGuess randomOutline() {
        return new OutlineGuess(randomPolygon(), (float)Math.random());
    }

    private List<OutlineGuess> randomOutlines() {
        Random generator = new Random();
        int length = generator.nextInt();

        List<OutlineGuess> outlines = new ArrayList<OutlineGuess>();
        for(int i = 0; i < length; i++) {
            outlines.add(randomOutline());
        }

        return outlines;
    }

    private List<Polygon> randomPolygons() {
        Random generator = new Random();
        int length = generator.nextInt();

        List<Polygon> polygons = new ArrayList<Polygon>();
        for(int i = 0; i < length; i++) {
            polygons.add(randomPolygon());
        }

        return polygons;
    }

    private List<Point> randomPoints() {
        Random generator = new Random();
        int length = generator.nextInt();

        List<Point> points = new ArrayList<Point>();
        for(int i = 0; i < length; i++) {
            points.add(new Point(generator.nextDouble(), generator.nextDouble()));
        }

        return points;
    }
}

@@ -515,16 +515,62 @@
 tCase %7B%0A
+    private final static int NUM_ITEMS = 32;%0A%0A
     publ
@@ -577,17 +577,21 @@
 ic void 
-f
+testF
 lattenRe
@@ -594,28 +594,24 @@
 enResultJSON
-Test
 () %7B%0A       
@@ -1144,17 +1144,21 @@
 ic void 
-o
+testO
 utlineGu
@@ -1160,20 +1160,16 @@
 ineGuess
-Test
 () %7B%0A   
@@ -1587,17 +1587,21 @@
 ic void 
-p
+testP
 olygonJS
@@ -1602,20 +1602,16 @@
 ygonJSON
-Test
 () %7B%0A%0A  
@@ -1948,32 +1948,41 @@
 nerator.nextInt(
+NUM_ITEMS
 );%0A%0A        List
@@ -2283,32 +2283,41 @@
 nerator.nextInt(
+NUM_ITEMS
 );%0A%0A        List
@@ -2565,32 +2565,32 @@
 = new Random();%0A
-
         int leng
@@ -2612,16 +2612,25 @@
 nextInt(
+NUM_ITEMS
 );%0A%0A    

Fix creation of using up all memory with test objects.