package com.kym.service.awoara.factory;

import com.kym.common.exception.BusinessException;
import com.kym.entity.awoara.Event;
import com.kym.service.FaultNotificationService;
import com.kym.service.MonitorLogService;
import com.kym.service.OrderSettlementService;
import com.kym.service.WashDeviceService;
import com.kym.service.WashOrderService;
import com.kym.service.awoara.AwoaraService;
import com.kym.service.awoara.event.handle.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 事件处理器工厂
 *
 * @author skyline
 */
@Component
public class AwoaraEventHandlerFactory {

    private static String DOMAIN;

    private static WashDeviceService washDeviceService;
    private static WashOrderService washOrderService;
    private static OrderSettlementService orderSettlementService;
    private static MonitorLogService monitorLogService;
    private static StringRedisTemplate stringRedisTemplate;
    private static AwoaraService awoaraService;
    private static FaultNotificationService faultNotificationService;


    public AwoaraEventHandlerFactory(WashDeviceService washDeviceService, WashOrderService washOrderService,
                                     OrderSettlementService orderSettlementService,
                                     MonitorLogService monitorLogService,
                                     StringRedisTemplate stringRedisTemplate,
                                     AwoaraService awoaraService,
                                     FaultNotificationService faultNotificationService,
    @Value("${kym.domain}") String domain) {
        DOMAIN = domain;
        AwoaraEventHandlerFactory.washDeviceService = washDeviceService;
        AwoaraEventHandlerFactory.washOrderService = washOrderService;
        AwoaraEventHandlerFactory.orderSettlementService = orderSettlementService;
        AwoaraEventHandlerFactory.monitorLogService = monitorLogService;
        AwoaraEventHandlerFactory.stringRedisTemplate = stringRedisTemplate;
        AwoaraEventHandlerFactory.awoaraService = awoaraService;
        AwoaraEventHandlerFactory.faultNotificationService = faultNotificationService;
    }

    public static AwoaraEventHandler getEventHandler(String eventName) {
        try {
            Event event = Event.valueOf(eventName);
            return switch (event) {
                case boot -> new BootEventHandler(washDeviceService, washOrderService, awoaraService, orderSettlementService);
                case device_state -> new DeviceStateEventHandler(washDeviceService, faultNotificationService);
                case order_create -> new OrderCreateEventHandler(washOrderService);
                case order_update -> new OrderUpdateEventHandler(washOrderService);
                case order_close ->
                        new OrderCloseEventHandler(washOrderService, orderSettlementService);
                case user_login -> new UserLoginEventHandler();
                case card_event -> new CardEventHandler();
                case device_status -> new DeviceStatusEventHandler(washDeviceService, monitorLogService, stringRedisTemplate, faultNotificationService);
            };
        } catch (IllegalArgumentException e) {
            throw new BusinessException("无效的事件名称: " + eventName);
        }
    }
}
