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 android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.helper.PriceHelper;
import ch.pizzalemon.android.interfaces.RecyclerItemClickListener;
import ch.pizzalemon.android.model.ShippingMethodModel;

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

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

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

    private ArrayList<ShippingMethodModel> shippingMethodList = new ArrayList<>();
    private RecyclerItemClickListener recyclerItemClickListener;

    public static class OrderViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.shipmentMethodRadioButton) AppCompatRadioButton shipmentMethodRadioButton;
        @BindView(R.id.shipmentMethodPriceTextView) TextView shipmentMethodPriceTextView;

        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 == shippingMethodList.size())
            return HOLDER_SPACE;

        return HOLDER_SHIPPING_METHOD;
    }

    public ShippingMethodsRecyclerAdapter(ArrayList<ShippingMethodModel> shippingMethodList,
                                       RecyclerItemClickListener recyclerItemClickListener){
        this.shippingMethodList = shippingMethodList;
        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_SHIPPING_METHOD:
                view = inflater.inflate(R.layout.row_shipment_method, viewGroup, false);
                viewHolder = new OrderViewHolder(view, recyclerItemClickListener);
                break;

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

            default:
                view = inflater.inflate(R.layout.row_shipment_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_SHIPPING_METHOD :
                OrderViewHolder orderViewHolder = (OrderViewHolder) holder;
                orderViewHolder.shipmentMethodPriceTextView.setText(PriceHelper.roundFractions(shippingMethodList.get(position).getPriceText()));
                orderViewHolder.shipmentMethodRadioButton.setText(shippingMethodList.get(position).getTitle());
                orderViewHolder.shipmentMethodRadioButton.setChecked(shippingMethodList.get(position).isSelected());
                break;

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

    }

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

}