package org.sugr.gearshift.ui.util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.TextView;

import org.sugr.gearshift.R;

public class Colorizer {
    public static void colorizeView(ImageView view, int color, int shape) {
        Resources res = view.getContext().getResources();

        Drawable currentDrawable = view.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable != null && currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(shape);
        }

        // Set stroke to dark version of color
        int darkenedColor = Color.rgb(
                Color.red(color) * 192 / 256,
                Color.green(color) * 192 / 256,
                Color.blue(color) * 192 / 256);

        colorChoiceDrawable.setColor(color);
        colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, res.getDisplayMetrics()), darkenedColor);
        view.setImageDrawable(colorChoiceDrawable);

    }

    public static void colorizeView(TextView view, int color) {
        view.setTextColor(color);
    }

    public static int defaultColor(Context context) {

        String[] colorArray = context.getResources().getStringArray(R.array.default_color_choice_values);

        if (colorArray != null && colorArray.length > 0) {
            return Color.parseColor(colorArray[0]);
        } else {
            return context.getResources().getColor(R.color.primary);
        }
    }

}

@@ -931,24 +931,179 @@
 %0A        %7D%0A%0A
+        int darkenedColor;%0A        if (color == defaultColor(view.getContext())) %7B%0A            darkenedColor = Color.rgb(95, 95, 95);%0A        %7D else %7B%0A    
         // S
@@ -1137,35 +1137,35 @@
 f color%0A        
-int
+   
  darkenedColor =
@@ -1188,24 +1188,28 @@
             
+    
 Color.red(co
@@ -1218,32 +1218,36 @@
 r) * 192 / 256,%0A
+    
                 
@@ -1290,24 +1290,28 @@
             
+   
+ 
 Color.blue(c
@@ -1329,16 +1329,26 @@
  / 256);
+%0A        %7D
 %0A%0A      

use a custom stroke color for the default one