package com.kfzs.duanduan.datashare;

import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 17/3/10.
 */
public class BuildConfigUtils {

    public static String getBuildConfigByKey(String key) {
        String res;
        try {
            final Class<?> activityThread = Class.forName("android.app.ActivityThread");
            final Method currentPackage = activityThread.getMethod("currentPackageName");
            final String packageName = (String) currentPackage.invoke(null, (Object[]) null);
            final Class<?> buildConfig = Class.forName(packageName + ".BuildConfig");
            final Field findField = buildConfig.getField(key);
            findField.setAccessible(true);
            if (findField.getGenericType().toString().equals(
                    "class java.lang.String")) {
                res = (String) findField.get("");
            } else {
                res = "";
            }
        } catch (final Throwable t) {
            res = getBuildPNByKey("com.kfzs.duanduan", key);
        }
        return res;
    }

    public static String getBuildPNByKey(String pn, String key) {
        String res;
        try {
            final Class<?> buildConfig = Class.forName(pn + ".BuildConfig");
            final Field findField = buildConfig.getField(key);
            findField.setAccessible(true);
            if (findField.getGenericType().toString().equals(
                    "class java.lang.String")) {
                res = (String) findField.get("");
            } else {
                res = "";
            }
        } catch (final Throwable t) {
            Log.w("Find", "getBuildConfigByKey error", t);
            final String message = t.getMessage();
            if (message != null && message.contains("BuildConfig")) {
                res = "default";
            } else {
                res = "";
            }
        }
        return res;
    }

    private static List<String> getParameters(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        List<String> res = new ArrayList<String>();
        for (Field f : fields) {
            res.add(f.getName());
        }
        return res;
    }

    private static void getFieldsValue(Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();

        String[] types = {"java.lang.Integer",
                "java.lang.Double",
                "java.lang.Float",
                "java.lang.Long",
                "java.lang.Short",
                "java.lang.Byte",
                "java.lang.Boolean",
                "java.lang.Character",
                "java.lang.String",
                "int", "double", "long", "short", "byte", "boolean", "char", "float"};
        for (Field f : fields) {
            f.setAccessible(true);
            try {
                for (String str : types) {
                    if (f.getType().getName().equals(str))
                        System.out.println("Name: " + f.getName() + " |Type: " + f.getType().getName() + " |val: " + f.get(obj));
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}
