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.activity.BaseActivity;
import ch.pizzalemon.android.helper.PriceHelper;
import ch.pizzalemon.android.interfaces.RecyclerItemClickListener;
import ch.pizzalemon.android.model.menu.MenuProductOptionValueModel;

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

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

    private ArrayList<MenuProductOptionValueModel> productOptionValueList;
    private RecyclerItemClickListener recyclerItemClickListener;

    public static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.productOptionRadioButton) AppCompatRadioButton productOptionRadioButton;
        @BindView(R.id.optionPriceDescriptionTextView) TextView optionPriceDescriptionTextView;

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

    public ProductRadioOptionsRecyclerAdapter(ArrayList<MenuProductOptionValueModel> productOptionValueList){
        this.productOptionValueList = productOptionValueList;
    }

    public ProductRadioOptionsRecyclerAdapter(ArrayList<MenuProductOptionValueModel> productOptionValueList,
                                              RecyclerItemClickListener recyclerItemClickListener){
        this.productOptionValueList = productOptionValueList;
        this.recyclerItemClickListener = recyclerItemClickListener;
    }

    @Override
    public ProductRadioOptionsRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View root = LayoutInflater.from(BaseActivity.currentActivity).inflate(R.layout.row_option_radio, viewGroup, false);
        return new ProductRadioOptionsRecyclerAdapter.ViewHolder(root, recyclerItemClickListener);
    }

    @Override
    public void onBindViewHolder(ProductRadioOptionsRecyclerAdapter.ViewHolder holder, int position) {
        holder.productOptionRadioButton.setChecked(productOptionValueList.get(position).isSelected());
        holder.productOptionRadioButton.setText(productOptionValueList.get(position).getName());
        holder.optionPriceDescriptionTextView.setText(PriceHelper.getProductOptionPriceText(productOptionValueList.get(position)));

    }

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

    public void setRecyclerItemClickListener(RecyclerItemClickListener recyclerItemClickListener){
        this.recyclerItemClickListener = recyclerItemClickListener;
    }

}