package com.flytxt.tp.translator.tpdateutils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.flytxt.tp.marker.Marker;
import com.flytxt.tp.marker.MarkerFactory;

//@Ignore
public class TranslatorTest {
	private MarkerFactory mf = new MarkerFactory();

	@Before
	public void init() {

	}

	class Sample{
		String input;
		String fmt;
		public DateTime date;
	}
	String[] goodFmts = {
//			"yyyy.MM.ddHH",
			"yyyy.MM.ddHH:mm:ssSZ" 
			," MM.yyyy.ddHH:mm:ssSZ"
	};

	String[] badFmts = {
		//	"Zyyyy.MM.ddHH:mm:ssS " ,
		//	" yyyyy.MMMMM.dd GGG hh:mm aaa ",
			
			"yyyy.MM.ddHH:mm:ssSZ ",
			"yyyy.MM.ddHH:mm:ss ",
			"EEE, d MMM yyyy HH:mm:ss Z",

			"yyMMddHHmmssZ",

			"yyyy-MM-dd'T'HH:mm:ss.SSSZ"

	};
	private List<Sample>genSamples(String[] fmts){
		DateTime dt = new DateTime();
		List<Sample> list = new ArrayList<Sample>(fmts.length);
		int i = 0;
		Sample sample;
		for (String fmt : fmts) {
			sample = new Sample();
			sample.input= toString(dt, fmt);
			sample.fmt = fmts[i++];
			sample.date = dt;
			list.add(sample);
		}
		return list;
	}
	private byte[] convertToFlyFmt(String fmtStr, String dateAsstr) throws ParseException {
		CoOccur coOccur = new CoOccur(fmtStr);
		Translator t = new Translator(coOccur.toPlan());
		Marker m = mf.createMarker(dateAsstr);
		byte[] result = t.translate(m, null);
		return result;
	}
	
	private byte[] convertToFlyFmtSDF(String fmtStr, String dateAsstr) throws ParseException {
		Translator t = new Translator(fmtStr);
		Marker m = mf.createMarker(dateAsstr);
		byte[] result = t.translate(m, null);
		return result;
	}

	private String toString(DateTime dt, String fmtStr) {
		DateTimeFormatter fmt = DateTimeFormat.forPattern(fmtStr);
		String dateAsstr = fmt.print(dt);
		return dateAsstr;
	}

	@Test
	public void testTranslation() {
		List<Sample> list = genSamples(goodFmts);
		for (int i = 0; i < list.size(); i++) {
			String input = list.get(i).input;
			String expected = toString(list.get(i).date, Translator.flyDateFormat);
			try {
				
				byte[] result = convertToFlyFmt(list.get(i).fmt, input);
//				System.out.println(list.get(i).fmt);
//				System.out.println("input     :" + input);
				System.out.println("expected  :" + expected);
				System.out.println("result    :" + new String(result));
			if (!expected.equals(new String(result))) {
					Assert.assertEquals(expected, new String(result));
				}
			} catch (ParseException e) {
				System.out.println(e.getMessage()); 
				System.out.println(list.get(i).input+" : "+list.get(i).fmt);
				Assert.fail();
			}
		}
	}

	@Test
	public void testbadTranslation() {
		List<Sample> list = genSamples(badFmts);
		for (int i = 0; i < list.size(); i++) {
			String input = list.get(i).input;
//			String expected = toString(list.get(i).date, Translator.flyDateFormat);
			try {

				byte[] result = convertToFlyFmt(list.get(i).fmt, input);
				//Assert.fail();
			} catch (ParseException e) {
			}
		}
	}
	@Test
	public void testbadSDF() {
		List<Sample> list = genSamples(badFmts);
		for (int i = 0; i < list.size(); i++) {
			String input = list.get(i).input;
			String expected = toString(list.get(i).date, Translator.flyDateFormat);
			try {
               System.out.println("index " + i);
				byte[] result = convertToFlyFmtSDF(list.get(i).fmt, input);
				if (!expected.equals(new String(result))) {
					Assert.assertEquals(expected, new String(result));
				}
			} catch (ParseException e) {
				Assert.fail();
			}
		}
	}
	
	@Test
	public void testGoodSDF() {
		List<Sample> list = genSamples(goodFmts);
		for (int i = 0; i < list.size(); i++) {
			String input = list.get(i).input;
			String expected = toString(list.get(i).date, Translator.flyDateFormat);
			try {

				byte[] result = convertToFlyFmtSDF(list.get(i).fmt, input);
				if (!expected.equals(new String(result))) {
					Assert.assertEquals(expected, new String(result));
				}
			} catch (ParseException e) {
				Assert.fail();
			}
		}
	}
}

@@ -758,18 +758,16 @@
 S %22 ,%0A%09%09
-//
 %09%22 yyyyy
@@ -797,66 +797,8 @@
  %22,%0A
-%09%09%09%0A%09%09%09%22yyyy.MM.ddHH:mm:ssSZ %22,%0A%09%09%09%22yyyy.MM.ddHH:mm:ss %22,%0A
 %09%09%09%22
@@ -2888,19 +2888,19 @@
 .input;%0A
+%09
 //
-%09
 %09%09String
@@ -3040,18 +3040,16 @@
 t);%0A%09%09%09%09
-//
 Assert.f
@@ -3087,16 +3087,21 @@
 on e) %7B%0A
+%09%09%09%09%0A
 %09%09%09%7D%0A%09%09%7D
@@ -3174,32 +3174,115 @@
 mples(badFmts);%0A
+%09%09%0A%09%09DateTimeFormatter sfmt = DateTimeFormat.forPattern(Translator.flyDateFormat);%0A
 %09%09for (int i = 0
@@ -3351,137 +3351,166 @@
 %0A%09%09%09
-String expected = toString(list.get(i).date, Translator.flyDateForma
+DateTimeFormatter sfmt1 = DateTimeFormat.forPattern(list.get(i).fm
 t);%0A%09%09%09
+S
 tr
-y %7B%0A               System.out.println(%22index %22 + i);
+ing expected = sfmt.print(sfmt1.parseDateTime(input)).toString();%0A%09%09%09%0A%09%09%09%0A%09%09%09try %7B
 %0A%09%09%09

Test class