package io.gisbi.extensions.datasource.plugin;

import io.gisbi.exception.DEException;
import io.gisbi.extensions.datasource.dto.DatasourceRequest;
import io.gisbi.extensions.datasource.provider.Provider;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.net.URI;
import java.nio.file.Paths;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @Author Junjun
 */
public abstract class DataEaseDatasourcePlugin extends Provider  {
    private final String DEFAULT_FILE_PATH = "/opt/gisbi2.0/drivers/plugin";


    @Override
    public List<String> getSchema(DatasourceRequest datasourceRequest) {
        return new ArrayList<>();
    }


//    @Override
//    public void loadPlugin() {
//        XpackPluginsDatasourceVO datasourceConfig = getConfig();
//        ProviderFactory.loadPlugin(datasourceConfig.getType(), this);
//        try {
//            loadDriver();
//        } catch (Exception e) {
//            DEException.throwException(e);
//        }
//    }

//    private void loadDriver() throws Exception {
//        XpackPluginsDatasourceVO config = getConfig();
//        String localPath = StringUtils.isEmpty(config.getDriverPath()) ? DEFAULT_FILE_PATH : config.getDriverPath();
//        ProtectionDomain protectionDomain = this.getClass().getProtectionDomain();
//        URI uri = protectionDomain.getCodeSource().getLocation().toURI();
//        try (JarFile jarFile = new JarFile(new File(uri))) {
//            Enumeration<JarEntry> entries = jarFile.entries();
//            while (entries.hasMoreElements()) {
//                JarEntry entry = entries.nextElement();
//                String name = entry.getName();
//                if (StringUtils.endsWith(name, ".jar")) {
//                    File file = new File(localPath, Paths.get(name).getFileName().toString());
//                    if (!file.getParentFile().exists()) {
//                        file.getParentFile().mkdirs();
//                    }
//
//                    try (InputStream inputStream = jarFile.getInputStream(entry);
//                         FileOutputStream outputStream = new FileOutputStream(file)) {
//                        byte[] bytes = new byte[1024];
//                        int length;
//                        while ((length = inputStream.read(bytes)) >= 0) {
//                            outputStream.write(bytes, 0, length);
//                        }
//                    }
//                }
//            }
//        }
//    }

//    public XpackPluginsDatasourceVO getConfig() {
//        DataEasePluginVO pluginInfo = null;
//        try {
//            pluginInfo = getPluginInfo();
//        } catch (Exception e) {
//            DEException.throwException(e);
//        }
//        String config = pluginInfo.getConfig();
//        XpackPluginsDatasourceVO vo = JsonUtil.parseObject(config, XpackPluginsDatasourceVO.class);
//        vo.setIcon(pluginInfo.getIcon());
//        return vo;
//    }


    public void unloadPlugin() {
        try {
            ProtectionDomain protectionDomain = this.getClass().getProtectionDomain();
            URI uri = protectionDomain.getCodeSource().getLocation().toURI();
            try (JarFile jarFile = new JarFile(new File(uri))) {
                Enumeration<JarEntry> entries = jarFile.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    String name = entry.getName();
                    if (StringUtils.endsWith(name, ".jar")) {
                        File file = new File(DEFAULT_FILE_PATH, Paths.get(name).getFileName().toString());
                        file.delete();
                    }
                }
            }
        } catch (Exception e) {
            DEException.throwException(e);
        }
    }
}
