package org.pdxfinder.mapping;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.pdxfinder.services.MappingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
 * Created by csaba on 24/04/2019.
 */
@Component
public class ExportMappings implements CommandLineRunner {


    @Autowired
    MappingService mappingService;
    Logger logger = LoggerFactory.getLogger(ExportMappings.class);



    @Override
    public void run(String... args) throws Exception {

        OptionParser parser = new OptionParser();
        parser.allowsUnrecognizedOptions();
        parser.accepts("exportMappings", "Exporting mapping rules to a file");
        OptionSet options = parser.parse(args);

        if (options.has("exportMappings")) {

            exportMappings();

        }
    }




    private void exportMappings(){


        String diagnosisFileName = "/Users/csaba/Downloads/diagnosis_mappings.json";
        String treatmentFileName = "/Users/csaba/Downloads/treatment_mappings.json";

        List<String> dataSourcesToExport = new ArrayList<>(Arrays.asList("TRACE", "IRCC-CRC", "IRCC-GC", "Curie-LC", "Curie-BC"));

        mappingService.saveMappingsToFile(diagnosisFileName, mappingService.getMappingsByDSAndType(dataSourcesToExport, "diagnosis").getEntityList());

        mappingService.saveMappingsToFile(treatmentFileName, mappingService.getMappingsByDSAndType(dataSourcesToExport, "treatment").getEntityList());

    }

}

@@ -252,16 +252,75 @@
 owired;%0A
+import org.springframework.beans.factory.annotation.Value;%0A
 import o
@@ -602,17 +602,16 @@
 nner %7B%0A%0A
-%0A
     @Aut
@@ -720,16 +720,85 @@
 lass);%0A%0A
+    @Value(%22$%7Bpdxfinder.root.dir%7D%22)%0A    private String finderRootDir;
 %0A%0A    @O
@@ -977,32 +977,39 @@
 .accepts(%22export
+EuroPdx
 Mappings%22, %22Expo
@@ -1117,24 +1117,31 @@
 .has(%22export
+EuroPdx
 Mappings%22)) 
@@ -1142,17 +1142,16 @@
 gs%22)) %7B%0A
-%0A
         
@@ -1156,24 +1156,31 @@
       export
+EuroPdx
 Mappings();%0A
@@ -1179,17 +1179,16 @@
 ings();%0A
-%0A
         
@@ -1196,19 +1196,16 @@
 %0A    %7D%0A%0A
-%0A%0A%0A
     priv
@@ -1215,24 +1215,31 @@
  void export
+EuroPdx
 Mappings()%7B%0A
@@ -1239,17 +1239,16 @@
 ngs()%7B%0A%0A
-%0A
         
@@ -1275,38 +1275,32 @@
 Name = %22
-/Users/csaba/Downloads
+%25s/mappings_out/
 /diagnos
@@ -1316,16 +1316,38 @@
 gs.json%22
+.format(finderRootDir)
 ;%0A      
@@ -1380,30 +1380,23 @@
  = %22
-/Users/csaba/Downloads
+%25s/mappings_out
 /tre
@@ -1416,18 +1416,39 @@
 gs.json%22
+.format(finderRootDir)
 ;
-%0A
 %0A       
@@ -1517,62 +1517,247 @@
 ist(
-%22TRACE%22, %22IRCC-CRC%22, %22IRCC-GC%22, %22Curie-LC%22, %22Curie-BC%22
+%0A            %22Curie-BC%22,%0A            %22Curie-LC%22,%0A            %22Curie-OC%22,%0A            %22IRCC-CRC%22,%0A            %22IRCC-GC%22,%0A            %22TRACE%22,%0A            %22UOC-BC%22,%0A            %22UOM-BC%22,%0A            %22VHIO-BC%22,%0A            %22VHIO-CRC%22%0A        
 ));%0A
@@ -1799,16 +1799,29 @@
 sToFile(
+%0A            
 diagnosi
@@ -1822,32 +1822,44 @@
 agnosisFileName,
+%0A           
  mappingService.
@@ -1926,28 +1926,36 @@
 EntityList()
+%0A        
 );%0A
-%0A
         mapp
@@ -1984,16 +1984,29 @@
 sToFile(
+%0A            
 treatmen
@@ -2015,16 +2015,28 @@
 ileName,
+%0A           
  mapping
@@ -2115,16 +2115,25 @@
 tyList()
+%0A        
 );%0A%0A    

Format export mappings for EuroPDX