/*
 *    Copyright 2010 Chris Sinjakli
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Chris Sinjakli
 */
package uk.co.sinjakli.trivial;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;


public class QuizActivity extends Activity {
	
	// Private Constants
	private static final String TAG = "QuizActivity";
	
	// Load available resources
	private final Resources res = getResources();
	
	private final ArrayList<Question> questions = new ArrayList<Question>();
	
	public void onCreate(final Bundle savedInstanceState) {
		
		// Load up all questions available from the files
		try {
			int failedParsesTotal = 0;
			for (final String fileName : getAssets().list("questions/")) {
				final InputStream input = getAssets().open("questions/" + fileName);
				final BufferedReader reader = new BufferedReader(new InputStreamReader(input));
				
				// Populate the questions ArrayList
				String inputLine;
				int failedParses = 0;
				while (null != (inputLine = reader.readLine())) {
					// Ignore any comments in the question file
					if (!inputLine.startsWith("//")) {
						try {
							questions.add(Question.parse(inputLine));
						} catch (final IllegalArgumentException e) {
							failedParses++;
							Log.e(TAG, "Unable to parse question: " + inputLine, e); // Dev String
						}
					}
				}
				
				// If any questions failed to parse within the current file, summarise how many failed
				if (0 < failedParses) {
					failedParsesTotal += failedParses;
					Log.e(TAG, failedParses + " questions were unable to be parsed in file questions/" + fileName); // Dev String
				}
			}
			
			// If any questions failed to parse within any file, summarise (to the user) how many failed
			if (0 < failedParsesTotal) {
				Toast.makeText(getApplicationContext(), String.format(res.getString(R.plurals.question_reading_parse_fail_number), failedParsesTotal), Toast.LENGTH_LONG).show();
			}
			
		} catch (final IOException e) {
			Log.e(TAG, "Error reading questions.", e); // Dev String
			Toast.makeText(getApplicationContext(), res.getString(R.string.question_reading_ioexception), Toast.LENGTH_LONG).show();
		}
	
		// Randomise the output order of the questions
		Collections.shuffle(questions);
	}
}

@@ -1234,16 +1234,68 @@
 ();%0D%0A%09%0D%0A
+%09// Load up all questions available from the files%0D%0A
 %09private
@@ -1337,32 +1337,34 @@
 s = 
-new ArrayList%3CQ
+loadQuestions(%22q
 uestion
-%3E(
+s/%22
 );%0D%0A
@@ -1428,63 +1428,244 @@
  %7B%0D%0A
-%09%09
 %0D%0A%09%09// 
-Load up all questions available from the files
+Randomise the output order of the questions%0D%0A%09%09Collections.shuffle(questions);%0D%0A%09%7D%0D%0A%0D%0A%09private ArrayList%3CQuestion%3E loadQuestions(final String questionFilePath) %7B%0D%0A%09%09final ArrayList%3CQuestion%3E questions = new ArrayList%3CQuestion%3E();
 %0D%0A%09%09
@@ -1751,25 +1751,24 @@
 ().list(
-%22
 question
 s/%22)) %7B%0D
@@ -1759,19 +1759,24 @@
 question
-s/%22
+FilePath
 )) %7B%0D%0A%09%09
@@ -1820,25 +1820,24 @@
 ().open(
-%22
 question
 s/%22 + fi
@@ -1820,35 +1820,40 @@
 ().open(question
-s/%22
+FilePath
  + fileName);%0D%0A%09
@@ -2664,16 +2664,20 @@
  in file
+ %22 +
  questio
@@ -2677,19 +2677,24 @@
 question
-s/%22
+FilePath
  + fileN
@@ -3271,92 +3271,29 @@
 %7D%0D%0A%09
+%09
 %0D%0A%09%09
-// Randomise the output order of the questions%0D%0A%09%09Collections.shuffle(questions)
+return questions
 ;%0D%0A%09

Refactor loading of questions from files into seperate method.