package org.andstatus.todoagenda.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.os.Build;
import android.util.TypedValue;
import android.widget.RemoteViews;

import org.andstatus.todoagenda.prefs.InstanceSettings;

import static java.lang.Float.parseFloat;

public class RemoteViewsUtil {

    private static final String METHOD_SET_TEXT_SIZE = "setTextSize";
    private static final String METHOD_SET_BACKGROUND_COLOR = "setBackgroundColor";
    private static final String METHOD_SET_SINGLE_LINE = "setSingleLine";
    private static final String METHOD_SET_ALPHA = "setAlpha";
    private static final String METHOD_SET_COLOR_FILTER = "setColorFilter";

    private RemoteViewsUtil() {
        // prohibit instantiation
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public static void setPadding(InstanceSettings settings, RemoteViews rv, int viewId, int leftDimenId,
                                  int topDimenId, int rightDimenId, int bottomDimenId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            int leftPadding = Math.round(getScaledValueInPixel(settings, leftDimenId));
            int topPadding = Math.round(getScaledValueInPixel(settings, topDimenId));
            int rightPadding = Math.round(getScaledValueInPixel(settings, rightDimenId));
            int bottomPadding = Math.round(getScaledValueInPixel(settings, bottomDimenId));
            rv.setViewPadding(viewId, leftPadding, topPadding, rightPadding, bottomPadding);
        }
    }

    public static void setAlpha(RemoteViews rv, int viewId, int alpha) {
        rv.setInt(viewId, METHOD_SET_ALPHA, alpha);
    }

    public static void setColorFilter(RemoteViews rv, int viewId, int color) {
        rv.setInt(viewId, METHOD_SET_COLOR_FILTER, color);
    }

    public static void setTextSize(InstanceSettings settings, RemoteViews rv, int viewId, int dimenId) {
        rv.setFloat(viewId, METHOD_SET_TEXT_SIZE, getScaledValue(settings, dimenId));
    }

    public static void setTextColorFromAttr(Context context, RemoteViews rv, int viewId, int colorAttrId) {
        rv.setTextColor(viewId, getColorValue(context, colorAttrId));
    }

    public static void setBackgroundColorFromAttr(Context context, RemoteViews rv, int viewId,
                                                  int colorAttrId) {
        setBackgroundColor(rv, viewId, getColorValue(context, colorAttrId));
    }

    public static void setBackgroundColor(RemoteViews rv, int viewId, int color) {
        rv.setInt(viewId, METHOD_SET_BACKGROUND_COLOR, color);
    }

    private static float getScaledValueInPixel(InstanceSettings settings, int dimenId) {
        float resValue = getDimension(settings.getContext(), dimenId);
        float prefTextScale = parseFloat(settings.getTextSizeScale());
        return resValue * prefTextScale;
    }

    private static float getScaledValue(InstanceSettings settings, int dimenId) {
        float resValue = getDimension(settings.getEntryThemeContext(), dimenId);
        float density = settings.getEntryThemeContext().getResources().getDisplayMetrics().density;
        float prefTextScale = parseFloat(settings.getTextSizeScale());
        return resValue * prefTextScale / density;
    }

    private static int getColorValue(Context context, int attrId) {
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(attrId, outValue, true);
        int colorId = outValue.resourceId;
        return context.getResources().getColor(colorId);
    }

    private static float getDimension(Context context, int dimenId) {
        try {
            return context.getResources().getDimension(dimenId);
        } catch (NotFoundException e) {
            // resource might not exist
            return 0f;
        }
    }

    public static void setMultiline(RemoteViews rv, int viewId, boolean multiLine) {
        rv.setBoolean(viewId, METHOD_SET_SINGLE_LINE, !multiLine);
    }

    public static void setImageFromAttr(Context context, RemoteViews rv, int viewId, int attrResId) {
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(attrResId, outValue, true);
        setImage(rv, viewId, outValue.resourceId);
    }

    public static void setImage(RemoteViews rv, int viewId, int resId) {
        rv.setImageViewResource(viewId, resId);
    }
}

@@ -177,16 +177,72 @@
 oid.
-os.Build
+graphics.Color;%0Aimport android.os.Build;%0Aimport android.util.Log
 ;%0Aim
@@ -3507,32 +3507,36 @@
 alue();%0A        
+if (
 context.getTheme
@@ -3578,26 +3578,31 @@
 e, true)
-;%0A
+) %7B%0A   
         
 int colo
@@ -3585,32 +3585,33 @@
 )) %7B%0A           
+ 
 int colorId = ou
@@ -3603,16 +3603,24 @@
 nt color
+Resource
 Id = out
@@ -3637,16 +3637,42 @@
 urceId;%0A
+            try %7B%0A        
         
@@ -3715,19 +3715,531 @@
 or(color
-Id)
+ResourceId);%0A            %7D catch (Exception e) %7B%0A                Log.w(RemoteViewsUtil.class.getSimpleName(), %22context.getResources() failed to resolve color for%22 +%0A                        %22 resource Id:%22 + colorResourceId +%0A                        %22 derived from attribute Id:%22 + attrId, e);%0A                return Color.GRAY;%0A            %7D%0A        %7D%0A        Log.w(RemoteViewsUtil.class.getSimpleName(),%0A                %22getColorValue failed to resolve color for attribute Id:%22 + attrId);%0A        return Color.GRAY
 ;%0A    %7D%0A
@@ -4295,32 +4295,44 @@
 ntext, int dimen
+sionResource
 Id) %7B%0A        tr
@@ -4391,24 +4391,36 @@
 ension(dimen
+sionResource
 Id);%0A       
@@ -4468,35 +4468,125 @@
     
-// resource might not exist
+Log.w(RemoteViewsUtil.class.getSimpleName(), %22getDimension failed for dimension resource Id:%22 + dimensionResourceId);
 %0A   

Fix crash: Prevent and catch android.content.res.Resources$NotFoundException on context.getResources().getColor