package com.kym.service.awoara.factory;

import com.kym.common.exception.BusinessException;
import com.kym.service.awoara.entity.event.DeviceStateObject;
import com.kym.service.awoara.entity.event.Event;
import com.kym.service.awoara.entity.event.handle.*;
import com.kym.service.miniapp.AccountService;
import com.kym.service.miniapp.WalletDetailService;
import com.kym.service.miniapp.WashDeviceService;
import com.kym.service.miniapp.WashOrderService;
import org.springframework.stereotype.Component;

/**
 * 事件处理器工厂
 *
 * @author skyline
 */
@Component
public class AwoaraEventHandlerFactory {
    private static WashDeviceService washDeviceService;
    private static WashOrderService washOrderService;
    private static WalletDetailService walletDetailService;
    private static AccountService accountService;

    public AwoaraEventHandlerFactory(WashDeviceService washDeviceService,WashOrderService washOrderService, WalletDetailService walletDetailService, AccountService accountService) {
        AwoaraEventHandlerFactory.washDeviceService = washDeviceService;
        AwoaraEventHandlerFactory.washOrderService = washOrderService;
        AwoaraEventHandlerFactory.walletDetailService = walletDetailService;
        AwoaraEventHandlerFactory.accountService = accountService;
    }

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