package org.opencb.bionetdb.lib.utils;

import org.opencb.bionetdb.core.config.DownloadProperties;
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() {
        // Gene
        if (downloadProperties.getGene() != null) {
            try {
                System.out.println("Downloading " + downloadProperties.getGene().getHost() + "...");
                URLUtils.download(new URL(downloadProperties.getGene().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

        // Protein
        if (downloadProperties.getProtein() != null) {
            try {
                System.out.println("Downloading " + downloadProperties.getProtein().getHost() + "...");
                URLUtils.download(new URL(downloadProperties.getProtein().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

//        // Panel
//        if (downloadProperties.getPanel() != null) {
//            try {
//                System.out.println("Downloading " + downloadProperties.getPanel().getHost() + "...");
//                URLUtils.download(new URL(downloadProperties.getPanel().getHost()), outDir);
//            } catch (IOException e) {
//                logger.error(e.getMessage());
//            }
//        }

        // Clinical variant
        if (downloadProperties.getClinicalVariant() != null) {
            try {
                System.out.println("Downloading " + downloadProperties.getClinicalVariant().getHost() + "...");
                URLUtils.download(new URL(downloadProperties.getClinicalVariant().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

        // Reactome
        if (downloadProperties.getNetwork() != null) {
            try {
                System.out.println("Downloading " + downloadProperties.getNetwork().getHost() + "...");
                URLUtils.download(new URL(downloadProperties.getNetwork().getHost()), outDir);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

    }

    @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;
    }
}

@@ -887,126 +887,16 @@
     
-System.out.println(%22Downloading %22 + downloadProperties.getGene().getHost() + %22...%22);%0A                URLUtils.
 download
 (new
@@ -883,32 +883,35 @@
         download
+URL
 (new URL(downloa
@@ -1174,129 +1174,16 @@
     
-System.out.println(%22Downloading %22 + downloadProperties.getProtein().getHost() + %22...%22);%0A                URLUtils.
 download
 (new
@@ -1170,32 +1170,35 @@
         download
+URL
 (new URL(downloa
@@ -1468,129 +1468,16 @@
     
-System.out.println(%22Downloading %22 + downloadProperties.getPanel().getHost() + %22...%22);%0A//                URLUtils.
 download
 (new
@@ -1464,32 +1464,35 @@
         download
+URL
 (new URL(downloa
@@ -1781,137 +1781,16 @@
     
-System.out.println(%22Downloading %22 + downloadProperties.getClinicalVariant().getHost() + %22...%22);%0A                URLUtils.
 download
 (new
@@ -1777,32 +1777,35 @@
         download
+URL
 (new URL(downloa
@@ -2080,129 +2080,16 @@
     
-System.out.println(%22Downloading %22 + downloadProperties.getNetwork().getHost() + %22...%22);%0A                URLUtils.
 download
 (new
@@ -2084,16 +2084,19 @@
 download
+URL
 (new URL
@@ -2261,24 +2261,242 @@
   %7D%0A%0A    %7D%0A%0A
+    private void downloadURL(URL url, Path outDir) throws IOException %7B%0A        logger.info(%22Downloading %22 + url + %22...%22);%0A        URLUtils.download(url, this.outDir);%0A        logger.info(url + %22 downloaded!%22);%0A%0A    %7D%0A
     @Overrid

update Downloader, #55