package shared.android;

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

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 + "*";
	}
}

@@ -128,16 +128,49 @@
 Proxy;%0A%0A
+import android.widget.TextView;%0A%0A
 import c
@@ -1711,16 +1711,22 @@
 return s
+ + '!'
 ;%0A%09%09else
@@ -1750,11 +1750,387 @@
  %22*%22;%0A%09%7D
+%0A%0A%09public static void translate(TextView... textViews) %7B%0A%09%09for (TextView textView : textViews) %7B%0A%09%09%09textView.setText(get(textView.getText().toString()));%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * %0A%09 * @param textView or Buttons and other subclasses%0A%09 */%0A%09public static void translate(TextView textView) %7B%0A%09%09if (textView == null)%0A%09%09%09return;%0A%09%09%0A%09%09textView.setText(get(textView.getText().toString()));%0A%09%7D
 %0A%7D%0A

translate using original string as key