package com.cf.imes.module.executor.service.orderImport.factory;

import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
import com.cf.imes.module.executor.enums.ErrorCodeConstants;
import com.cf.imes.module.executor.enums.OrderImportTypeEnum;
import com.cf.imes.module.executor.service.orderImport.OrderImportHandler;
import com.cf.imes.module.executor.service.orderImport.handler.api.AbstractApiOrderImportHandler;
import com.cf.imes.module.executor.service.orderImport.handler.excel.AbstractExcelOrderImportHandler;
import com.cf.imes.module.executor.service.orderImport.handler.json.AbstractJsonOrderImportHandler;
import com.cf.imes.module.executor.service.orderImport.handler.xml.AbstractXmlOrderImportHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

/**
 * 生产单导入处理器工厂类
 *
 * @author Gqr
 * @since 2024/11/28 16:21
 */
@Component
public class OrderImportHandlerFactory {
    private final ApplicationContext applicationContext;

    @Autowired
    public OrderImportHandlerFactory(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * 根据导入类型获取对应的处理器
     *
     * @param type        导入类型
     * @param factoryName 工厂名称预留
     * @return
     */
    public OrderImportHandler getOrderImportHandler(int type, String factoryName) {
        OrderImportTypeEnum importTypeEnum = OrderImportTypeEnum.match(type);
        // 没有传入工厂/厂商用default
        if (StringUtils.isEmpty(factoryName)) {
            factoryName = "default";
        }
        ServiceException typeNotSupportException = ServiceExceptionUtil.exception(ErrorCodeConstants.ORDER_IMPORT_TYPE_NOT_SUPPORT, type);
        switch (importTypeEnum) {
            case EXCEL:
                return Optional.ofNullable(getExcelHandler(factoryName)).orElseThrow(() -> typeNotSupportException);
            case JSON:
                return Optional.ofNullable(getJsonHandler(factoryName)).orElseThrow(() -> typeNotSupportException);
            case XML:
                return Optional.ofNullable(getXmlHandler(factoryName)).orElseThrow(() -> typeNotSupportException);
            case API:
                return Optional.ofNullable(getApiHandler(factoryName)).orElseThrow(() -> typeNotSupportException);
            default:
                throw typeNotSupportException;
        }
    }

    /**
     * 获取excel处理类
     *
     * @param factoryName
     * @return
     */
    private OrderImportHandler getExcelHandler(String factoryName) {
        for (Map.Entry<String, AbstractExcelOrderImportHandler> entry : applicationContext.getBeansOfType(AbstractExcelOrderImportHandler.class).entrySet()) {
            AbstractExcelOrderImportHandler entryValue = entry.getValue();
            if (factoryName.equals(entryValue.getFactoryName())) {
                return entryValue;
            }
        }
        return null;
    }

    /**
     * 获取json处理类
     *
     * @param factoryName
     * @return
     */
    private OrderImportHandler getJsonHandler(String factoryName) {
        for (Map.Entry<String, AbstractJsonOrderImportHandler> entry : applicationContext.getBeansOfType(AbstractJsonOrderImportHandler.class).entrySet()) {
            AbstractJsonOrderImportHandler entryValue = entry.getValue();
            if (factoryName.equals(entryValue.getFactoryName())) {
                return entryValue;
            }
        }
        return null;
    }

    /**
     * 获取xml处理类
     *
     * @param factoryName
     * @return
     */
    private OrderImportHandler getXmlHandler(String factoryName) {
        for (Map.Entry<String, AbstractXmlOrderImportHandler> entry : applicationContext.getBeansOfType(AbstractXmlOrderImportHandler.class).entrySet()) {
            AbstractXmlOrderImportHandler entryValue = entry.getValue();
            if (factoryName.equals(entryValue.getFactoryName())) {
                return entryValue;
            }
        }
        return null;
    }

    /**
     * 获取api处理类
     *
     * @param factoryName
     * @return
     */
    private OrderImportHandler getApiHandler(String factoryName) {
        for (Map.Entry<String, AbstractApiOrderImportHandler> entry : applicationContext.getBeansOfType(AbstractApiOrderImportHandler.class).entrySet()) {
            AbstractApiOrderImportHandler entryValue = entry.getValue();
            if (factoryName.equals(entryValue.getFactoryName())) {
                return entryValue;
            }
        }
        return null;
    }
}
