package w.soulofpluto.stockauditean;

import android.app.Activity;
import android.content.Context;
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;
    PreferenceManager preferenceManager;

    public PlanDetailsAdapter(Context context, ArrayList<Plans> storesArrayList){
        this.context       = context;
        this.arrayList     = storesArrayList;
        preferenceManager  = new PreferenceManager(context);
    }

    @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());
        holder.planDetails_delete  . setOnClickListener(view -> {
           try{
              if(Constants.isNetworkAvailable(context)){
                ((PlanDetails) context).DeleteBarcode(items,position);
              }else{
                ((Activity)context).runOnUiThread(() -> 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);
        }
    }
}

