package eu.ailao.hub.transformations;

import org.joda.time.LocalDate;
import org.joda.time.Years;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.json.JSONArray;
import org.json.JSONObject;

import static eu.ailao.hub.Statics.isContain;

/**
 * Created by Petr Marek on 30.12.2015.
 * Transformation transforming "How old is" question to "When he/she was born" and back.
 *
 * EXAMPLE: Question "How old is Travolta?" is transformed to "When was Travolta born?"
 * Answer "1954" is than transformed back to "61" (2015-1954).
 */
public class AgeTransformation extends Transformation {

	/***
	 * Patterns of date
	 */
	String[] patterns={"yyyy", "yyyy-MM-dd"};

	/***
	 * Detect if question contains "How old is" string;
	 * @param questionText Text of question to test
	 * @return TRUE if it can be transformed
	 */
	@Override
	public boolean transformDetection(String questionText) {
		String howOldQuestion = "How old is";
		if (isContain(questionText, howOldQuestion)) {
			return true;
		}
		return false;
	}

	/***
	 * "How old is" question to "When he/she was born"
	 * @param questionText Text of question to transform
	 * @return Transformed text of question
	 */
	@Override
	public String transform(String questionText) {
		String transformationFirstPart = "When was";
		String transformationSecondPart = "born?";
		String stripedQuestion = questionText.replace("How old is", "");
		stripedQuestion = stripedQuestion.replace("?", " ");
		return transformationFirstPart + stripedQuestion + transformationSecondPart;
	}

	/***
	 * Transforms answer back by getting date of born and subtracting it from actual date
	 * @param answer Answer to transform back
	 * @return Transformed answer
	 */
	@Override
	public JSONObject transformBack(JSONObject answer) {
		JSONArray answers = answer.getJSONArray("answers");
		for (Object o : answers) {
			JSONObject singleAnswer = (JSONObject) o;
			String answerText = (String) singleAnswer.get("text");

			LocalDate date = parseToLocalDate(answerText);
			if (date == null){
				singleAnswer.put("text", answerText);
				continue;
			}

			LocalDate now = new LocalDate();
			Years age = Years.yearsBetween(date, now);
			singleAnswer.put("text", String.valueOf(age.getYears()));
		}
		answer.put("answers", answers);
		return answer;
	}

	/***
	 * Tries to parse string of question to LocalDate, uses all patterns from patterns array
	 * @param answerText Text of single answer
	 * @return LocalDate or null
	 */
	private LocalDate parseToLocalDate(String answerText) {
		LocalDate dt=null;
		int i=0;
		while(dt == null) {
			if (i >= patterns.length){
				break;
			}
			dt = parseByPattern(answerText, patterns[i]);
			i++;
		}
		return dt;
	}

	/***
	 * Tries to parse text of answer by pattern
	 * @param answerText Text of single answer
	 * @param pattern Pattern of LocalDate
	 * @return LocalDate or null
	 */
	private LocalDate parseByPattern(String answerText,String pattern){
		LocalDate dt=null;
		try {
			DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
			dt = dtf.parseLocalDate(answerText);
		} catch (Exception e) {}
		return dt;
	}
}

@@ -684,17 +684,19 @@
 patterns
-=
+ = 
 %7B%22yyyy%22,
@@ -2073,16 +2073,17 @@
 == null)
+ 
 %7B%0A%09%09%09%09si
@@ -2319,16 +2319,426 @@
 swers);%0A
+%09%09if (!answer.isNull(%22answerSentence%22)) %7B%0A%09%09%09String answerSentence = (String) answer.get(%22answerSentence%22);%0A%09%09%09String%5B%5D answerSentenceSplit = answerSentence.split(%22date of birth is%22);%0A%09%09%09answerSentenceSplit = answerSentenceSplit%5B0%5D.split(%22birth date%22);%0A%09%09%09answer.put(%22answerSentence%22, answerSentenceSplit%5B0%5D + %22is %22%0A%09%09%09%09%09+ ((JSONObject) answer.getJSONArray(%22answers%22).get(0)).get(%22text%22) + %22 years old.%22);%0A%09%09%7D%0A
 %09%09return
@@ -2988,33 +2988,35 @@
 %7B%0A%09%09LocalDate dt
-=
+ = 
 null;%0A%09%09int i=0;
@@ -3012,17 +3012,19 @@
 %0A%09%09int i
-=
+ = 
 0;%0A%09%09whi
@@ -3025,16 +3025,17 @@
 %0A%09%09while
+ 
 (dt == n
@@ -3069,16 +3069,17 @@
 .length)
+ 
 %7B%0A%09%09%09%09br
@@ -3388,16 +3388,17 @@
 werText,
+ 
 String p
@@ -3404,16 +3404,17 @@
 pattern)
+ 
 %7B%0A%09%09Loca
@@ -3421,17 +3421,19 @@
 lDate dt
-=
+ = 
 null;%0A%09%09
@@ -3541,16 +3541,16 @@
 rText);%0A
-
 %09%09%7D catc
@@ -3566,16 +3566,19 @@
 ion e) %7B
+%0A%09%09
 %7D%0A%09%09retu

Transforming answer sentence by age transformation.