package shared.android;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import android.widget.TextView;

import com.google.tagmanager.Container;

public class ContainerFacade {
	
	@SuppressWarnings("unchecked")
	public static <T> T get(final Container container, Class<T> interfaceClass) {
		InvocationHandler handler = new InvocationHandler() {			
			@Override
			public Object invoke(Object obj, Method method, Object[] args)
					throws Throwable {
				assert args.length == 0;
				
				final Class<?> returnType = method.getReturnType();
				final String name = method.getName();

				if (returnType.equals(String.class)) {
					final String string = container.getString(name);
					if (string.length() > 0)
						return string;
					else
						return name;
				} else if (returnType.equals(Boolean.class) || returnType.equals(boolean.class)) {
					return container.getBoolean(name);
				} else if (returnType.equals(Long.class) || returnType.equals(long.class)) {
					return container.getLong(name);
				} else if (returnType.equals(Double.class) || returnType.equals(double.class)) {
					return container.getDouble(name);
				} else {
					throw new IllegalArgumentException("Must be String, Boolean, Long or Double: " + returnType);
				}
			}
		};
		
		Object obj = Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass},
				handler);
		return (T) obj;
	}

	public static <T> T get(Class<T> interfaceClass) {
		return get(ContainerHolder.getContainer(), interfaceClass);
	}

	public static String get(String name) {
		String s = ContainerHolder.getContainer().getString(name);
		if (s.length() > 0)
			return s + '!';
		else
			return name + "*";
	}

	public static void translate(TextView... textViews) {
		for (TextView textView : textViews) {
			textView.setText(get(textView.getText().toString()));
		}
	}

	/**
	 * 
	 * @param textView or Buttons and other subclasses
	 */
	public static void translate(TextView textView) {
		if (textView == null)
			return;
		
		textView.setText(get(textView.getText().toString()));
	}
}

@@ -128,16 +128,102 @@
 Proxy;%0A%0A
+import android.app.Activity;%0Aimport android.view.View;%0Aimport android.view.ViewGroup;%0A
 import a
@@ -2160,61 +2160,736 @@
 %09%0A%09%09
-textView.setText(get(textView.getText().toString()));
+final String string = textView.getText().toString();%0A%09%09if (string.length() %3E 0 && string.endsWith(%22%22)) %7B%0A%09%09%09textView.setText(get(string.trim()));%0A%09%09%7D%0A%09%7D%0A%0A%09public static void translate(View view) %7B%0A%09%09if (view instanceof ViewGroup) %7B%0A%09%09%09ViewGroup group = (ViewGroup)view;%0A%09%09%09for (int i = 0; i %3C group.getChildCount(); i++) %7B%0A%09%09%09%09View child = group.getChildAt(i);%0A%09%09%09%09%0A%09%09%09%09translate(child);%0A%09%09%09%7D%0A%09%09%7D else if (view instanceof TextView) %7B%0A%09%09%09translate((TextView)view);%0A%09%09%7D%0A%09%7D%0A%0A%09public static void translate(Activity act) %7B%0A        act.setTitle(ContainerFacade.get(act.getTitle().toString()));%0A        //HierarchicalViewIdViewer.debugViewIds(act.findViewById(android.R.id.content), %22views%22);%0A        // no easy way to get content view
 %0A%09%7D%0A

add way to translate view tree