package com.cscodetech.moverslorry.adepter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cscodetech.moverslorry.R;
import com.cscodetech.moverslorry.databinding.HomelorryLayoutBinding;
import com.cscodetech.moverslorry.model.BidLorryDataItem;
import com.cscodetech.moverslorry.retrofit.APIClient;
import com.cscodetech.moverslorry.ui.AddLorryActivity;

import java.util.List;

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


    private Context mContext;
    private List<BidLorryDataItem> typeList;



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

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

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

        }
    }

    public HomeLorryAdapter(Context mContext, List<BidLorryDataItem> typeList) {
        this.mContext = mContext;
        this.typeList = typeList;



    }

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

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

        Glide.with(mContext).load(APIClient.BASE_URL + "/" + typeList.get(position).getLorryImg()).thumbnail(Glide.with(mContext).load(R.drawable.tprofile)).into(holder.binding.imgt);
        holder.binding.txtTitle.setText("" + typeList.get(position).getLorryTitle());
        holder.binding.txtRouts.setText(typeList.get(position).getRoutes()+"+ "+mContext.getResources().getString(R.string.routs));

        if (typeList.get(position).getRcVerify().equalsIgnoreCase("1")) {
            holder.binding.txtRcverification.setCompoundDrawablesWithIntrinsicBounds(mContext.getDrawable(R.drawable.ic_document_done), null, null, null);
            holder.binding.txtRcverification.setText(mContext.getResources().getString(R.string.rc_verified));
        }else if(typeList.get(position).getRcVerify().equalsIgnoreCase("2")) {
            holder.binding.txtRcverification.setCompoundDrawablesWithIntrinsicBounds(mContext.getDrawable(R.drawable.ic_unverified), null, null, null);
            holder.binding.txtRcverification.setText("Document Reupload");
        }else {
            holder.binding.txtRcverification.setCompoundDrawablesWithIntrinsicBounds(mContext.getDrawable(R.drawable.ic_document_process), null, null, null);
            holder.binding.txtRcverification.setText(mContext.getResources().getString(R.string.rc_verifiednot));

        }
        holder.binding.txtLorrynumber.setText("" + typeList.get(position).getLorryNo());

        holder.binding.txtRouts.setOnClickListener(view -> displayAddressWindow(holder.binding.txtRouts, typeList.get(position).getTotalRoutes()));
        holder.binding.imgEdit.setOnClickListener(view -> mContext.startActivity(new Intent(mContext, AddLorryActivity.class).putExtra("my_class",typeList.get(position))));
    }

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

    private void displayAddressWindow(View anchorView, String address[]) {
        Activity activity = (Activity) mContext;
        PopupWindow popup = new PopupWindow(mContext);
        View layout = activity.getLayoutInflater().inflate(R.layout.popup_content1, null);

        ListView myListview = (ListView) layout.findViewById(R.id.list);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, R.layout.popup_content1_chail, address);
        myListview.setAdapter(adapter);
        popup.setContentView(layout);
        // Set content width and height
        popup.setHeight(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        popup.setWidth(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        // Closes the popup window when touch outside of it - when looses focus
        popup.setOutsideTouchable(true);
        popup.setFocusable(true);
        // Show anchored to button
        popup.setBackgroundDrawable(new ColorDrawable());
        popup.showAsDropDown(anchorView);
    }
}