package com.phoenix.eteacher.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;

public class SampleQuestions {
	public static Map<Integer, Question> questions = new HashMap<Integer, Question>();
	private static boolean isLoaded = false;
	
	public static boolean isCorrect(Integer index, String ans){
		String rightAnswer = questions.get(index).answer;
		ans = ans.replaceAll("\\s*", "");
		if (rightAnswer != null && rightAnswer.equalsIgnoreCase(ans)){
			return true;
		}
		else{
			return false;
		}
	}
	
	public static int size(){
		return questions.size();
	}
	
	public static String getCorrectAnswer(Integer index){
		return questions.get(index).answer;
	}
	
	public static Question getQuestion(Integer index){
		return questions.get(index);
	}
	
	public static String getReadableQuestion(Integer index){
		return questions.get(index).getReadableQuestion(index);
	}
	
	

	public static void load(Context context) {
		if (!isLoaded){
			SimpleResourceHelper helper = new SimpleResourceHelper(context);
			String[] paths = helper.getResourcePaths(new String[]{"tests.txt"});
			for(String path : paths){
				BufferedReader reader = null;
				try{
					reader = new BufferedReader(new FileReader(path));
					String line = reader.readLine();
					int count = 0;
					while(line != null){
						questions.put(count++, Question.createQuestion(line));
						line = reader.readLine();
					}
				}
				catch(Exception ex){
					ex.printStackTrace();
				}
				finally{
					if(reader != null){
						try {
							reader.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			isLoaded = true;
		}
	}
}

@@ -120,16 +120,45 @@
 ption;%0D%0A
+import java.util.ArrayList;%0D%0A
 import j
@@ -198,16 +198,43 @@
 il.Map;%0D
+%0Aimport java.util.regex.*;%0D
 %0A%0D%0Aimpor
@@ -487,16 +487,122 @@
  ans)%7B%0D%0A
+%09%09if (questions.get(index).type == Question.TYPE_APPLITION)%0D%0A%09%09%09return isCorrectApplication(index, ans);%0D%0A
 %09%09String
@@ -801,32 +801,683 @@
 e;%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A
+%09public static boolean isCorrectApplication(Integer index, String ans)%7B%0D%0A%09%09String rightAnswer = questions.get(index).answer;%0D%0A//%09%09ans = ans.replaceAll(%22%5C%5Cs*%22, %22%22);%0D%0A%09%09Pattern p = Pattern.compile(%22%5B%5C%5Cu4e00-%5C%5Cu9fa5%5D+%7C%5C%5Cd+%22);%0D%0A%09%09Matcher rightAns = p.matcher(rightAnswer);%0D%0A%09%09String%5B%5D answAll = ans.split(%22%5Cn%22);%0D%0A%09%09String myLastLine = answAll%5BanswAll.length - 1%5D;%0D%0A%09%09myLastLine = myLastLine.replaceAll(%22%5C%5Cs*%22, %22%22);%0D%0A%09%09Matcher myAns = p.matcher(myLastLine);%0D%0A%09%09ArrayList myAnsList = appendResToStr(myAns);%0D%0A%09%09ArrayList rightAnsList = appendResToStr(rightAns);%0D%0A%09%09if ( isContaining( myAnsList, rightAnsList ) )%0D%0A%09%09%09return true;%0D%0A%09%09return false;%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A
 %09public static i
@@ -2685,11 +2685,446 @@
 %09%7D%0D%0A%09%7D%0D%0A
+%09public static ArrayList appendResToStr(Matcher content)%7B%0D%0A%09%09ArrayList res = new ArrayList();%0D%0A%09%09while(content.find())%7B%0D%0A%09%09%09res.add(content.group());%0D%0A%09%09%7D%0D%0A%09%09return res;%0D%0A%09%7D%0D%0A%09public static Boolean isContaining(ArrayList source, ArrayList target)%7B%0D%0A%09%09Integer sourceNum = source.size();%0D%0A%09%09Integer targetNum = target.size();%0D%0A%09%09Integer i, j;%0D%0A%09%09Boolean res = false, tmp = false;%0D%0A%09%09res = source.containsAll(target);%0D%0A%09%09return res;%0D%0A%09%7D%0D%0A
 %7D%0D%0A

Add support for Application Questions