package com.braintreepayments.api.dropin.view;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.braintreepayments.api.dropin.R;
import com.braintreepayments.api.dropin.interfaces.AddPaymentUpdateListener;
import com.braintreepayments.api.models.Configuration;
import com.braintreepayments.cardform.OnCardFormSubmitListener;
import com.braintreepayments.cardform.OnCardFormValidListener;
import com.braintreepayments.cardform.utils.CardType;
import com.braintreepayments.cardform.view.CardForm;
import com.braintreepayments.cardform.view.SupportedCardTypesView;

public class AddCardView extends LinearLayout implements OnCardFormSubmitListener, OnCardFormValidListener,
        OnClickListener {

    private CardForm mCardForm;
    private SupportedCardTypesView mSupportedCardTypesView;
    private AnimatedButtonView mAnimatedButtonView;
    private AddPaymentUpdateListener mListener;

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

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

    public AddCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public AddCardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        if (isInEditMode()) {
            return;
        }

        setOrientation(VERTICAL);

        LayoutInflater.from(getContext()).inflate(R.layout.bt_add_card, this, true);

        mCardForm = (CardForm) findViewById(R.id.bt_card_form);
        mCardForm.setOnCardFormSubmitListener(this);
        mCardForm.setOnCardFormValidListener(this);

        mSupportedCardTypesView = (SupportedCardTypesView) findViewById(R.id.bt_supported_card_types);
        mAnimatedButtonView = (AnimatedButtonView) findViewById(R.id.bt_animated_button_view);

        updateSupportedCardTypes();

        mAnimatedButtonView.setClickListener(this);
    }

    public void setup(Activity activity, Configuration configuration) {
        mCardForm.cardRequired(true)
                .setup(activity);

        mCardForm.getCardEditText().setDisplayCardTypeIcon(false);
        mAnimatedButtonView.setVisibility(configuration.getUnionPay().isEnabled() ? VISIBLE : GONE);
    }

    public void setAddPaymentUpdatedListener(AddPaymentUpdateListener listener) {
        mListener = listener;
    }

    public CardForm getCardForm() {
        return mCardForm;
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        mAnimatedButtonView.showButton();
    }

    @Override
    public void onClick(View view) {
        if (mCardForm.isValid()) {
            callAddPaymentUpdateListener();
        } else {
            mCardForm.validate();
        }
    }

    private void updateSupportedCardTypes() {
        mSupportedCardTypesView.setSupportedCardTypes(CardType.values());
    }

    @Override
    public void onCardFormSubmit() {
        if (mCardForm.isValid()) {
            mAnimatedButtonView.showLoading();
            callAddPaymentUpdateListener();
        } else {
            mCardForm.validate();
        }
    }

    @Override
    public void onCardFormValid(boolean valid) {
        if (valid) {
            mAnimatedButtonView.showLoading();
            callAddPaymentUpdateListener();
        }
    }

    private void callAddPaymentUpdateListener() {
        if (mListener != null) {
            mListener.onPaymentUpdated(this);
        }
    }
}

@@ -695,16 +695,99 @@
 rdType;%0A
+import com.braintreepayments.cardform.view.CardEditText.OnCardTypeChangedListener;%0A
 import c
@@ -1030,16 +1030,43 @@
 Listener
+, OnCardTypeChangedListener
  %7B%0A%0A    
@@ -2403,45 +2403,8 @@
 );%0A%0A
-        updateSupportedCardTypes();%0A%0A
     
@@ -2601,17 +2601,16 @@
 ivity);%0A
-%0A
         
@@ -2664,24 +2664,154 @@
 con(false);%0A
+        mCardForm.setOnCardTypeChangedListener(this);%0A%0A        mSupportedCardTypesView.setSupportedCardTypes(CardType.values());%0A%0A
         mAni
@@ -3238,32 +3238,314 @@
 utton();%0A    %7D%0A%0A
+    @Override%0A    public void onCardTypeChanged(CardType cardType) %7B%0A        if (cardType == CardType.EMPTY) %7B%0A            mSupportedCardTypesView.setSupportedCardTypes(CardType.values());%0A        %7D else %7B%0A            mSupportedCardTypesView.setSelected(cardType);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  
@@ -3730,135 +3730,8 @@
  %7D%0A%0A
-    private void updateSupportedCardTypes() %7B%0A        mSupportedCardTypesView.setSupportedCardTypes(CardType.values());%0A    %7D%0A%0A
     

Add OnCardTypeChangedListener to AddCardView