package w.soulofpluto.posstock;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

 /*
 Create by Ramesh Babu
 */

public class PlanDetailsAdapter extends RecyclerView.Adapter<PlanDetailsAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Plans> arrayList;
    public PlanDetailsAdapter(Context context, ArrayList<Plans> storesArrayList){
        this.context       = context;
        this.arrayList     = storesArrayList;
    }
    @NonNull
    @Override
    public PlanDetailsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem = layoutInflater.inflate(R.layout.plansdetails_item, parent, false);
        PlanDetailsAdapter.ViewHolder viewHolder = new PlanDetailsAdapter.ViewHolder(listItem);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull PlanDetailsAdapter.ViewHolder holder, final int position){
        final Plans items = arrayList.get(position);
        holder.plandetails_barcode.setText(items.getBarcode());
        holder.plandetails_qty.setText(items.getQty());
        Log.e("PlanDetails",items.getPlanId());
        holder.plandetails_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    if(Constants.isNetworkAvailable(context)){
                       ((PlanDetails) context).DeleteBarcode(items,position);
                    }else{
                       Constants.getToast(context,context.getResources().getString(R.string.connection));
                    }
                }catch(NullPointerException np){
                    np.printStackTrace();
                }
            }
        });
    }
    @Override
    public int getItemCount(){
        return arrayList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView plandetails_barcode,plandetails_qty;
        ImageView plandetails_delete;
        public ViewHolder(View itemView){
            super(itemView);
            plandetails_barcode       = itemView.findViewById(R.id.plandetails_barcode);
            plandetails_qty           = itemView.findViewById(R.id.plandetails_qty);
            plandetails_delete        = itemView.findViewById(R.id.plandetails_delete);
        }
    }
}

