package io.card.payment.ui;

/* ActivityHelper.java
 * See the file "LICENSE.md" for the full license governing this code.
 */

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class ActivityHelper {
    /**
     * Request the ActionBar window feature if we are on a supported Android
     * version. This should be called before the activity's setContentView.
     *
     * @param activity
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static void addActionBarIfSupported(Activity activity) {
        if (actionBarSupported(activity)) {
            activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);
        }
    }

    /**
     * Sets up the title, appearance, and behavior of the ActionBar if it is
     * supported. Should be invoked after
     * {@link #addActionBarIfSupported(Activity)}.
     *
     * @param activity
     * @param titleTextView
     * @param title
     * @param titleTextViewPrefix
     * @param icon
     */
    public static void setupActionBarIfSupported(Activity activity, TextView titleTextView,
                                                 String title, String titleTextViewPrefix, Drawable icon) {
        if (titleTextViewPrefix == null) {
            titleTextViewPrefix = "";
        }
        activity.setTitle(titleTextViewPrefix + title);
        if (actionBarSupported(activity)) {
            setupActionBar(activity, title, icon);
            if (titleTextView != null) {
                titleTextView.setVisibility(View.GONE);
            }
        } else {
            if (titleTextView != null) {
                titleTextView.setText(title);
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private static void setupActionBar(Activity activity, String title, Drawable icon) {
        ActionBar bar = activity.getActionBar();

        bar.setBackgroundDrawable(Appearance.ACTIONBAR_BACKGROUND);
        bar.setTitle(title);

        // Hack to set the actionbar text to white
        int actionBarTitleId =
                Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
        TextView actionBarTextView = (TextView) activity.findViewById(actionBarTitleId);
        if (actionBarTextView != null) {
            actionBarTextView.setTextColor(Color.WHITE);
        }

        bar.setDisplayHomeAsUpEnabled(false);
        if (icon != null
                && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            setActionBarHomeIcon(bar, icon);
            // bar.setDisplayHomeAsUpEnabled(true);
        } else {
            bar.setDisplayShowHomeEnabled(false);
        }
    }

    private static boolean actionBarSupported(Activity activity) {
        return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB
                && activity.getActionBar() != null;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private static void setActionBarHomeIcon(ActionBar bar, Drawable icon) {
        bar.setIcon(icon);
    }

    public static boolean holoSupported() {
        return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB;
    }

    /**
     * Overrides whatever theme the activity currently has with either
     * Theme_Holo_Light or Theme_Light, depending on OS support.
     *
     * @param activity
     * @param useApplicationTheme
     */

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static void setActivityTheme(Activity activity, boolean useApplicationTheme) {

        if (useApplicationTheme && 0 != activity.getApplicationInfo().theme) {
            activity.setTheme(activity.getApplicationInfo().theme);
        } else if (holoSupported()) {
            activity.setTheme(android.R.style.Theme_Holo_Light);
        } else {
            activity.setTheme(android.R.style.Theme_Light);
        }
    }
}

@@ -811,37 +811,29 @@
 arSupported(
-activity
 )) %7B%0A
+
             
@@ -1602,16 +1602,38 @@
 pported(
+) && actionBarNonNull(
 activity
@@ -1926,32 +1926,198 @@
        %7D%0A    %7D%0A%0A
+    @TargetApi(Build.VERSION_CODES.HONEYCOMB)%0A    private static boolean actionBarNonNull(Activity activity) %7B%0A        return activity.getActionBar() != null;%0A    %7D%0A%0A
     @TargetApi(B
@@ -3159,33 +3159,16 @@
 pported(
-Activity activity
 ) %7B%0A    
@@ -3258,59 +3258,8 @@
 COMB
-%0A                && activity.getActionBar() != null
 ;%0A  

check the actionBar only when setting it up, as there are side effects