package dev.momo.library.core.tool;

import android.annotation.TargetApi;
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.NonNull;
import android.support.annotation.Nullable;
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;
        }
    }

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

    }

    @NonNull
    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 "";
    }


    @Nullable
    public static Drawable getDrawable(@DrawableRes int resId) {
        if (resources == null) return null;
        if (resId == 0) return null;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                return resources.getDrawable(resId, applicationContext.getTheme());
            }
            return resources.getDrawable(resId);
        } catch (Resources.NotFoundException e) {
            Logger.E(TAG, e);
            return null;
        }
    }

    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();
    }
}
@@ -34,45 +34,8 @@
 l;%0A%0A
-import android.annotation.TargetApi;%0A
 impo
@@ -417,24 +417,71 @@
 n.Nullable;%0A
+import android.support.annotation.RequiresApi;%0A
 import andro
@@ -3394,33 +3394,32 @@
 ERSION.SDK_INT %3E
-=
  Build.VERSION_C
@@ -3524,33 +3524,44 @@
 );%0A            %7D
-%0A
+ else %7B%0A    
             retu
@@ -3585,32 +3585,46 @@
 rawable(resId);%0A
+            %7D%0A
         %7D catch 

update ResouceHelper