package com.ycwl.basic.order.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 订单事件管理器
 * 管理事件监听器的注册和事件分发
 */
@Slf4j
@Component
public class OrderEventManager {
    
    private final List<OrderEventListener> listeners = new ArrayList<>();
    private final Executor eventExecutor = Executors.newFixedThreadPool(5, 
            r -> new Thread(r, "order-event-thread"));
    
    @PostConstruct
    public void init() {
        log.info("订单事件管理器初始化完成");
    }
    
    /**
     * 注册事件监听器
     * 
     * @param listener 监听器实例
     */
    public void registerListener(OrderEventListener listener) {
        if (listener != null && !listeners.contains(listener)) {
            listeners.add(listener);
            log.info("注册订单事件监听器: {}", listener.getClass().getSimpleName());
        }
    }
    
    /**
     * 移除事件监听器
     * 
     * @param listener 监听器实例
     */
    public void removeListener(OrderEventListener listener) {
        if (listeners.remove(listener)) {
            log.info("移除订单事件监听器: {}", listener.getClass().getSimpleName());
        }
    }
    
    /**
     * 发布支付状态变更事件
     * 
     * @param event 支付状态变更事件
     */
    public void publishPaymentStatusChangeEvent(PaymentStatusChangeEvent event) {
        log.info("发布支付状态变更事件: orderId={}, {} -> {}", 
                event.getOrderId(), event.getOldPaymentStatus(), event.getNewPaymentStatus());
        
        CompletableFuture.runAsync(() -> {
            for (OrderEventListener listener : listeners) {
                try {
                    listener.onPaymentStatusChanged(event);
                } catch (Exception e) {
                    log.error("处理支付状态变更事件失败: listener={}, orderId={}", 
                            listener.getClass().getSimpleName(), event.getOrderId(), e);
                }
            }
        }, eventExecutor);
    }
    
    /**
     * 发布退款状态变更事件
     * 
     * @param event 退款状态变更事件
     */
    public void publishRefundStatusChangeEvent(RefundStatusChangeEvent event) {
        log.info("发布退款状态变更事件: orderId={}, refundId={}, {} -> {}", 
                event.getOrderId(), event.getRefundId(), 
                event.getOldRefundStatus(), event.getNewRefundStatus());
        
        CompletableFuture.runAsync(() -> {
            for (OrderEventListener listener : listeners) {
                try {
                    listener.onRefundStatusChanged(event);
                } catch (Exception e) {
                    log.error("处理退款状态变更事件失败: listener={}, orderId={}, refundId={}", 
                            listener.getClass().getSimpleName(), event.getOrderId(), event.getRefundId(), e);
                }
            }
        }, eventExecutor);
    }
    
    /**
     * 发布订单状态变更事件
     * 
     * @param event 订单状态变更事件
     */
    public void publishOrderStatusChangeEvent(OrderStatusChangeEvent event) {
        log.info("发布订单状态变更事件: orderId={}, {} -> {}", 
                event.getOrderId(), event.getOldOrderStatus(), event.getNewOrderStatus());
        
        CompletableFuture.runAsync(() -> {
            for (OrderEventListener listener : listeners) {
                try {
                    listener.onOrderStatusChanged(event);
                } catch (Exception e) {
                    log.error("处理订单状态变更事件失败: listener={}, orderId={}", 
                            listener.getClass().getSimpleName(), event.getOrderId(), e);
                }
            }
        }, eventExecutor);
    }
    
    /**
     * 获取已注册的监听器数量
     * 
     * @return 监听器数量
     */
    public int getListenerCount() {
        return listeners.size();
    }
}