package cd.casic.plugin.utils;

import cd.casic.plugin.PluginDescriptorStorage;
import cd.casic.plugin.pf4j.OpsPluginDescriptor;
import cn.hutool.core.util.StrUtil;
import org.pf4j.PluginDependency;

import java.util.*;
import java.util.stream.Collectors;

public class PluginDescriptorUtils {
    public static final String PLUGIN_ID = "plugin.id";
    public static final String PLUGIN_DESCRIPTION = "plugin.description";
    public static final String PLUGIN_CLASS = "plugin.class";
    public static final String PLUGIN_VERSION = "plugin.version";
    public static final String PLUGIN_PROVIDER = "plugin.provider";
    public static final String PLUGIN_DEPENDENCIES = "plugin.dependencies";
    public static final String PLUGIN_REQUIRES = "plugin.requires";
    public static final String PLUGIN_LICENSE = "plugin.license";
    public static final String MYBATIS_MAPPER_LOCATION = "mybatis.mapper.location";
    public static final String STATIC_LOCATIONS = "static.locations";
    public static final String CONFIG_FILE_NAME = "plugin.config.file";
    public static final String CONFIG_FILE_ACTIVE = "plugin.config.active";

    public static PluginDescriptorStorage propertiesToStorage(Properties properties){
        PluginDescriptorStorage pluginDescriptor = new PluginDescriptorStorage();

        String id = properties.getProperty(PLUGIN_ID);
        pluginDescriptor.setPluginId(id);

        String description = properties.getProperty(PLUGIN_DESCRIPTION);
        if (StrUtil.isEmptyIfStr(description)) {
            pluginDescriptor.setPluginDescription("");
        } else {
            pluginDescriptor.setPluginDescription(description);
        }

        String clazz = properties.getProperty(PLUGIN_CLASS);
        if (!StrUtil.isEmptyIfStr(clazz)) {
            pluginDescriptor.setPluginClass(clazz);
        }

        String version = properties.getProperty(PLUGIN_VERSION);
        if (!StrUtil.isEmptyIfStr(version)) {
            pluginDescriptor.setVersion(version);
        }

        String provider = properties.getProperty(PLUGIN_PROVIDER);
        pluginDescriptor.setProvider(provider);

        String requires = properties.getProperty(PLUGIN_REQUIRES);
        if (StrUtil.isEmptyIfStr(requires)) {
            pluginDescriptor.setRequires("0.0.0");
        }else{
            pluginDescriptor.setRequires(requires);
        }

        String mapperXmlDir = properties.getProperty(MYBATIS_MAPPER_LOCATION);
        pluginDescriptor.setMapperXmlDir(mapperXmlDir);

        String staticDir = properties.getProperty(STATIC_LOCATIONS);
        pluginDescriptor.setStaticDir(staticDir);

        String license = properties.getProperty(PLUGIN_LICENSE);
        pluginDescriptor.setLicense(license);

        String dependencies = properties.getProperty(PLUGIN_DEPENDENCIES);
        if(!StrUtil.isEmptyIfStr(dependencies)){
            List<PluginDependency> dependencyList = Arrays.stream(dependencies.trim().split(","))
                    .map(dependency->new PluginDependency(dependency.trim()))
                    .collect(Collectors.toList());
            pluginDescriptor.setDependencies(dependencyList);
        }else {
            pluginDescriptor.setDependencies(Collections.emptyList());
        }

        String configFileName = properties.getProperty(CONFIG_FILE_NAME);
        if(!StrUtil.isEmptyIfStr(configFileName)){
            pluginDescriptor.setConfigFileName(configFileName);
        }

        List<String> configFileActives = new ArrayList<>();
        for (String key : properties.stringPropertyNames()) {
            if (key.startsWith(CONFIG_FILE_ACTIVE)) {
                configFileActives.add(properties.getProperty(key));
            }
        }
        if(!configFileActives.isEmpty()){
            pluginDescriptor.setConfigFileActive(configFileActives);
        }else{
            pluginDescriptor.setConfigFileActive(Collections.emptyList());
        }

        return pluginDescriptor;
    }

//    public static DefaultPluginDescriptor storageToDescriptor(PluginDescriptorStorage pluginDescriptor){
//        return new DefaultPluginDescriptor(pluginDescriptor.getPluginId(),
//                pluginDescriptor.getPluginDescription(),
//                pluginDescriptor.getPluginClass(),
//                pluginDescriptor.getVersion(),
//                pluginDescriptor.getRequires(),
//                pluginDescriptor.getProvider(),
//                pluginDescriptor.getLicense());
//    }

    public static OpsPluginDescriptor storageToDescriptor(PluginDescriptorStorage pluginDescriptor){
        return new OpsPluginDescriptor(pluginDescriptor.getPluginId(),
                pluginDescriptor.getPluginDescription(),
                pluginDescriptor.getPluginClass(),
                pluginDescriptor.getVersion(),
                pluginDescriptor.getRequires(),
                pluginDescriptor.getProvider(),
                pluginDescriptor.getLicense(),
                pluginDescriptor.getConfigFileName(),
                pluginDescriptor.getConfigFileActive());
    }
}
