package annis.visualizers.component.tree;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import annis.gui.AnnisUI;
import annis.gui.UIConfig;
import annis.libgui.Helper;
import annis.libgui.VisualizationToggle;
import annis.libgui.visualizers.VisualizerInput;
import annis.service.objects.Match;
import com.github.romankh3.image.comparison.ImageComparison;
import com.github.romankh3.image.comparison.model.ImageComparisonResult;
import com.github.romankh3.image.comparison.model.ImageComparisonState;
import com.vaadin.server.VaadinSession;
import java.awt.FontFormatException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import org.corpus_tools.salt.SALT_TYPE;
import org.corpus_tools.salt.common.SDocument;
import org.corpus_tools.salt.common.SDocumentGraph;
import org.corpus_tools.salt.common.SaltProject;
import org.corpus_tools.salt.samples.SampleGenerator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

class TigerTreeVisualizerTest {

  private AnnisUI ui;
  private VisualizerInput visInput;
  private VisualizationToggle visToggle;
  private VaadinSession session;
  private ServletContext servletContext;
  private UIConfig config = new UIConfig();


  private TigerTreeVisualizer vis;

  @BeforeEach
  void setup() throws FontFormatException, IOException {

    vis = new TigerTreeVisualizer();

    // Init mocks
    visInput = mock(VisualizerInput.class);
    visToggle = mock(VisualizationToggle.class);
    ui = mock(AnnisUI.class);
    session = mock(VaadinSession.class);
    servletContext = mock(ServletContext.class);

    when(visInput.getUI()).thenReturn(ui);

    when(ui.getSession()).thenReturn(session);
    when(ui.getServletContext()).thenReturn(servletContext);
    when(ui.getConfig()).thenReturn(config);

  }

  @Test
  void compareSampleImage() throws IOException {

    // Use the example document from Salt
    SaltProject project = SampleGenerator.createSaltProject();
    SDocument doc = project.getCorpusGraphs().get(0).getDocuments().get(0);
    SDocumentGraph graph = doc.getDocumentGraph();

    // Mark the root node as match
    Match m = new Match(
        Arrays.asList(graph.getRootsByRelation(SALT_TYPE.SDOMINANCE_RELATION).get(0).getId()));
    Helper.addMatchToDocumentGraph(m, graph);

    when(visInput.getDocument()).thenReturn(doc);
    Map<String, String> mappings = new LinkedHashMap<>();
    mappings.put("node_ns", "syntax");
    mappings.put("node_key", "const");
    mappings.put("edge_type", "null");
    when(visInput.getMappings()).thenReturn(mappings);

    // Create the resource as byte array
    File tmpFile = File.createTempFile("annis-tree-test-", ".png");
    try (FileOutputStream outStream = new FileOutputStream(tmpFile)) {
      vis.writeOutput(visInput, outStream);
    }
    // Create an image so we can compare the image data instead of the binary png format
    BufferedImage actualImage = ImageIO.read(tmpFile);

    // Load the expected image as resource
    ClassPathResource expectedRes =
        new ClassPathResource("example_constituent_tree.png", this.getClass());
    BufferedImage expectedImage = ImageIO.read(expectedRes.getInputStream());

    // Compare image appearance with default thresholds
    ImageComparison imageComparison = new ImageComparison(expectedImage, actualImage);
    // Anti-aliasing difference can lead to different pixels, allow some variation
    imageComparison.setAllowingPercentOfDifferentPixels(0.5);
    ImageComparisonResult comparisonResult = imageComparison.compareImages();
    assertEquals(ImageComparisonState.MATCH, comparisonResult.getImageComparisonState(), String
        .format("%f percent image difference detected", comparisonResult.getDifferencePercent()));
  }

}

@@ -973,16 +973,58 @@
 T_TYPE;%0A
+import org.corpus_tools.salt.SaltFactory;%0A
 import o
@@ -1129,98 +1129,35 @@
 org.
-corpus_tools.salt.common.SaltProject;%0Aimport org.corpus_tools.salt.samples.SampleGenerator
+eclipse.emf.common.util.URI
 ;%0Aim
@@ -2187,11 +2187,12 @@
  // 
-Use
+Load
  the
@@ -2204,163 +2204,292 @@
 ple 
-document from Salt%0A    SaltProject project = SampleGenerator.createSaltProject();%0A    SDocument doc = project.getCorpusGraphs().get(0).getDocuments().get(0
+tree%0A    ClassPathResource docFile = new ClassPathResource(%0A        %22example_constituent_tree/rootCorpus/subCorpus1/doc1.salt%22, TigerTreeVisualizerTest.class);%0A    SDocument doc = SaltFactory.createSDocument();%0A    doc.loadDocumentGraph(URI.createURI(docFile.getURI().toASCIIString())
 );%0A 
@@ -2963,15 +2963,99 @@
 %22, %22
-null%22);
+edge%22);%0A    mappings.put(%22secedge_type%22, %22secedge%22);%0A    mappings.put(%22edge_key%22, %22func%22);%0A
 %0A   

Load test tree from Salt document file