package com.kym.service.awoara.factory;

import com.kym.common.exception.BusinessException;
import com.kym.entity.awoara.Event;
import com.kym.service.*;
import com.kym.service.awoara.event.handle.*;
import org.springframework.beans.factory.annotation.Value;
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 WalletDetailService walletDetailService;
    private static AccountService accountService;

    private static StationAccountService stationAccountService;
    private static SplitRecordService splitRecordService;

    private static StationAccountRecordService stationAccountRecordService;

    private static MpMsgTemplateService mpMsgTemplateService;

    public AwoaraEventHandlerFactory(WashDeviceService washDeviceService, WashOrderService washOrderService, WalletDetailService walletDetailService,
                                     AccountService accountService, StationAccountService stationAccountService, SplitRecordService splitRecordService,
                                     StationAccountRecordService stationAccountRecordService, MpMsgTemplateService mpMsgTemplateService,
    @Value("${kym.domain}") String domain) {
        DOMAIN = domain;
        AwoaraEventHandlerFactory.washDeviceService = washDeviceService;
        AwoaraEventHandlerFactory.washOrderService = washOrderService;
        AwoaraEventHandlerFactory.walletDetailService = walletDetailService;
        AwoaraEventHandlerFactory.accountService = accountService;
        AwoaraEventHandlerFactory.stationAccountService = stationAccountService;
        AwoaraEventHandlerFactory.splitRecordService = splitRecordService;
        AwoaraEventHandlerFactory.stationAccountRecordService = stationAccountRecordService;
        AwoaraEventHandlerFactory.mpMsgTemplateService = mpMsgTemplateService;
    }

    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, stationAccountService, splitRecordService, stationAccountRecordService, mpMsgTemplateService,DOMAIN);
                case user_login -> new UserLoginEventHandler();
                case card_event -> new CardEventHandler();
            };
        } catch (IllegalArgumentException e) {
            throw new BusinessException("无效的事件名称: " + eventName);
        }
    }
}
