package org.pdxfinder.commands.postload;

import org.pdxfinder.reportmanager.ReportManager;
import org.pdxfinder.services.UtilityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/*
 * Created by csaba on 27/02/2019.
 */
@Component
@Order(value = 93)
public class PrintReport implements CommandLineRunner, ApplicationContextAware {


    private final static Logger log = LoggerFactory.getLogger(PrintReport.class);
    static ApplicationContext context;
    ReportManager reportManager;


    @Value("${pdxfinder.root.dir}")
    private String finderRootDir;

    private UtilityService utilityService;

    @Autowired
    public PrintReport(UtilityService utilityService) {
        this.utilityService = utilityService;
    }

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


        printReport();
        saveReportInCsv();
    }


    private void printReport(){

        reportManager = (ReportManager) context.getBean("ReportManager");
        reportManager.printMessages("ERROR");


    }


    private void saveReportInCsv(){
        String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
        String logFile = finderRootDir + "/logs/markerlog_"+timeStamp+".csv";
        List<String> headers = new ArrayList<>();
        headers.addAll(Arrays.asList("Type", "Reporter", "DataSource", "Model", "MolChar", "Platform", "MarkerInFile", "HarmonizedMarker", "ReasonOfChange", "Message"));
        utilityService.writeCsvFile(headers, reportManager.getMarkerHarmonizationMessagesInList(), logFile);

    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

}

@@ -625,16 +625,37 @@
 onent;%0A%0A
+import java.io.File;%0A
 import j
@@ -1831,16 +1831,110 @@
 %22.csv%22;%0A
+%0A        File logDir = new File(finderRootDir+ %22/logs%22);%0A%0A        if(logDir.canWrite())%7B%0A%0A    
         
@@ -1971,24 +1971,28 @@
 ayList%3C%3E();%0A
+    
         head
@@ -2157,16 +2157,20 @@
         
+    
 utilityS
@@ -2262,16 +2262,150 @@
 gFile);%0A
+        %7D%0A        else%7B%0A%0A            log.warn(%22Cannot save log file, need write permission to %22+finderRootDir + %22/logs%22);%0A        %7D%0A%0A%0A
 %0A    %7D%0A%0A

Check write permission for log dir