package com.cscodetech.movers.adepter.find;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.cscodetech.movers.R;
import com.cscodetech.movers.databinding.VehicleTypeLayoutBinding;
import com.cscodetech.movers.model.find.FindLorryDataItem;
import com.cscodetech.movers.utils.SessionManager;

import java.util.List;

public class VehicleFindAdapter extends RecyclerView.Adapter<VehicleFindAdapter.MyViewHolder> {



    private Context mContext;
    private List<FindLorryDataItem> typeList;
    private RecyclerTouchListener listener;
    private int lastCheckedPos = 0;
    SessionManager sessionManager;

    public interface RecyclerTouchListener {
        public void onClickVehicleFindInfo(String item, int position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private VehicleTypeLayoutBinding binding;
        public MyViewHolder(View view) {
            super(view);
            binding = VehicleTypeLayoutBinding.bind(view);
        }
    }

    public VehicleFindAdapter(Context mContext, List<FindLorryDataItem> typeList, final RecyclerTouchListener listener) {
        this.mContext = mContext;
        this.typeList = typeList;
        this.listener = listener;


    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.vehicle_type_layout, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (lastCheckedPos == position) {
            holder.binding.lvlClick.setBackground(mContext.getDrawable(R.drawable.rounded_boxfill));
        } else {
            holder.binding.lvlClick.setBackground(mContext.getDrawable(R.drawable.rounded_box));
        }
        holder.binding.txtType.setText("" + typeList.get(position).getTitle());
        holder.binding.txtTonne.setText("" + typeList.get(position).getMinWeight() + " - " + typeList.get(position).getMaxWeight()+" "+ mContext.getResources().getString(R.string.tonnes));
        holder.binding.lvlClick.setOnClickListener(view -> {
            lastCheckedPos=position;
            listener.onClickVehicleFindInfo("", position);
            notifyDataSetChanged();
        });
    }

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