package eu.inmite.android.lib.validations.form;

import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;
import eu.inmite.android.lib.validations.form.adapters.JoinedAdapter;
import eu.inmite.android.lib.validations.form.adapters.SpinnerAdapter;
import eu.inmite.android.lib.validations.form.adapters.TextViewAdapter;
import eu.inmite.android.lib.validations.form.annotations.Joined;
import eu.inmite.android.lib.validations.form.iface.IFieldAdapter;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

/**
 * Created adapters for views with validation.
 *
 * @author Tomas Vondracek
 */
public class FieldAdapterFactory {

	private static JoinedAdapter sJoinedAdapter;
	private static TextViewAdapter sTextViewAdapter;
	private static SpinnerAdapter sSpinnerViewAdapter;

	private static Map<Class<? extends View>, IFieldAdapter<? extends View,?>> sExternalAdapters;

	static void registerAdapter(Class<? extends View> viewType, Class<? extends IFieldAdapter<? extends View,?>> adapterClazz) throws IllegalAccessException, InstantiationException {
		if (sExternalAdapters == null) {
			sExternalAdapters = new HashMap<Class<? extends View>, IFieldAdapter<? extends View,?>>();
		}
		sExternalAdapters.put(viewType, adapterClazz.newInstance());
	}


	public static IFieldAdapter getAdapterForField(View view) {
		return getAdapterForField(view, null);
	}

	public static IFieldAdapter<? extends View,?> getAdapterForField(View view, Annotation annotation) {
		final IFieldAdapter<? extends View,?> adapter;
		if (annotation != null && Joined.class.equals(annotation.annotationType())) {
			if (sJoinedAdapter == null) {
				sJoinedAdapter = new JoinedAdapter();
			}
			adapter = sJoinedAdapter;
		} else if (view instanceof TextView) {
			if (sTextViewAdapter == null) {
				sTextViewAdapter = new TextViewAdapter();
			}
			adapter = sTextViewAdapter;
		} else if (view instanceof Spinner) {
			if (sSpinnerViewAdapter == null) {
				sSpinnerViewAdapter = new SpinnerAdapter();
			}
			adapter = sSpinnerViewAdapter;
		} else if (sExternalAdapters != null && sExternalAdapters.containsKey(view.getClass())) {
			adapter = sExternalAdapters.get(view.getClass());
		} else {
			adapter = null;
		}
		return adapter;
	}

	static void clear() {
		if (sExternalAdapters != null) {
			sExternalAdapters.clear();
		}
		sJoinedAdapter = null;
		sTextViewAdapter = null;
		sSpinnerViewAdapter = null;
	}
}

@@ -126,24 +126,186 @@
 et.TextView;
+%0A%0Aimport java.lang.annotation.Annotation;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0Aimport eu.inmite.android.lib.validations.form.adapters.CompoundAdapter;
 %0Aimport eu.i
@@ -644,97 +644,8 @@
 r;%0A%0A
-import java.lang.annotation.Annotation;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -908,16 +908,70 @@
 Adapter;
+%0A%09private static CompoundAdapter sCompoundViewAdapter;
 %0A%0A%09priva
@@ -1882,32 +1882,183 @@
 inedAdapter;%0A%09%09%7D
+ else if (sExternalAdapters != null && sExternalAdapters.containsKey(view.getClass())) %7B%0A%09%09%09adapter = sExternalAdapters.get(view.getClass());%0A        %7D
  else if (view i
@@ -2319,32 +2319,32 @@
 Adapter();%0A%09%09%09%7D%0A
+
 %09%09%09adapter = sSp
@@ -2365,153 +2365,8 @@
 er;%0A
-%09%09%7D else if (sExternalAdapters != null && sExternalAdapters.containsKey(view.getClass())) %7B%0A%09%09%09adapter = sExternalAdapters.get(view.getClass());%0A
 %09%09%7D 

FIX :: External adapters should have more priority otherwise they become unusable