package com.pixelnx.localhelp.ui.home;

import android.content.Context;

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.ui.base.ActivityBase;
import com.pixelnx.localhelp.utils.Consts;

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


import java.util.List;

public class AdapterDialogOrder extends RecyclerView.Adapter<AdapterDialogOrder.HolderDialogOrder> {
    private List<ModelSearchItem.Data> dataArrayList;
 Context mContext;
    private HomeInterface homeInterface;

    public AdapterDialogOrder(List<ModelSearchItem.Data> dataArrayList, Context mContext) {
        this.dataArrayList = dataArrayList;
        this.mContext = mContext;
        homeInterface = ((ActivityBase) mContext);
    }

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

    @Override
    public void onBindViewHolder(@NonNull final HolderDialogOrder holder, final int i) {
String name=""+dataArrayList.get(i).getSubCatName();
       holder.tvName.setText(name);
       double d=Double.parseDouble(dataArrayList.get(i).getRate())*Double.parseDouble(dataArrayList.get(i).getTyp());
      String price="Rate   ₹ "+dataArrayList.get(i).getRate()+" / "+dataArrayList.get(i).getItemStockWeight()+dataArrayList.get(i).getQntUnit()+"\nQty " +
              "    "+dataArrayList.get(i).getTyp()+" "+"\nItem Total =  ₹ "+d+"";
       holder.tvPrise.setText(price);
        holder.tvRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    dataArrayList.remove(i);
                    holder.itemView.setVisibility(View.GONE);
                    notifyDataSetChanged();
                    homeInterface.updateList(dataArrayList);


            }
        });
        Picasso
              .get()
                .load(Consts.Status.BASE_IMAGE_URL + dataArrayList.get(i).getImage())
                .placeholder(R.drawable.noimagee)
                .into(holder.imageView);
    }

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

    public static class HolderDialogOrder extends RecyclerView.ViewHolder {
        CustomTextMedium tvName;
        CustomTextMedium tvRemove;
        CustomTextMedium tvPrise;

        ImageView imageView;

        public HolderDialogOrder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvPrise = itemView.findViewById(R.id.tvPrise);
            tvRemove = itemView.findViewById(R.id.tvRemove);
            imageView = itemView.findViewById(R.id.ivImage);
        }
    }
}
