package com.stripe.android.view;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.stripe.android.R;
import com.stripe.android.model.PaymentMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * A {@link RecyclerView.Adapter} that holds a set of {@link MaskedCardView} items for a given set
 * of {@link PaymentMethod} objects.
 */
class MaskedCardAdapter extends RecyclerView.Adapter<MaskedCardAdapter.ViewHolder> {

    private static final int NO_SELECTION = -1;
    @NonNull private final List<PaymentMethod> mPaymentMethods;
    private int mSelectedIndex = NO_SELECTION;

    MaskedCardAdapter(@NonNull List<PaymentMethod> paymentMethods) {
        mPaymentMethods = new ArrayList<>();
        setPaymentMethods(paymentMethods);
    }

    void setPaymentMethods(@NonNull List<PaymentMethod> paymentMethods) {
        final PaymentMethod selectedPaymentMethod = getSelectedPaymentMethod();
        final String selectedPaymentMethodId =
                selectedPaymentMethod != null ? selectedPaymentMethod.id : null;

        mPaymentMethods.clear();
        mPaymentMethods.addAll(paymentMethods);

        // if there were no selected payment methods, or the previously selected payment method
        // was not found and set selected, select the newest payment method
        if (selectedPaymentMethodId == null || !setSelectedPaymentMethod(selectedPaymentMethodId)) {
            setSelectedIndex(getNewestPaymentMethodIndex());
        }

        notifyDataSetChanged();
    }

    private int getNewestPaymentMethodIndex() {
        int index = NO_SELECTION;
        Long created = 0L;
        for (int i = 0; i < mPaymentMethods.size(); i++) {
            final PaymentMethod paymentMethod = mPaymentMethods.get(i);
            if (paymentMethod.created != null && paymentMethod.created > created) {
                created = paymentMethod.created;
                index = i;
            }
        }

        return index;
    }

    @Override
    public int getItemCount() {
        return mPaymentMethods.size();
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.setMaskedCardData(mPaymentMethods.get(position));
        holder.setIndex(position);
        holder.setSelected(position == mSelectedIndex);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        FrameLayout itemView = (FrameLayout) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.masked_card_row, parent, false);
        return new ViewHolder(itemView);
    }

    /**
     * Sets the selected payment method to the one whose ID is identical to the input string, if
     * such a value is found.
     *
     * @param paymentMethodId a stripe ID to search for among the list of {@link PaymentMethod}
     *         objects
     * @return {@code true} if the value was found, {@code false} if not
     */
    boolean setSelectedPaymentMethod(@NonNull String paymentMethodId) {
        for (int i = 0; i < mPaymentMethods.size(); i++) {
            if (paymentMethodId.equals(mPaymentMethods.get(i).id)) {
                setSelectedIndex(i);
                return true;
            }
        }
        return false;
    }

    @Nullable
    String getSelectedPaymentMethodId() {
        if (mSelectedIndex == NO_SELECTION) {
            return null;
        }
        return mPaymentMethods.get(mSelectedIndex).id;
    }

    @Nullable
    PaymentMethod getSelectedPaymentMethod() {
        if (mSelectedIndex == NO_SELECTION) {
            return null;
        }

        return mPaymentMethods.get(mSelectedIndex);
    }

    void setSelectedIndex(int selectedIndex) {
        mSelectedIndex = selectedIndex;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @NonNull private final MaskedCardView maskedCardView;
        private int index;

        ViewHolder(FrameLayout itemLayout) {
            super(itemLayout);
            maskedCardView = itemLayout.findViewById(R.id.masked_card_item);
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!maskedCardView.isSelected()) {
                        maskedCardView.toggleSelected();
                        setSelectedIndex(index);
                        notifyDataSetChanged();
                    }
                }
            });
        }

        void setMaskedCardData(@NonNull PaymentMethod paymentMethod) {
            maskedCardView.setPaymentMethod(paymentMethod);
        }

        void setIndex(int index) {
            this.index = index;
        }

        void setSelected(boolean selected) {
            maskedCardView.setSelected(selected);
        }
    }
}

@@ -257,43 +257,8 @@
 oup;
-%0Aimport android.widget.FrameLayout;
 %0A%0Aim
@@ -2285,24 +2285,30 @@
 er(@NonNull 
+final 
 ViewHolder h
@@ -2399,43 +2399,8 @@
 ));%0A
-        holder.setIndex(position);%0A
     
@@ -2443,32 +2443,508 @@
 SelectedIndex);%0A
+        holder.itemView.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View view) %7B%0A                final int currentPosition = holder.getAdapterPosition();%0A                if (!holder.mMaskedCardView.isSelected()) %7B%0A                    holder.mMaskedCardView.toggleSelected();%0A                    setSelectedIndex(currentPosition);%0A                    notifyDataSetChanged();%0A                %7D%0A            %7D%0A        %7D);%0A
     %7D%0A%0A    @NonN
@@ -3057,44 +3057,29 @@
     
-FrameLayout itemView = (FrameLayout)
+final View itemView =
  Lay
@@ -4391,16 +4391,23 @@
  %7D%0A%0A    
+static 
 class Vi
@@ -4497,16 +4497,17 @@
 rdView m
+M
 askedCar
@@ -4513,16 +4513,17 @@
 rdView;%0A
+%0A
         
@@ -4534,61 +4534,41 @@
 ate 
-int index;%0A%0A        ViewHolder(FrameLayout itemLayout
+ViewHolder(@NonNull View itemView
 ) %7B%0A
@@ -4589,22 +4589,20 @@
 per(item
-Layout
+View
 );%0A     
@@ -4601,32 +4601,33 @@
 );%0A            m
+M
 askedCardView = 
@@ -4630,22 +4630,20 @@
 w = item
-Layout
+View
 .findVie
@@ -4684,420 +4684,8 @@
     
-    itemLayout.setOnClickListener(new View.OnClickListener() %7B%0A                @Override%0A                public void onClick(View view) %7B%0A                    if (!maskedCardView.isSelected()) %7B%0A                        maskedCardView.toggleSelected();%0A                        setSelectedIndex(index);%0A                        notifyDataSetChanged();%0A                    %7D%0A                %7D%0A            %7D);%0A        
 %7D%0A%0A 
@@ -4682,32 +4682,40 @@
       %7D%0A%0A       
+ private
  void setMaskedC
@@ -4767,32 +4767,33 @@
  %7B%0A            m
+M
 askedCardView.se
@@ -4846,85 +4846,15 @@
     
-void setIndex(int index) %7B%0A            this.index = index;%0A        %7D%0A%0A       
+private
  voi
@@ -4900,16 +4900,17 @@
        m
+M
 askedCar

Make MaskedCardAdapter.ViewHolder a static inner class (#1374)