package com.example.petstoremobile.adapters;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.petstoremobile.R;
import com.example.petstoremobile.api.UserApi;
import com.example.petstoremobile.databinding.ItemCustomerBinding;
import com.example.petstoremobile.dtos.CustomerDTO;
import com.example.petstoremobile.utils.GlideUtils;

import java.util.List;

public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.CustomerViewHolder> {

    private List<CustomerDTO> list;
    private OnCustomerClickListener listener;
    private String baseUrl;
    private String token;

    public interface OnCustomerClickListener {
        void onCustomerClick(int position);
    }

    public CustomerAdapter(List<CustomerDTO> list, OnCustomerClickListener listener) {
        this.list = list;
        this.listener = listener;
    }

    public void setBaseUrl(String baseUrl) { this.baseUrl = baseUrl; }
    public void setToken(String token) { this.token = token; }

    public static class CustomerViewHolder extends RecyclerView.ViewHolder {
        final ItemCustomerBinding binding;

        public CustomerViewHolder(@NonNull ItemCustomerBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    @NonNull
    @Override
    public CustomerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCustomerBinding binding = ItemCustomerBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false);
        return new CustomerViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull CustomerViewHolder holder, int position) {
        CustomerDTO c = list.get(position);
        ItemCustomerBinding b = holder.binding;

        b.tvCustomerFullName.setText(c.getFullName() != null ? c.getFullName() : "");
        b.tvCustomerUsername.setText("@" + (c.getUsername() != null ? c.getUsername() : ""));
        b.tvCustomerEmail.setText(c.getEmail() != null ? c.getEmail() : "");

        int points = c.getLoyaltyPoints() != null ? c.getLoyaltyPoints() : 0;
        b.tvCustomerLoyalty.setText(points + " pts");

        boolean active = Boolean.TRUE.equals(c.getActive());
        b.tvCustomerStatus.setText(active ? "Active" : "Inactive");
        b.tvCustomerStatus.setTextColor(active ? Color.parseColor("#4CAF50") : Color.parseColor("#F44336"));

        if (baseUrl != null && c.getCustomerId() != null) {
            String imageUrl = baseUrl + String.format(UserApi.AVATAR_PATH, c.getCustomerId());
            GlideUtils.loadImageWithTokenCircle(holder.itemView.getContext(), b.ivCustomerProfile, imageUrl, token, R.drawable.placeholder);
        } else {
            b.ivCustomerProfile.setImageResource(R.drawable.placeholder);
        }

        holder.itemView.setOnClickListener(v -> listener.onCustomerClick(position));
    }

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