package com.cscodetech.moverslorry.adepter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cscodetech.moverslorry.R;
import com.cscodetech.moverslorry.databinding.VehicleLayoutBinding;
import com.cscodetech.moverslorry.model.VehicleDataItem;
import com.cscodetech.moverslorry.retrofit.APIClient;
import com.cscodetech.moverslorry.ui.AddLorryActivity;

import java.util.List;

public class VehicleAdapter extends RecyclerView.Adapter<VehicleAdapter.MyViewHolder> {


    private Context mContext;
    int tonne;
    String isSelect;
    private List<VehicleDataItem> typeList;
    private RecyclerTouchListener listener;
    private int lastCheckedPos = -1;


    public interface RecyclerTouchListener {
        public void onClickVehicleInfo(VehicleDataItem item, int position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private VehicleLayoutBinding binding;





        public MyViewHolder(View view) {
            super(view);
            binding = VehicleLayoutBinding.bind(view);
        }
    }

    public VehicleAdapter(Context mContext, List<VehicleDataItem> typeList, String isSelect, final RecyclerTouchListener listener) {
        this.mContext = mContext;
        this.typeList = typeList;
        this.listener = listener;
        this.isSelect = isSelect;
        lastCheckedPos = -1;


    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.vehicle_layout, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, @SuppressLint("RecyclerView") int position) {

        VehicleDataItem item = typeList.get(position);
        Glide.with(mContext).load(APIClient.BASE_URL + "/" + item.getImg()).thumbnail(Glide.with(mContext).load(R.drawable.tprofile)).into(holder.binding.img);
        holder.binding.txtName.setText(item.getTitle());
        holder.binding.txtType.setText(item.getMinWeight() + " - " + item.getMaxWeight() + " Tonnes");

        if (lastCheckedPos == position) {
            holder.binding.lvlClick.setBackground(mContext.getDrawable(R.drawable.round_boxfill));
        } else {
            holder.binding.lvlClick.setBackground(mContext.getDrawable(R.drawable.round_box));
        }

        if (isSelect!=null && isSelect.equalsIgnoreCase(item.getTitle())) {
            isSelect = "0";
            if (tonne < Integer.parseInt(item.getMaxWeight())) {
                lastCheckedPos = position;
                listener.onClickVehicleInfo(item, position);
                //delay button
                holder.binding.lvlClick.postDelayed(() -> {
                    notifyDataSetChanged();

                }, 800);

            }

        }
        holder.binding.lvlClick.setOnClickListener(view -> {
            Log.e("Liclclc", "djk");
            Log.e("Liclclc", ""+tonne);
            if(!TextUtils.isEmpty(AddLorryActivity.getInstance().binding.edTypeweight.getText())){

                if (Integer.parseInt(AddLorryActivity.getInstance().binding.edTypeweight.getText().toString()) < Integer.parseInt(item.getMaxWeight())) {
                    lastCheckedPos = position;
                    listener.onClickVehicleInfo(item, position);
                    notifyDataSetChanged();
                }else {
                    AddLorryActivity.getInstance().tempAddLorry.setVehicleId(null);
                    Toast.makeText(mContext,"The vehicle types do not match to the number of tonnes.",Toast.LENGTH_SHORT).show();
                }
            }else {
                AddLorryActivity.getInstance().binding.edTypeweight.setError("");
            }


        });
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }
}