package com.example.petstoremobile.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.example.petstoremobile.R;
import com.example.petstoremobile.databinding.ItemSaleBinding;
import com.example.petstoremobile.dtos.SaleDTO;
import java.util.List;

public class SaleAdapter extends RecyclerView.Adapter<SaleAdapter.SaleViewHolder> {

    private List<SaleDTO> saleList;
    private OnSaleClickListener listener;

    public interface OnSaleClickListener {
        void onSaleClick(int position);
    }

    public SaleAdapter(List<SaleDTO> saleList, OnSaleClickListener listener) {
        this.saleList = saleList;
        this.listener = listener;
    }

    public static class SaleViewHolder extends RecyclerView.ViewHolder {
        final ItemSaleBinding binding;

        public SaleViewHolder(@NonNull ItemSaleBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull SaleViewHolder holder, int position) {
        SaleDTO s = saleList.get(position);
        ItemSaleBinding binding = holder.binding;

        binding.tvSaleId.setText("Sale #" + (s.getSaleId() != null ? s.getSaleId() : ""));
        binding.tvSaleEmployee.setText("By: " + (s.getEmployeeName() != null ? s.getEmployeeName() : ""));
        if (s.getCustomerName() != null && !s.getCustomerName().isEmpty()) {
            binding.tvSaleCustomer.setText("Customer: " + s.getCustomerName());
            binding.tvSaleCustomer.setVisibility(View.VISIBLE);
        } else {
            binding.tvSaleCustomer.setVisibility(View.GONE);
        }
        binding.tvSaleDate.setText(s.getSaleDate() != null ? s.getSaleDate().substring(0, Math.min(10, s.getSaleDate().length())) : "");
        binding.tvSalePayment.setText(s.getPaymentMethod() != null ? s.getPaymentMethod() : "");
        binding.tvSaleTotal.setText(s.getTotalAmount() != null ? "$" + s.getTotalAmount() : "");

        if (Boolean.TRUE.equals(s.getIsRefund())) {
            binding.tvSaleRefundBadge.setVisibility(View.VISIBLE);
            binding.tvSaleTotal.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.status_adopted));
        } else {
            binding.tvSaleRefundBadge.setVisibility(View.GONE);
            binding.tvSaleTotal.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.status_available));
        }

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

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