package com.braintreepayments.cardform.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;

import com.braintreepayments.cardform.R;

public enum ExpirationDateDialogTheme {

    LIGHT(R.color.bt_black_87, R.color.bt_white_87, R.color.bt_black_38),
    DARK(R.color.bt_white_87, R.color.bt_black_87, R.color.bt_white_38);

    private int mItemTextColor;
    private int mItemInverseTextColor;
    private int mItemDisabledTextColor;

    private Context mContext;

    ExpirationDateDialogTheme(int itemTextColor, int itemInverseTextColor, int itemDisabledTextColor) {
        mItemTextColor = itemTextColor;
        mItemInverseTextColor = itemInverseTextColor;
        mItemDisabledTextColor = itemDisabledTextColor;
    }

    public static ExpirationDateDialogTheme detectTheme(Activity activity) {
        int color = activity.getResources().getColor(R.color.bt_white);
        try {
            Drawable background = activity.getWindow().getDecorView().getRootView().getBackground();
            if (background instanceof ColorDrawable) {
                color = ((ColorDrawable) background).getColor();
            }
        } catch (Exception ignored) {}

        double luminance = (0.2126 * Color.red(color)) + (0.7152 * Color.green(color)) +
                (0.0722 * Color.blue(color));
        ExpirationDateDialogTheme theme;
        if (luminance < 128) {
            theme = ExpirationDateDialogTheme.LIGHT;
        } else {
            theme = ExpirationDateDialogTheme.DARK;
        }

        theme.setContext(activity);
        return theme;
    }

    public int getItemTextColor() {
        return mContext.getResources().getColor(mItemTextColor);
    }

    public int getItemInvertedTextColor() {
        return getColor("textColorPrimaryInverse", mItemInverseTextColor);
    }

    public int getItemDisabledTextColor() {
        return mContext.getResources().getColor(mItemDisabledTextColor);
    }

    public int getSelectedItemBackground() {
        return getColor("colorAccent", R.color.bt_blue);
    }

    private int getColor(String attr, int fallbackColor) {
        TypedValue color = new TypedValue();
        try {
            int colorId = mContext.getResources().getIdentifier(attr, "attr", mContext.getPackageName());
            if (mContext.getTheme().resolveAttribute(colorId, color, true)) {
                return color.data;
            }
        } catch (Exception ignored) {}

        return mContext.getResources().getColor(fallbackColor);
    }

    private void setContext(Context context) {
        mContext = context;
    }
}


@@ -73,40 +73,8 @@
 ty;%0A
-import android.content.Context;%0A
 impo
@@ -459,32 +459,38 @@
 );%0A%0A    private 
+final 
 int mItemTextCol
@@ -497,32 +497,38 @@
 or;%0A    private 
+final 
 int mItemInverse
@@ -546,24 +546,30 @@
     private 
+final 
 int mItemDis
@@ -601,24 +601,178 @@
 ate 
-Context mContext
+int mResolvedItemTextColor;%0A    private int mResolvedItemInverseTextColor;%0A    private int mResolvedItemDisabledTextColor;%0A    private int mResolvedSelectedItemBackground
 ;%0A%0A 
@@ -1827,30 +1827,423 @@
 eme.
-setContext(activity
+mResolvedItemTextColor = activity.getResources().getColor(theme.mItemTextColor);%0A        theme.mResolvedItemInverseTextColor = getColor(activity, %22textColorPrimaryInverse%22, theme.mItemInverseTextColor);%0A        theme.mResolvedItemDisabledTextColor = activity.getResources().getColor(theme.mItemDisabledTextColor);%0A        theme.mResolvedSelectedItemBackground = getColor(activity, %22colorAccent%22, R.color.bt_blue
 );%0A
+%0A
     
@@ -2323,41 +2323,16 @@
 rn m
-Context.getResources().getColor(m
+Resolved
 Item
@@ -2332,33 +2332,32 @@
 vedItemTextColor
-)
 ;%0A    %7D%0A%0A    pub
@@ -2412,45 +2412,17 @@
 urn 
-getColor(%22textColorPrimaryInverse%22, m
+mResolved
 Item
@@ -2429,33 +2429,32 @@
 InverseTextColor
-)
 ;%0A    %7D%0A%0A    pub
@@ -2510,41 +2510,16 @@
 rn m
-Context.getResources().getColor(m
+Resolved
 Item
@@ -2531,25 +2531,24 @@
 ledTextColor
-)
 ;%0A    %7D%0A%0A   
@@ -2608,48 +2608,39 @@
 urn 
-getColor(%22colorAccent%22, R.color.bt_blue)
+mResolvedSelectedItemBackground
 ;%0A  
@@ -2652,24 +2652,31 @@
     private 
+static 
 int getColor
@@ -2676,16 +2676,35 @@
 etColor(
+Activity activity, 
 String a
@@ -2814,24 +2814,24 @@
 lorId = 
-mContext
+activity
 .getReso
@@ -2866,24 +2866,24 @@
 %22attr%22, 
-mContext
+activity
 .getPack
@@ -2910,24 +2910,24 @@
     if (
-mContext
+activity
 .getThem
@@ -3076,24 +3076,24 @@
  return 
-mContext
+activity
 .getReso
@@ -3134,90 +3134,8 @@
    %7D
-%0A%0A    private void setContext(Context context) %7B%0A        mContext = context;%0A    %7D
 %0A%7D%0A%0A

Fix Activity leak in ExpirationDateDialogTheme