package ch.pizzalemon.android.adapter.recycler;

import androidx.appcompat.widget.AppCompatRadioButton;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.interfaces.RecyclerItemClickListener;
import ch.pizzalemon.android.model.PaymentMethodModel;

/**
 * Created by cimenmus on 17/10/2017.
 */

public class PaymentMethodsRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private final int HOLDER_PAYMENT_METHOD = 0;
    private final int HOLDER_SPACE = 1;

    private ArrayList<PaymentMethodModel> paymentMethodList = new ArrayList<>();
    private RecyclerItemClickListener recyclerItemClickListener;

    public static class OrderViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.paymentMethodRadioButton) AppCompatRadioButton paymentMethodRadioButton;

        public OrderViewHolder(final View view, final RecyclerItemClickListener recyclerItemClickListener) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(recyclerItemClickListener != null)
                        recyclerItemClickListener.onItemClick(view, getAdapterPosition());
                }
            });
        }
    }

    public static class SpaceViewHolder extends RecyclerView.ViewHolder{
        public SpaceViewHolder(final View view) {
            super(view);
        }
    }

    @Override
    public int getItemViewType(int position) {

        if(position == paymentMethodList.size())
            return HOLDER_SPACE;

        return HOLDER_PAYMENT_METHOD;
    }

    public PaymentMethodsRecyclerAdapter(ArrayList<PaymentMethodModel> paymentMethodList,
                                          RecyclerItemClickListener recyclerItemClickListener){
        this.paymentMethodList = paymentMethodList;
        this.recyclerItemClickListener = recyclerItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        RecyclerView.ViewHolder viewHolder;
        View view;
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        switch (viewType){

            case HOLDER_PAYMENT_METHOD:
                view = inflater.inflate(R.layout.row_payment_method, viewGroup, false);
                viewHolder = new OrderViewHolder(view, recyclerItemClickListener);
                break;

            case HOLDER_SPACE:
                view = inflater.inflate(R.layout.row_space_4dp, viewGroup, false);
                viewHolder = new SpaceViewHolder(view);
                break;

            default:
                view = inflater.inflate(R.layout.row_payment_method, viewGroup, false);
                viewHolder = new OrderViewHolder(view, recyclerItemClickListener);
                break;
        }

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        switch (holder.getItemViewType()){
            case HOLDER_PAYMENT_METHOD :
                OrderViewHolder orderViewHolder = (OrderViewHolder) holder;
                orderViewHolder.paymentMethodRadioButton.setText(paymentMethodList.get(position).getTitle());
                orderViewHolder.paymentMethodRadioButton.setChecked(paymentMethodList.get(position).isSelected());
                break;

            case HOLDER_SPACE :
                SpaceViewHolder spaceViewHolder = (SpaceViewHolder) holder;
                break;
        }

    }

    @Override
    public int getItemCount() {
        return paymentMethodList.size() + 1 ;
    }

}