package com.auth0.android.lock.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

@SuppressLint("Instantiatable")
class SocialButton extends RelativeLayout {

    private static final int NORMAL_STATE_ALPHA = 230;
    private static final float FOCUSED_STATE_ALPHA = 0.64f;
    private static final String STRATEGY_GOOGLE_OAUTH2 = "google-oauth2";

    private ImageView icon;
    private TextView title;

    public SocialButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SocialButton(Context context) {
        super(context);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.com_auth0_lock_btn_social_large, this);
        icon = findViewById(R.id.com_auth0_lock_icon);
        title = findViewById(R.id.com_auth0_lock_text);
        setFocusableInTouchMode(false);
        setFocusable(true);
        setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                v.setAlpha(hasFocus ? FOCUSED_STATE_ALPHA : 1f);
            }
        });
    }

    private StateListDrawable getTouchFeedbackBackground(@ColorInt int pressedColor, @ViewUtils.Corners int corners) {
        ShapeDrawable normalBackground = ViewUtils.getRoundedBackground(this, pressedColor, corners);
        normalBackground.getPaint().setAlpha(NORMAL_STATE_ALPHA);
        Drawable pressedBackground = ViewUtils.getRoundedBackground(this, pressedColor, corners);

        StateListDrawable states = new StateListDrawable();
        states.addState(new int[]{android.R.attr.state_pressed}, pressedBackground);
        states.addState(new int[]{}, normalBackground);
        return states;
    }

    /**
     * Configures the button with the given connection information.
     *
     * @param config contains the connection information.
     * @param mode   the current button mode. Used to prefix the title with "Log In" or "Sign Up".
     */
    public void setStyle(AuthConfig config, @AuthMode int mode) {
        final Drawable logo = config.getLogo(getContext());
        final int backgroundColor = config.getBackgroundColor(getContext());
        Drawable touchBackground = getTouchFeedbackBackground(backgroundColor, ViewUtils.Corners.ONLY_RIGHT);

        /*
         * Branding guidelines command we remove the padding for Google logo.
         * Since it's the only exception to the rule, handle it this way.
         *
         * Source: https://developers.google.com/identity/branding-guidelines
         */
        if (STRATEGY_GOOGLE_OAUTH2.equalsIgnoreCase(config.getConnection().getStrategy())) {
            icon.setPadding(0, 0, 0, 0);
        }
        icon.setImageDrawable(logo);
        final String name = config.getName(getContext());
        ShapeDrawable leftBackground = ViewUtils.getRoundedBackground(this, backgroundColor, ViewUtils.Corners.ONLY_LEFT);
        final String prefixFormat = getResources().getString(mode == AuthMode.LOG_IN ? R.string.com_auth0_lock_social_log_in : R.string.com_auth0_lock_social_sign_up);
        title.setText(String.format(prefixFormat, name));
        ViewUtils.setBackground(icon, leftBackground);
        ViewUtils.setBackground(title, touchBackground);
    }

}

@@ -104,16 +104,47 @@
 ontext;%0A
+import android.graphics.Color;%0A
 import a
@@ -778,82 +778,8 @@
 64f;
-%0A    private static final String STRATEGY_GOOGLE_OAUTH2 = %22google-oauth2%22;
 %0A%0A  
@@ -1692,32 +1692,38 @@
 ners) %7B%0A        
+final 
 ShapeDrawable no
@@ -1720,16 +1720,304 @@
 rawable 
+normalBackground;%0A        final ShapeDrawable pressedBackground;%0A%0A        boolean shouldDrawOutline = pressedColor == Color.WHITE;%0A        if (shouldDrawOutline) %7B%0A            int outlineColor = getResources().getColor(R.color.com_auth0_lock_social_light_background_focused);%0A            
 normalBa
@@ -2039,32 +2039,39 @@
 Utils.getRounded
+Outline
 Background(this,
@@ -2069,120 +2069,252 @@
 und(
-this, pressedColor, corners);%0A        normalBackground.getPaint().setAlpha(NORMAL_STATE_ALPHA);%0A        Drawable
+getResources(), outlineColor);%0A            pressedBackground = ViewUtils.getRoundedBackground(this, outlineColor, corners);%0A        %7D else %7B%0A            normalBackground = ViewUtils.getRoundedBackground(this, pressedColor, corners);%0A           
  pre
@@ -2390,16 +2390,94 @@
 orners);
+%0A        %7D%0A%0A        pressedBackground.getPaint().setAlpha(NORMAL_STATE_ALPHA);
 %0A%0A      
@@ -3253,18 +3253,11 @@
 ers.
-ONLY_RIGHT
+ALL
 );%0A%0A
@@ -3269,622 +3269,403 @@
    /
-*%0A         * Branding guidelines command we remove the padding for Google logo.%0A         * Since it's the only exception to the rule, handle it this way.%0A         *%0A         * Source: https://developers.google.com/identity/branding-guidelines%0A         */%0A        if (STRATEGY_GOOGLE_OAUTH2.equalsIgnoreCase(config.getConnection().getStrategy())) %7B%0A            icon.setPadding(0, 0, 0, 0);%0A        %7D%0A        icon.setImageDrawable(logo);%0A        final String name = config.getName(getContext());%0A        ShapeDrawable leftBackground = ViewUtils.getRoundedBackground(this, backgroundColor, ViewUtils.Corners.ONLY_LEFT
+/ When background is white, change default text color%0A        boolean shouldUseDarkText = backgroundColor == Color.WHITE;%0A        if (shouldUseDarkText) %7B%0A            int textColor = getResources().getColor(R.color.com_auth0_lock_social_text_light);%0A            title.setTextColor(textColor);%0A        %7D%0A        icon.setImageDrawable(logo);%0A        final String name = config.getName(getContext()
 );%0A 
@@ -3925,68 +3925,12 @@
 und(
-icon, leftBackground);%0A        ViewUtils.setBackground(title
+this
 , to

Make SocialButton aware of white backgrounds