package com.cscodetech.moverslorry.adepter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
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.LorryBookLayoutBinding;
import com.cscodetech.moverslorry.model.LorrydataItem;
import com.cscodetech.moverslorry.retrofit.APIClient;
import com.cscodetech.moverslorry.ui.BidderInfoActivity;

import java.util.List;

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



    private Context mContext;
    private List<LorrydataItem> lorrydataItemList;
    private RecyclerTouchListener listener;


    public interface RecyclerTouchListener {
        public void onClickLorryInfo(LorrydataItem item, int position);
    }

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


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

        }
    }

    public LorryBookAdapter(Context mContext, List<LorrydataItem> typeList, final RecyclerTouchListener listener) {
        this.mContext = mContext;
        this.lorrydataItemList = typeList;
        this.listener = listener;


    }

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

    @Override
    public void onBindViewHolder(final MyViewHolder holder, @SuppressLint("RecyclerView") int position) {

        LorrydataItem item = lorrydataItemList.get(position);
        holder.binding.txtTrackname.setText(item.getLorryTitle());
        holder.binding.txtLocation.setText(item.getPickupPoint());
        holder.binding.txtLocation2.setText(item.getDropPoint());
        holder.binding.txtVnumber.setText(item.getLorryNo());
        holder.binding.txtRates.setText(item.getReview());
        holder.binding.txtWeight.setText("" + item.getWeight() + " " + mContext.getResources().getString(R.string.tonnes));
        holder.binding.txtRouts.setText( item.getRoutesCount()+" "+ mContext.getResources().getString(R.string.routes));
        holder.binding.txtRcVerified.setText(R.string.rc_verified);
        if (item.getRcVerify().equalsIgnoreCase("1")) {
            holder.binding.txtRcVerified.setCompoundDrawablesWithIntrinsicBounds(mContext.getDrawable(R.drawable.ic_document_done), null, null, null);
        }

        holder.binding.txtName.setText(item.getLorryOwnerTitle());
        Glide.with(mContext).load(APIClient.BASE_URL + "/" + item.getLorryOwnerImg()).thumbnail(Glide.with(mContext).load(R.drawable.tprofile)).into(holder.binding.imgs);
        Glide.with(mContext).load(APIClient.BASE_URL + "/" + item.getLorryImg()).thumbnail(Glide.with(mContext).load(R.drawable.tprofile)).into(holder.binding.imgTrak);

        String lastWord = item.getCurrLocation().substring(item.getCurrLocation().lastIndexOf(",") + 3);
        Log.e("Location ", "--" + lastWord);


        holder.binding.txtBooknow.setOnClickListener(view -> listener.onClickLorryInfo(item, position));

        holder.binding.imgs.setOnClickListener(view -> mContext.startActivity(new Intent(mContext, BidderInfoActivity.class)
                .putExtra("uid", item.getLorryOwnerId())));
    }

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