package com.cscodetech.moverslorry.adepter;

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.bumptech.glide.Glide;
import com.cscodetech.moverslorry.R;
import com.cscodetech.moverslorry.databinding.LoadLayoutBinding;
import com.cscodetech.moverslorry.model.LoadHistoryDataItem;
import com.cscodetech.moverslorry.retrofit.APIClient;
import com.cscodetech.moverslorry.utils.SessionManager;
import com.cscodetech.moverslorry.utils.Utility;

import java.util.List;

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

    private Context mContext;
    private List<LoadHistoryDataItem> loadHistoryDataItems;
    private RecyclerTouchListener listener;
    SessionManager sessionManager;

    public interface RecyclerTouchListener {
        void onClickLoadPost(LoadHistoryDataItem item, int position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private LoadLayoutBinding binding;

        public MyViewHolder(View view) {
            super(view);
            binding = LoadLayoutBinding.bind(view);
        }
    }

    public MyLoadAdapter(Context mContext, List<LoadHistoryDataItem> typeList, final RecyclerTouchListener listener) {
        this.mContext = mContext;
        this.loadHistoryDataItems = typeList;
        this.listener = listener;
        sessionManager = new SessionManager(mContext);
    }

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

    @Override
    public void onBindViewHolder(final MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        LoadHistoryDataItem item = loadHistoryDataItems.get(position);
        Glide.with(mContext)
                .load(APIClient.BASE_URL + "/" + item.getVehicleImg())
                .thumbnail(Glide.with(mContext).load(R.drawable.tprofile))
                .into(holder.binding.imgt);
        holder.binding.txtTitle.setText("" + item.getVehicleTitle());
        holder.binding.txtPrice.setText(sessionManager.getStringData(SessionManager.currency) + item.getAmount());
        holder.binding.txtTonne.setText(" /" + item.getAmtType());
        holder.binding.txtPick.setText("" + item.getPickupState());
        holder.binding.txtDrop.setText("" + item.getDropState());
        holder.binding.txtDate.setText("" + Utility.getInstance().parseDateToddMMyy(item.getPostDate()));
        holder.binding.txtStaus.setText("" + item.getLoadStatus());
        holder.binding.txtDistnt.setText("" + item.getLoadDistance());

        holder.binding.lvlClicl.setOnClickListener(view -> listener.onClickLoadPost(item, position));
    }

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

    public List<LoadHistoryDataItem> getItemList() {
        return loadHistoryDataItems;
    }
}