package org.opencb.bionetdb.lib.utils;

import org.opencb.bionetdb.core.config.DownloadProperties;
import org.opencb.commons.utils.FileUtils;
import org.opencb.commons.utils.URLUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;

public class Downloader {

    private DownloadProperties downloadProperties;
    private Path outDir;

    private Logger logger;

    public Downloader() {
        this.logger = LoggerFactory.getLogger(this.getClass().toString());
    }

    public Downloader(DownloadProperties downloadProperties, Path outDir) {
        this.downloadProperties = downloadProperties;
        this.outDir = outDir;

        this.logger = LoggerFactory.getLogger(this.getClass().toString());
    }

    public void download() throws IOException {
        FileUtils.checkDirectory(outDir);

        // Ensembl gene
        if (downloadProperties.getEnsemblGene() != null) {
            try {
                downloadURL(new URL(downloadProperties.getEnsemblGene().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

        // RefSeq gene
        if (downloadProperties.getRefSeqGene() != null) {
            try {
                downloadURL(new URL(downloadProperties.getRefSeqGene().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

        // Protein
        if (downloadProperties.getProtein() != null) {
            try {
                downloadURL(new URL(downloadProperties.getProtein().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

        // Panel
        if (downloadProperties.getPanel() != null) {
            try {
                downloadURL(new URL(downloadProperties.getPanel().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

        // Clinical variant
        if (downloadProperties.getClinicalVariant() != null) {
            try {
                downloadURL(new URL(downloadProperties.getClinicalVariant().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

        // Reactome
        if (downloadProperties.getNetwork() != null) {
            try {
                downloadURL(new URL(downloadProperties.getNetwork().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

    }

    private void downloadURL(URL url, Path outDir) throws IOException {
        logger.info("Downloading " + url + "...");
        URLUtils.download(url, outDir);
        logger.info(url + " downloaded!");
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Downloader{");
        sb.append("downloadProperties=").append(downloadProperties);
        sb.append(", outDir=").append(outDir);
        sb.append('}');
        return sb.toString();
    }

    public DownloadProperties getDownloadProperties() {
        return downloadProperties;
    }

    public Downloader setDownloadProperties(DownloadProperties downloadProperties) {
        this.downloadProperties = downloadProperties;
        return this;
    }

    public Path getOutDir() {
        return outDir;
    }

    public Downloader setOutDir(Path outDir) {
        this.outDir = outDir;
        return this;
    }
}

@@ -235,16 +235,37 @@
 ctory;%0A%0A
+import java.io.File;%0A
 import j
@@ -2761,32 +2761,271 @@
 s IOException %7B%0A
+        String filename = new File(url.getFile()).getName();%0A        if (outDir.resolve(filename).toFile().exists()) %7B%0A            logger.info(%22Skipping to downloadd %22 + filename + %22. It already exists in %22 + outDir);%0A        %7D else %7B%0A    
         logger.i
@@ -3067,16 +3067,20 @@
         
+  
+  
 URLUtils
@@ -3095,32 +3095,36 @@
 d(url, outDir);%0A
+    
         logger.i
@@ -3146,24 +3146,34 @@
 nloaded!%22);%0A
+        %7D%0A
     %7D%0A%0A    @

check if the files to download exist in the output directory before downloading