package dev.momo.library.core.tool;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;

import java.util.IllegalFormatConversionException;

import dev.momo.library.core.log.Logger;

/**
 * Created by Peng on 2015/12/3.
 */
public class ResourceHelper {

    private final static String TAG = ResourceHelper.class.getSimpleName();

    public static final String RESOURCE_TYPE_ID = "id";
    public static final String RESOURCE_TYPE_RAW = "raw";
    public static final String RESOURCE_TYPE_DRAWABLE = "drawable";
    public static final String RESOURCE_TYPE_String = "string";

    private static Context applicationContext;
    private static String appPackageName;
    public static Resources resources;

    public static void init(Context context, String packageName) {
        applicationContext = context.getApplicationContext();
        resources = applicationContext.getResources();
        appPackageName = packageName;

    }

    public static Resources get() {
        return resources;
    }

    public static int getID(String name) {
        return getID(RESOURCE_TYPE_ID, name);
    }

    public static int getID(String type, String name) {
        try {
            int res = applicationContext.getResources().getIdentifier(name, type, appPackageName);
            return res;
        } catch (Resources.NotFoundException e) {
            //            Logger.E("CAN NOT FIND RES WITH ID " + name);
            Logger.E(TAG, e);
            return 0;
        }
    }

    public static int getColor(@ColorRes int resId) {
        if (resources == null) return Color.BLACK;
        if (resId == 0) return Color.BLACK;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                return ContextCompat.getColor(applicationContext, resId);
            }
            return resources.getColor(resId);
        } catch (Resources.NotFoundException e) {
            Logger.E(TAG, e);
            return Color.BLACK;
        }
    }

    public static String getString(@StringRes int resId) {
        if (resources == null) return "";
        if (resId == 0) return "";

        return resources.getString(resId);
    }

    public static String getString(@StringRes int resId, Object... formatArgs) {
        if (resources == null) return "";
        if (resId == 0) return "";
        try {
            return resources.getString(resId, formatArgs);
        } catch (IllegalFormatConversionException e) {
            Logger.E(TAG, e);
        }
        return "";
    }


    public static Drawable getDrawable(@DrawableRes int resId) {
        if (resources == null) return null;
        if (resId == 0) return null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return resources.getDrawable(resId, applicationContext.getTheme());
        }
        return resources.getDrawable(resId);
    }

    public static int getDimenPixel(@DimenRes int resId) {
        if (resources == null) return 0;
        return (int) resources.getDimension(resId);
    }

    public static int getDimenDp(@DimenRes int resId) {
        if (resources == null) return 0;
        return Math.round(getDimenPixel(resId) / resources.getDisplayMetrics().density);
    }

    public static float getDimenFloat(@DimenRes int resId) {
        TypedValue outValue = new TypedValue();
        resources.getValue(resId, outValue, true);
        return outValue.getFloat();
    }
}
@@ -96,24 +96,63 @@
 .Resources;%0A
+import android.content.res.TypedArray;%0A
 import andro
@@ -238,16 +238,60 @@
 .Build;%0A
+import android.support.annotation.ArrayRes;%0A
 import a
@@ -2980,16 +2980,861 @@
 %0A    %7D%0A%0A
+    public static String%5B%5D getStringArray(@ArrayRes int resId) %7B%0A        String%5B%5D strings = new String%5B%5D%7B%22%22%7D;%0A        if (resources == null) return strings;%0A        if (resId == 0) return strings;%0A        try %7B%0A            strings = resources.getStringArray(resId);%0A        %7D catch (Resources.NotFoundException e) %7B%0A            Logger.E(TAG, e);%0A        %7D%0A        return strings;%0A    %7D%0A%0A    public static String getString(@ArrayRes int resID, int index) %7B%0A        String%5B%5D strings = getStringArray(resID);%0A        if (strings == null %7C%7C strings.length %3C= index) return %22%22;%0A        return strings%5Bindex%5D;%0A    %7D%0A%0A    public static @StringRes int getStringID(@ArrayRes int resID, int index) %7B%0A        if (resources == null) return 0;%0A        TypedArray array = resources.obtainTypedArray(resID);%0A        return array.getResourceId(index, 0);%0A    %7D%0A
 %0A    pub

update array resource methods