package com.flashmath.util;

import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class QuestionUtil {
	public static JSONArray getMockQuestions(String subject) {
		JSONArray questions = new JSONArray();
		for (int i = 0; i < 10; i++) {
			questions.put(generateQuestion(subject));
		}
		
		return questions;
	}
	
	private static JSONObject generateQuestion(String subject) {
		JSONObject question = new JSONObject();
		
		try {
			String questionText = "";
			String answer = "";
			
			if (subject.equalsIgnoreCase("Addition")) {
				int x = getRandomInt(5, 20);
				int y = getRandomInt(5, 20);
				questionText = "('" + String.valueOf(x) + "', '" + String.valueOf(y) + "')";
				answer = String.valueOf(x + y);
			} else if (subject.equalsIgnoreCase("Subtraction")) {
				int x = getRandomInt(5, 20);
				int y = getRandomInt(3, x - 1);
				questionText = "('" + String.valueOf(x) + "', '" + String.valueOf(y) + "')";
				answer = String.valueOf(x - y);
			} else if (subject.equalsIgnoreCase("Multiplication")) {
				int x = getRandomInt(3, 7);
				int y = getRandomInt(3, 7);
				questionText = "('" + String.valueOf(x) + "', '" + String.valueOf(y) + "')";
				int a = x * y;
				answer = String.valueOf(a);
			} else if (subject.equalsIgnoreCase("Division")) {
				int a = getRandomInt(3, 7);
				int y = getRandomInt(3, 7);
				int x = a * y;
				questionText = "('" + String.valueOf(x) + "', '" + String.valueOf(y) + "')";
				answer = String.valueOf(a);
			} else if (subject.equalsIgnoreCase("Fraction")) {
				int m = getRandomInt(3, 5);
				int n = getRandomInt(1, 4);
				int d = getRandomInt(n + 1, 8);
				int ad = d * m;
				questionText = "('" + String.valueOf(n) + "', '" + String.valueOf(d) + "', '" + String.valueOf(ad) + "')";
				answer = String.valueOf(n * m);
			}
			
			question.put("explanation", " ");
			question.put("text", questionText);
			question.put("answer", answer);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return question;
	}
	
	private static int getRandomInt(int min, int max) {
	    Random rand = new Random();
	    return rand.nextInt((max - min) + 1) + min;
	}
}

@@ -668,39 +668,32 @@
 %09%09questionText =
- %22('%22 +
  String.valueOf(
@@ -690,36 +690,33 @@
 g.valueOf(x) + %22
-', '
+ 
 %22 + String.value
@@ -712,39 +712,32 @@
 tring.valueOf(y)
- + %22')%22
 ;%0A%09%09%09%09answer = S
@@ -898,32 +898,24 @@
 estionText =
- %22('%22 + 
 String.value
@@ -915,36 +915,33 @@
 g.valueOf(x) + %22
-', '
+ 
 %22 + String.value
@@ -937,39 +937,32 @@
 tring.valueOf(y)
- + %22')%22
 ;%0A%09%09%09%09answer = S
@@ -1117,39 +1117,32 @@
 %09%09questionText =
- %22('%22 +
  String.valueOf(
@@ -1139,36 +1139,33 @@
 g.valueOf(x) + %22
-', '
+ 
 %22 + String.value
@@ -1165,31 +1165,24 @@
 g.valueOf(y)
- + %22')%22
 ;%0A%09%09%09%09int a 
@@ -1369,39 +1369,32 @@
 %09%09questionText =
- %22('%22 +
  String.valueOf(
@@ -1395,28 +1395,25 @@
 lueOf(x) + %22
-', '
+ 
 %22 + String.v
@@ -1421,23 +1421,16 @@
 lueOf(y)
- + %22')%22
 ;%0A%09%09%09%09an
@@ -1651,15 +1651,8 @@
 xt =
- %22('%22 +
  Str
@@ -1665,28 +1665,25 @@
 lueOf(n) + %22
-', '
+ 
 %22 + String.v
@@ -1699,12 +1699,9 @@
  + %22
-', '
+ 
 %22 + 
@@ -1722,15 +1722,8 @@
 (ad)
- + %22')%22
 ;%0A%09%09

Conform to space separated question format