package com.xplora.xpsettings.Utils;
import android.content.Context;

public class ResUtils {
    private static Context mSaveContext = null;
    private static String mSavePackageName = null;

    public static void initResUtils(Context context, String name) {
        mSaveContext = context;
        mSavePackageName = name;
    }

    public static String getString(int strId,Object...formatArgs) {
        if (mSaveContext == null)
            return null;
        return mSaveContext.getResources().getString(strId, formatArgs);
    }

    public static int getStringId(String prefix, int index) {
        StringBuilder builder = new StringBuilder();
        builder.append(prefix).append(index);

        return ResUtils.getResourcesId(builder.toString(), "string");
    }

    public static int getImageId(String prefix, int index) {
        StringBuilder builder = new StringBuilder();
        builder.append(prefix).append(index);

        return ResUtils.getResourcesId(builder.toString(), "drawable");
    }

    public static int getWidgetId(String prefix, int index) {
        StringBuilder builder = new StringBuilder();
        builder.append(prefix).append(index);

        return ResUtils.getResourcesId(builder.toString(), "id");
    }

    public static int getLayoutId(String prefix, int index) {
        StringBuilder builder = new StringBuilder();
        builder.append(prefix).append(index);

        return ResUtils.getResourcesId(builder.toString(), "layout");
    }

    public static int getResourcesId(String imageName, String defType) {
        if (mSaveContext == null)
            return 0;

        return mSaveContext.getResources().getIdentifier(imageName, defType, mSavePackageName);
    }

    public static int getResourcesId(Context context, String packageName, String imageName, String defType) {
        return context.getResources().getIdentifier(imageName, defType, packageName);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(float dpValue) {
        final float scale = mSaveContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(float pxValue) {
        final float scale = mSaveContext.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
