package com.pixelnx.localhelp.ui.storeitems;

import android.content.Context;
import android.content.Intent;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;

import com.pixelnx.localhelp.R;
import com.pixelnx.localhelp.model.ModelSearchItem;


import com.pixelnx.localhelp.utils.Consts;
import com.pixelnx.localhelp.utils.widgets.CustomTextBold;
import com.pixelnx.localhelp.utils.widgets.CustomTextMedium;
import com.squareup.picasso.Picasso;



import java.util.List;

public class AdapterItemList extends RecyclerView.Adapter<AdapterItemList.HolderSearchAdapter> {
    List<ModelSearchItem.Data> subcategoryData;
    Context mContext;



    public AdapterItemList(Context mContext, List<ModelSearchItem.Data> subcategoryData) {
        this.subcategoryData = subcategoryData;
        this.mContext = mContext;

    }

    @NonNull
    @Override
    public HolderSearchAdapter onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_itemlist, viewGroup, false);
        return new HolderSearchAdapter(view);

    }

    @Override
    public void onBindViewHolder(@NonNull final HolderSearchAdapter holder, int i) {
        final ModelSearchItem.Data data = subcategoryData.get(i);
        holder.tvItemName.setText(data.getSubCatName());

        if(data.getItemMrp().equalsIgnoreCase("0")){
            holder.tvItemMrp.setVisibility(View.GONE);
            holder.cut.setVisibility(View.GONE);
        }else{
            holder.tvItemMrp.setVisibility(View.VISIBLE);
            holder.cut.setVisibility(View.VISIBLE);
        holder.tvItemMrp.setText("MRP : ₹"+data.getItemMrp()+"  ");}
        holder.tvItemPrise.setText("Rate :  ₹"+data.getRate() + "  / "+data.getItemStockWeight() + data.getQntUnit());
holder.tvItemQty.setText("Stock Qty :  "+data.getStockQnt());


        Picasso
          .get()
                .load(Consts.Status.BASE_IMAGE_URL + data.getImage())
                .placeholder(R.drawable.noimagecart)
                .into(holder.ivImage);

        holder.tvAddToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(mContext,ActivityAddItem.class);
                intent.putExtra("update","update");
                    intent.putExtra("data",  data);
                mContext.startActivity(intent);


            }
        });

    }

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

    public static class HolderSearchAdapter extends RecyclerView.ViewHolder {

        CustomTextBold tvAddToCart;

        CustomTextMedium tvItemName;
        CustomTextMedium  tvItemPrise;
        CustomTextMedium  tvItemQty;
        CustomTextMedium  tvItemMrp;

        ImageView ivImage;
        View cut;

        public HolderSearchAdapter(@NonNull View itemView) {
            super(itemView);
            tvAddToCart = itemView.findViewById(R.id.tvAddToCart);

            ivImage = itemView.findViewById(R.id.ivImage);
            cut = itemView.findViewById(R.id.cut);

            tvItemName = itemView.findViewById(R.id.tvItemName);
            tvItemPrise = itemView.findViewById(R.id.tvItemPrise);
            tvItemQty = itemView.findViewById(R.id.tvItemQty);
            tvItemMrp = itemView.findViewById(R.id.tvItemMrp);


        }
    }

}
