package com.stripe.android.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

public class AddPaymentMethodFpxView extends AddPaymentMethodView {
    @NonNull private final Adapter mAdapter;

    @NonNull
    public static AddPaymentMethodFpxView create(@NonNull Context context) {
        return new AddPaymentMethodFpxView(context);
    }

    private AddPaymentMethodFpxView(@NonNull Context context) {
        super(context);
        inflate(getContext(), R.layout.add_payment_method_fpx_layout, this);
        mAdapter = new Adapter();
        final RecyclerView recyclerView = findViewById(R.id.fpx_list);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setItemAnimator(null);
    }

    @Nullable
    @Override
    PaymentMethodCreateParams getCreateParams() {
        final FpxBank fpxBank = mAdapter.getSelectedBank();
        if (fpxBank == null) {
            return null;
        }

        return PaymentMethodCreateParams.create(
                new PaymentMethodCreateParams.Fpx.Builder()
                        .setBank(fpxBank.code)
                        .build()
        );
    }

    private static final class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
        private int mSelectedPosition = -1;

        private Adapter() {
            super();
            setHasStableIds(true);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            final View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.fpx_bank, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
            viewHolder.update(FpxBank.values()[i].displayName);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO(mshafrir): update UI for selected bank
                    mSelectedPosition = viewHolder.getAdapterPosition();
                }
            });
        }

        @Override
        public long getItemId(int position) {
            return FpxBank.values()[position].hashCode();
        }

        @Override
        public int getItemCount() {
            return FpxBank.values().length;
        }

        @Nullable
        FpxBank getSelectedBank() {
            if (mSelectedPosition == -1) {
                return null;
            } else {
                return FpxBank.values()[mSelectedPosition];
            }
        }

        private static final class ViewHolder extends RecyclerView.ViewHolder {
            @NonNull private final TextView mName;

            private ViewHolder(@NonNull View itemView) {
                super(itemView);

                mName = itemView.findViewById(R.id.name);
            }

            void update(@NonNull String bankName) {
                mName.setText(bankName);
            }
        }
    }

    private enum FpxBank {
        // TODO(mshafrir): add complete bank list

        AffinBank("affin_bank", "Affin Bank"),
        AllianceBankBusiness("alliance_bank", "Alliance Bank (Business)"),
        AmBank("ambank", "AmBank"),
        BankMuamalat("bank_muamalat", "Bank Muamalat"),
        DeutscheBank("deutsche_bank", "Deutsche Bank"),
        Maybank2e("maybank2e", "Maybank2E"),
        PublicBankEnterprise("pb_enterprise", "PB Enterprise"),
        StandardChartered("standard_chartered", "Standard Chartered"),
        UobBank("uob", "UOB Bank"),
        UobRegional("uob_regional", "UOB Regional");

        @NonNull private final String code;
        @NonNull private final String displayName;

        FpxBank(@NonNull String code, @NonNull String displayName) {
            this.code = code;
            this.displayName = displayName;
        }
    }
}

@@ -922,16 +922,40 @@
 Adapter(
+new ThemeConfig(context)
 );%0A     
@@ -1790,24 +1790,114 @@
     
-private Adapter(
+@NonNull private final ThemeConfig mThemeConfig;%0A%0A        private Adapter(@NonNull ThemeConfig themeConfig
 ) %7B%0A
@@ -1944,32 +1944,72 @@
 tableIds(true);%0A
+            mThemeConfig = themeConfig;%0A
         %7D%0A%0A     
@@ -2300,16 +2300,30 @@
 itemView
+, mThemeConfig
 );%0A     
@@ -2422,32 +2422,92 @@
 older, int i) %7B%0A
+            viewHolder.setSelected(i == mSelectedPosition);%0A
             view
@@ -2793,35 +2793,301 @@
         
-mSelectedPosition =
+final int currentPosition = viewHolder.getAdapterPosition();%0A                    if (currentPosition != mSelectedPosition) %7B%0A                        final int prevSelectedPosition = mSelectedPosition;%0A                        mSelectedPosition = currentPosition;%0A                       
  viewHol
@@ -3090,34 +3090,97 @@
 wHolder.
-getAdapter
+setSelected(true);%0A                        notifyItemChanged(prevSelected
 Position
 ();%0A    
@@ -3163,35 +3163,116 @@
 SelectedPosition
-(
 );
+%0A                        notifyItemChanged(currentPosition);%0A                    %7D
 %0A               
@@ -3901,24 +3901,85 @@
 tView mName;
+%0A            @NonNull private final ThemeConfig mThemeConfig;
 %0A%0A          
@@ -4021,16 +4021,50 @@
 itemView
+, @NonNull ThemeConfig themeConfig
 ) %7B%0A    
@@ -4092,17 +4092,16 @@
 mView);%0A
-%0A
         
@@ -4146,24 +4146,68 @@
 R.id.name);%0A
+                mThemeConfig = themeConfig;%0A
             
@@ -4307,32 +4307,173 @@
 );%0A            %7D
+%0A%0A            void setSelected(boolean isSelected) %7B%0A                mName.setTextColor(mThemeConfig.getTextColor(isSelected));%0A            %7D
 %0A        %7D%0A    %7D

Add styling to selected FPX bank (#1404)