package ch.pizzalemon.android.adapter.recycler;

import androidx.appcompat.widget.AppCompatCheckBox;
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 ProductCheckboxOptionsRecyclerAdapter extends RecyclerView.Adapter<ProductCheckboxOptionsRecyclerAdapter.ViewHolder> {

    private ArrayList<MenuProductOptionValueModel> productOptionValueList;
    private RecyclerItemClickListener recyclerItemClickListener;

    public static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.productOptionCheckBox) AppCompatCheckBox productOptionCheckBox;
        @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 ProductCheckboxOptionsRecyclerAdapter(ArrayList<MenuProductOptionValueModel> productOptionValueList){
        this.productOptionValueList = productOptionValueList;
    }

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

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

    @Override
    public void onBindViewHolder(ProductCheckboxOptionsRecyclerAdapter.ViewHolder holder, int position) {
        holder.productOptionCheckBox.setChecked(productOptionValueList.get(position).isSelected());
        holder.productOptionCheckBox.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;
    }

}