package io.gisbi.extensions.datasource.factory;


import io.gisbi.exception.DEException;
import io.gisbi.extensions.datasource.plugin.DataEaseDatasourcePlugin;
import io.gisbi.extensions.datasource.provider.Provider;
import io.gisbi.extensions.datasource.utils.SpringContextUtil;
import io.gisbi.extensions.datasource.vo.DatasourceConfiguration;
import io.gisbi.extensions.datasource.vo.XpackPluginsDatasourceVO;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author Junjun
 */
public class ProviderFactory {

    public static Provider getProvider(String type) throws DEException {
        if (type.equalsIgnoreCase("es")) {
            return SpringContextUtil.getApplicationContext().getBean("esProvider", Provider.class);
        }
        List<String> list = Arrays.stream(DatasourceConfiguration.DatasourceType.values()).map(DatasourceConfiguration.DatasourceType::getType).toList();
        if (list.contains(type)) {
            return SpringContextUtil.getApplicationContext().getBean("calciteProvider", Provider.class);
        }
        Provider instance = getInstance(type);
        if (instance == null) {
            DEException.throwException("插件异常，请检查插件");
        }
        return instance;
    }

    public static Provider getDefaultProvider() {
        return SpringContextUtil.getApplicationContext().getBean("calciteProvider", Provider.class);
    }


    private static final Map<String, DataEaseDatasourcePlugin> templateMap = new ConcurrentHashMap<>();

    public static Provider getInstance(String type) {
//        if (!LicenseUtil.licenseValid()) DEException.throwException("插件功能只对企业版本可用！");
        String key = type;
        return templateMap.get(key);
    }

    public static void loadPlugin(String type, DataEaseDatasourcePlugin plugin) {
//        if (!LicenseUtil.licenseValid()) DEException.throwException("插件功能只对企业版本可用！");
//        String key = type;
//        if (templateMap.containsKey(key)) return;
//        templateMap.put(key, plugin);
//        try {
//            String moduleName = plugin.getPluginInfo().getModuleName();
//            DataEasePluginFactory.loadTemplate(moduleName, plugin);
//        } catch (Exception e) {
//            LogUtil.error(e.getMessage(), new Throwable(e));
//            DEException.throwException(e);
//        }
    }

    public static List<XpackPluginsDatasourceVO> getDsConfigList() {
//        if (!LicenseUtil.licenseValid()) DEException.throwException("插件功能只对企业版本可用！");
//        return templateMap.values().stream().map(DataEaseDatasourcePlugin::getConfig).toList();
        return null;
    }
}
