package com.example.petstoremobile.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petstoremobile.R;
import com.example.petstoremobile.dtos.CouponDTO;
import com.example.petstoremobile.utils.DateTimeUtils;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CouponAdapter extends RecyclerView.Adapter<CouponAdapter.ViewHolder> {
    private final List<CouponDTO> coupons;
    private final OnCouponClickListener listener;
    private boolean selectionMode = false;
    private final Set<Long> selectedIds = new HashSet<>();

    public interface OnCouponClickListener {
        void onCouponClick(CouponDTO coupon);
        void onSelectionChanged(int count);
    }

    public CouponAdapter(List<CouponDTO> coupons, OnCouponClickListener listener) {
        this.coupons = coupons;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_coupon, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CouponDTO coupon = coupons.get(position);
        holder.tvCouponCode.setText(coupon.getCouponCode());
        
        String discountText = "";
        if ("PERCENT".equals(coupon.getDiscountType())) {
            discountText = coupon.getDiscountValue().stripTrailingZeros().toPlainString() + "% OFF";
        } else {
            discountText = "$" + coupon.getDiscountValue().stripTrailingZeros().toPlainString() + " OFF";
        }
        holder.tvCouponDiscount.setText(discountText);
        
        holder.tvCouponMinOrder.setText("Min order: $" + coupon.getMinOrderAmount().stripTrailingZeros().toPlainString());
        
        if (coupon.getEndsAt() != null) {
            holder.tvCouponExpiry.setText("Expires: " + DateTimeUtils.extractDate(coupon.getEndsAt()));
            holder.tvCouponExpiry.setVisibility(View.VISIBLE);
        } else {
            holder.tvCouponExpiry.setVisibility(View.GONE);
        }

        if (Boolean.TRUE.equals(coupon.getActive())) {
            holder.tvCouponStatus.setText("ACTIVE");
            holder.tvCouponStatus.setBackgroundTintList(ContextCompat.getColorStateList(holder.itemView.getContext(), R.color.primary_dark));
        } else {
            holder.tvCouponStatus.setText("INACTIVE");
            holder.tvCouponStatus.setBackgroundTintList(ContextCompat.getColorStateList(holder.itemView.getContext(), R.color.accent_coral));
        }

        holder.cbSelectCoupon.setVisibility(selectionMode ? View.VISIBLE : View.GONE);
        holder.cbSelectCoupon.setChecked(selectedIds.contains(coupon.getCouponId()));

        holder.itemView.setOnClickListener(v -> {
            if (selectionMode) {
                toggleSelection(coupon.getCouponId());
            } else {
                listener.onCouponClick(coupon);
            }
        });

        holder.itemView.setOnLongClickListener(v -> {
            if (!selectionMode) {
                setSelectionMode(true);
                toggleSelection(coupon.getCouponId());
                return true;
            }
            return false;
        });

        holder.cbSelectCoupon.setOnClickListener(v -> toggleSelection(coupon.getCouponId()));
    }

    private void toggleSelection(Long id) {
        if (selectedIds.contains(id)) {
            selectedIds.remove(id);
        } else {
            selectedIds.add(id);
        }
        notifyDataSetChanged();
        listener.onSelectionChanged(selectedIds.size());
    }

    public void setSelectionMode(boolean selectionMode) {
        this.selectionMode = selectionMode;
        if (!selectionMode) selectedIds.clear();
        notifyDataSetChanged();
        listener.onSelectionChanged(selectedIds.size());
    }

    public Set<Long> getSelectedIds() {
        return selectedIds;
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvCouponCode, tvCouponDiscount, tvCouponMinOrder, tvCouponExpiry, tvCouponStatus;
        CheckBox cbSelectCoupon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCouponCode = itemView.findViewById(R.id.tvCouponCode);
            tvCouponDiscount = itemView.findViewById(R.id.tvCouponDiscount);
            tvCouponMinOrder = itemView.findViewById(R.id.tvCouponMinOrder);
            tvCouponExpiry = itemView.findViewById(R.id.tvCouponExpiry);
            tvCouponStatus = itemView.findViewById(R.id.tvCouponStatus);
            cbSelectCoupon = itemView.findViewById(R.id.cbSelectCoupon);
        }
    }
}
