/*
 * LockProgress.java
 *
 * Copyright (c) 2016 Auth0 (http://auth0.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.auth0.android.lock.views;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.auth0.android.lock.R;
import com.auth0.android.lock.internal.configuration.Theme;

public class ActionButton extends FrameLayout {

    private static final String TAG = ActionButton.class.getSimpleName();
    private ProgressBar progress;
    private ImageView icon;
    private LinearLayout labeledLayout;
    private TextView title;
    private boolean shouldShowLabel;

    public ActionButton(Context context, Theme lockTheme) {
        super(context);
        init(lockTheme);
    }

    private void init(Theme lockTheme) {
        inflate(getContext(), R.layout.com_auth0_lock_action_button, this);
        progress = (ProgressBar) findViewById(R.id.com_auth0_lock_progress);
        progress.setVisibility(View.GONE);
        icon = (ImageView) findViewById(R.id.com_auth0_lock_icon);
        labeledLayout = (LinearLayout) findViewById(R.id.com_auth0_lock_labeled);
        title = (TextView) findViewById(R.id.com_auth0_lock_title);

        ViewUtils.setBackground(icon, generateStateBackground(lockTheme));
        ViewUtils.setBackground(labeledLayout, generateStateBackground(lockTheme));
        showLabel(false);
    }

    private Drawable generateStateBackground(Theme lockTheme) {
        int normalColor = lockTheme.getPrimaryColor(getContext());
        int pressedColor = lockTheme.getDarkPrimaryColor(getContext());
        int disabledColor = ContextCompat.getColor(getContext(), R.color.com_auth0_lock_submit_disabled);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            return new RippleDrawable(ColorStateList.valueOf(pressedColor), new ColorDrawable(normalColor), null);
        } else {
            StateListDrawable states = new StateListDrawable();
            states.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, new ColorDrawable(pressedColor));
            states.addState(new int[]{android.R.attr.state_enabled}, new ColorDrawable(normalColor));
            states.addState(new int[]{}, new ColorDrawable(disabledColor));
            return states;
        }
    }

    /**
     * Used to display a progress bar and disable the button.
     *
     * @param show whether to show the progress bar or not.
     */
    public void showProgress(boolean show) {
        Log.v(TAG, show ? "Disabling the button while showing progress" : "Enabling the button and hiding progress");
        setEnabled(!show);
        progress.setVisibility(show ? VISIBLE : GONE);
        if (show) {
            icon.setVisibility(INVISIBLE);
            labeledLayout.setVisibility(INVISIBLE);
            return;
        }
        icon.setVisibility(shouldShowLabel ? GONE : VISIBLE);
        labeledLayout.setVisibility(!shouldShowLabel ? GONE : VISIBLE);
    }

    /**
     * Label to display in the button.
     *
     * @param stringRes the new resource to display as the button label.
     */
    public void setLabel(@StringRes int stringRes) {
        title.setText(stringRes);
    }

    /**
     * Whether to show an icon or a label with the current selected mode.
     * By default, it will show the icon.
     *
     * @param showLabel whether to show an icon or a label.
     */
    public void showLabel(boolean showLabel) {
        shouldShowLabel = showLabel;
        labeledLayout.setVisibility(showLabel ? VISIBLE : GONE);
        icon.setVisibility(!showLabel ? VISIBLE : GONE);
    }
}

@@ -1445,32 +1445,76 @@
 teListDrawable;%0A
+import android.support.annotation.Nullable;%0A
 import android.s
@@ -2951,24 +2951,198 @@
 bel(false);%0A
+        icon.setFocusable(true);%0A        icon.setFocusableInTouchMode(false);%0A        labeledLayout.setFocusable(true);%0A        labeledLayout.setFocusableInTouchMode(false);%0A
     %7D%0A%0A    p
@@ -3458,165 +3458,175 @@
     
-if (android.os.Build.VERSION.SDK_INT %3E= android.os.Build.VERSION_CODES.LOLLIPOP) %7B%0A            return new RippleDrawable(ColorStateList.valueOf(
+final StateListDrawable buttonDrawable = new StateListDrawable();%0A        buttonDrawable.addState(new int%5B%5D%7Bandroid.R.attr.state_enabled, android.R.attr.state_
 pressed
-Color)
+%7D
 , ne
@@ -3645,128 +3645,46 @@
 ble(
-normalColor), null);%0A        %7D else %7B%0A            StateListDrawable states = new StateListDrawable();%0A            states
+pressedColor));%0A        buttonDrawable
 .add
@@ -3742,36 +3742,36 @@
 id.R.attr.state_
-pres
+focu
 sed%7D, new ColorD
@@ -3798,34 +3798,38 @@
 ));%0A        
-    states
+buttonDrawable
 .addState(ne
@@ -3912,18 +3912,22 @@
     
-    states
+buttonDrawable
 .add
@@ -3984,44 +3984,407 @@
 ));%0A
-            return states;%0A        %7D
+%0A        if (android.os.Build.VERSION.SDK_INT %3E= android.os.Build.VERSION_CODES.LOLLIPOP) %7B%0A            return new RippleDrawable(ColorStateList.valueOf(pressedColor), buttonDrawable, null);%0A        %7D%0A        return buttonDrawable;%0A    %7D%0A%0A    @Override%0A    public void setOnClickListener(@Nullable OnClickListener l) %7B%0A        icon.setOnClickListener(l);%0A        labeledLayout.setOnClickListener(l);
 %0A   

allow ActionButton to be focused on non touch screens