package org.pharmgkb.pharmcat.reporter.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.pharmgkb.pharmcat.reporter.model.Annotation;
import org.pharmgkb.pharmcat.reporter.model.CPICException;
import org.pharmgkb.pharmcat.reporter.model.Group;
import org.pharmgkb.pharmcat.reporter.resultsJSON.GeneReport;
import org.pharmgkb.pharmcat.reporter.resultsJSON.Interaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ReporterWriter {
  private static final Logger sf_logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  private static final String sf_outputFileName = "reporter.output.md";

  public static void printResults(Path outputDir, List<Interaction> guidelineResultList, Map<String, GeneReport> symbolToGeneReportMap) throws IOException {
    Path reportPath = outputDir.resolve(sf_outputFileName);
    sf_logger.info("Writing report to {}", reportPath);

    try (BufferedWriter writer = Files.newBufferedWriter(reportPath)) {
      writer.write("# PharmCAT Report\n\n\n");

      writer.write("## Haplotype Calls\n\n");
      for (String gene : symbolToGeneReportMap.keySet()) {
        writer.write("### Gene: " + gene + "\n");
        GeneReport geneReport = symbolToGeneReportMap.get(gene);

        if (geneReport.getDips().size() == 1) {
          writer.write("call: `" + geneReport.getDips().iterator().next() + "`\n");
        } else {
          writer.write("__possible calls__:\n\n");
          for (String dip : geneReport.getDips()) {
            writer.write(" * `" + dip + "`\n");
          }
          writer.write("\n");
        }

        writer.write("__warnings__: ");
        if (geneReport.getExceptionList() == null || geneReport.getExceptionList().size() == 0) {
          writer.write("none applicable\n");
        } else {
          writer.write("\n\n");
          for (CPICException exception : geneReport.getExceptionList()) {
            writer.write(" * "+exception.getMessage()+"\n");
          }
        }
        writer.write("\n");
      }

      writer.write("\n\n");

      writer.write("## Guidelines\n\n");

      for (Interaction guideline : guidelineResultList) {
        writer.write("### Guideline: " + guideline.getName() + "\n");
        writer.write("[guideline on PharmGKB]("+guideline.getUrl()+")\n");

        if (guideline.getMatchingGroups() == null) {
          writer.write("_no matching annotations found_\n");
          continue;
        }

        for (Group group : guideline.getMatchingGroups()) {
          writer.write("_");
          writer.write(group.getName());
          writer.write("_ ");
          writer.write(guideline.getMatchedDiplotypes().get(group.getId()).stream()
              .map(d -> "`"+d+"`")
              .collect(Collectors.joining(", ")));
          writer.write("\n\n");

          writer.write("|Type|Annotation|\n");
          writer.write("|---|---|\n");
          for (Annotation ann : group.getAnnotations()) {
            writer.write("|");
            writer.write(ann.getType().getTerm());
            writer.write("|");
            writer.write(ann.getText().replaceAll("[\\n\\r]", " "));
            writer.write("|\n");
          }
          writer.write("\n");
        }
       writer.write("\n\n");
      }
    }
  }
}

@@ -2381,19 +2381,8 @@
 %22###
- Guideline:
  %22 +
@@ -2403,24 +2403,26 @@
 Name() + %22%5Cn
+%5Cn
 %22);%0A        
@@ -2435,17 +2435,51 @@
 .write(%22
-%5B
+For more information see the %5Bfull 
 guidelin
@@ -2516,16 +2516,19 @@
 Url()+%22)
+.%5Cn
 %5Cn%22);%0A%0A 
@@ -2683,27 +2683,12 @@
     
-for (Group group : 
+if (
 guid
@@ -2708,24 +2708,33 @@
 hingGroups()
+.size()%3E1
 ) %7B%0A        
@@ -2754,52 +2754,189 @@
 e(%22_
-%22);%0A          writer.write(group.getName());
+Note: More than one call was made for the applicable gene so multiple annotation groups could be shown_%5Cn%5Cn%22);%0A        %7D%0A%0A        for (Group group : guideline.getMatchingGroups()) %7B
 %0A   
@@ -2956,17 +2956,37 @@
 .write(%22
-_
+#### Annotations for:
  %22);%0A   
@@ -3611,32 +3611,55 @@
 writer.write(%22%5Cn
+---------------------%5Cn
 %5Cn%22);%0A      %7D%0A  

Improve ReportWriter output.