package org.pdxfinder.commandline;

import org.pdxfinder.utils.CbpTransformer;
import org.pdxfinder.utils.CbpTransformer.cbioType;
;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

@Component
public class FinderTransformer {

    private static final Logger log = LoggerFactory.getLogger(FinderTransformer.class);

    @Value("${data-dir}")
    private String defaultDirectory;
    private String defaultTemplateDirectory;
    private String defaultExportDirectory;
    private CbpTransformer cbioTransformer;
    private File rootDir;
    private File templateDir;
    private File exportDir;
    private cbioType resolvedCbioType;

    @Autowired
    FinderTransformer(CbpTransformer cbioTransformer){
       this.cbioTransformer = cbioTransformer;
    }

    void run(File dataDirectory, File overideTemplateDir, File overideExportDir, File ingestFile, String cmdCbioType) throws IOException {
        resolveDirEnv(dataDirectory,overideTemplateDir,overideExportDir);
        if (cmdCbioType != null && !cmdCbioType.isEmpty()){
           runCbioportal(ingestFile, cmdCbioType);
        }
    }

    private void resolveDirEnv(File dataDirectory, File overideTemplateDir, File overideExportDir) throws IOException{
        if(dataDirectory != null && dataDirectory.exists()){
            log.info(String.format("Using %s as root directory", dataDirectory));
            rootDir = dataDirectory;
        } else {
            rootDir = new File(defaultDirectory);
        }
        if(doesFileExists(overideTemplateDir)){
            log.info(String.format("Using %s as template directory", overideTemplateDir));
            templateDir = overideTemplateDir;
        } else {
            templateDir = new File(rootDir.getAbsoluteFile() + "/template");
        }
        if(doesFileExists(overideExportDir)){
            log.info(String.format("Using %s as export directory", overideExportDir));
            exportDir = overideExportDir;
        } else {
            exportDir = new File(rootDir.getAbsoluteFile() + "/export");
        }
        if (!(doesFileExists(rootDir) && doesFileExists(templateDir) && doesFileExists(exportDir))){
            throw new IOException("Erorr resolving root, template, or export directory. Either default directories in the Finder root directory or arguments");
        }
    }

    private void runCbioportal(File ingestFile, String cmdCbioType) throws IOException {
        resolveCbioType(cmdCbioType);
        if (!(doesFileExists(ingestFile)))
        {
            throw new IOException("Ingest file directory does not exist");
        }
        cbioTransformer.exportCBP(exportDir,templateDir,ingestFile,resolvedCbioType);
    }

    private void resolveCbioType(String cmdCbioType){
        if (cmdCbioType.equals(cbioType.MUT.name())){
            resolvedCbioType = cbioType.MUT;
        }
        else if (cmdCbioType.equals(cbioType.GISTIC.name())){
            resolvedCbioType = cbioType.GISTIC;
        } else throw new IllegalArgumentException("Only MUT and GISTIC types are currently supported");
    }

    private boolean doesFileExists(File file) {
        return file != null && file.exists();
    }

    public void setDefaultDirectories(String defaultDirectory) {
        this.defaultDirectory = defaultDirectory;
         defaultTemplateDirectory = defaultDirectory + "/template";
         defaultExportDirectory = defaultDirectory + "/export";
    }
    public void setDefaultDirectory(String defaultDirectory){
        this.defaultDirectory = defaultDirectory;
    }

    public void setTemplateDir(File templateDir) { this.templateDir = templateDir; }

    public void setExportDir(File exportDir) { this.exportDir = exportDir; }
}

@@ -1136,22 +1136,23 @@
  resolve
+Root
 Dir
-Env
 (dataDir
@@ -1161,28 +1161,84 @@
 tory
-,overideTemplateDir,
+);%0A        resolveTemplateDir(overideTemplateDir);%0A        resolveExportDir(
 over
@@ -1248,24 +1248,25 @@
 ExportDir);%0A
+%0A
         if (
@@ -1409,14 +1409,15 @@
 olve
+Root
 Dir
-Env
 (Fil
@@ -1435,56 +1435,8 @@
 tory
-, File overideTemplateDir, File overideExportDir
 ) th
@@ -1451,16 +1451,17 @@
 xception
+ 
 %7B%0A      
@@ -1464,16 +1464,17 @@
       if
+ 
 (dataDir
@@ -1514,16 +1514,17 @@
 xists())
+ 
 %7B%0A      
@@ -1715,38 +1715,292 @@
     %7D%0A        if
-(doesF
+ (!(fileExists(rootDir))) %7B%0A            throw new IOException(String.format(%22Erorr resolving root data directory: %25s%22, rootDir.getAbsoluteFile()));%0A        %7D%0A    %7D%0A%0A    private void resolveTemplateDir(File overideTemplateDir) throws IOException %7B%0A        if (f
 ileExists(overid
@@ -2009,24 +2009,25 @@
 emplateDir))
+ 
 %7B%0A          
@@ -2267,22 +2267,277 @@
       if
-(doesF
+ (!(fileExists(templateDir)))%7B%0A            throw new IOException(String.format(%22Erorr resolving template directory %25s%22, templateDir.getAbsoluteFile()));%0A        %7D%0A    %7D%0A%0A    private void resolveExportDir(File overideExportDir) throws IOException %7B%0A%0A        if(f
 ileExist
@@ -2805,71 +2805,9 @@
  (!(
-doesFileExists(rootDir) && doesFileExists(templateDir) && doesF
+f
 ileE
@@ -2860,16 +2860,30 @@
 ception(
+String.format(
 %22Erorr r
@@ -2895,114 +2895,58 @@
 ing 
-root, template, or export directory. Either default directories in the Finder root directory or arguments%22
+export directory %25s%22, exportDir.getAbsoluteFile())
 );%0A 
@@ -3102,21 +3102,17 @@
   if (!(
-doesF
+f
 ileExist
@@ -3723,13 +3723,9 @@
 ean 
-doesF
+f
 ileE

split resolveDirEnv into multiple methods