package net.cherokeedictionary.bind;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.skife.jdbi.v2.SQLStatement;
import org.skife.jdbi.v2.sqlobject.Binder;
import org.skife.jdbi.v2.sqlobject.BinderFactory;
import org.skife.jdbi.v2.sqlobject.BindingAnnotation;

import net.cherokeedictionary.model.DictionaryEntry;
import net.cherokeedictionary.model.DictionaryEntry.EntryExample;
import net.cherokeedictionary.util.DaoUtils;

@BindingAnnotation(BindEnglishIndex.Factory.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface BindEnglishIndex {
	public static class Factory implements BinderFactory {
		public Binder<BindEnglishIndex, DictionaryEntry> build(Annotation annotation) {
			return new Binder<BindEnglishIndex, DictionaryEntry>() {
				public void bind(SQLStatement<?> q, BindEnglishIndex bind, DictionaryEntry record) {
					String syllabary = null;
					String pronunciation=null;
					String definition=null;
					if (record.forms!=null && record.forms.size()>0) {
						syllabary=record.forms.get(0).syllabary;
						pronunciation=record.forms.get(0).pronunciation;
						if (pronunciation==null||pronunciation.isEmpty()) {
							pronunciation=record.forms.get(0).latin;
						}
					}
					if (record.definitions!=null) {
						definition="";
						for (String def: record.definitions) {
							if (!definition.isEmpty()) {
								definition+=";";
							}
							definition+=def;
						}
					}
					
					q.bind("id", record.id);
					q.bind("source", record.source);
					q.bind("syllabary", syllabary);
					q.bind("pronunciation", DaoUtils.unicodePronunciation(pronunciation));
					q.bind("definition", definition);
					
					StringBuilder sb = new StringBuilder();
					if (record.definitions!=null) {
						for (String def: record.definitions) {
							if (def==null||def.isEmpty()) {
								continue;
							}
							if (!definition.isEmpty()) {
								definition+=";";
							}
							definition+=def;
						}
					}
					q.bind("forms", sb.toString().replaceAll("<.*?>", "").trim());
					sb.setLength(0);
					if (record.examples!=null) {
						for (EntryExample example: record.examples) {
							if (example.english!=null && !example.english.isEmpty()) {
								sb.append(example.english);
								sb.append("\n");
							}
						}
					}
					q.bind("examples", sb.toString().replaceAll("<.*?>", "").trim());
				}
			};
		}
	}
}

/*
(select :id as id, :source as source, :syllabary as syllabary,"
			+ " :pronunciation as pronunciation, :definition as definition,"
			+ " :forms as forms, :examples as examples
*/
@@ -2053,56 +2053,48 @@
 if (
-!definition.isEmpty()) %7B%0A%09%09%09%09%09%09%09%09definition+=
+sb.length()!=0) %7B%0A%09%09%09%09%09%09%09%09sb.append(
 %22;%22
+)
 ;%0A%09%09
@@ -2103,39 +2103,38 @@
 %09%09%09%7D%0A%09%09%09%09%09%09%09
-definition+=
+sb.append(
 def
+)
 ;%0A%09%09%09%09%09%09%7D%0A%09%09

undo foobar with definitions are supposed to go into forms field