package org.corpus_tools.annis.gui.exporter;

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

import com.github.mvysny.kaributesting.v8.MockVaadin;
import com.google.common.eventbus.EventBus;
import com.vaadin.spring.internal.UIScopeImpl;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import org.corpus_tools.annis.api.model.QueryLanguage;
import org.corpus_tools.annis.gui.AnnisUI;
import org.corpus_tools.annis.gui.SingletonBeanStoreRetrievalStrategy;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.internal.util.collections.Sets;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.web.WebAppConfiguration;

@SpringBootTest
@ActiveProfiles({"desktop", "test", "headless"})
@WebAppConfiguration
class CSVExporterTest {

  @Autowired
  private BeanFactory beanFactory;

  @Autowired
  private CSVExporter exporter;
  private AnnisUI ui;


  @BeforeEach
  void setup() {
    UIScopeImpl.setBeanStoreRetrievalStrategy(new SingletonBeanStoreRetrievalStrategy());
    this.ui = beanFactory.getBean(AnnisUI.class);
    MockVaadin.setup(() -> ui);
  }

  @AfterEach
  public void tearDown() {
    MockVaadin.tearDown();
  }

  @Test
  void exportFeigenblattQuery() throws IOException {
    EventBus eventBus = mock(EventBus.class);
    Writer out = new StringWriter();

    Exception ex = exporter.convertText("tok=\"Feigenblatt\"", QueryLanguage.AQL, 5, 5,
        Sets.newSet("pcc2"), null, "", false, out, eventBus, new HashMap<>(), ui);

    assertNull(ex);

    // Compare the generated CSV file with the ground truth
    out.close();
    String[] lines = out.toString().split("\n");
    assertEquals(3, lines.length);
    assertEquals("1_id\t1_span\t1_anno_tiger::lemma\t1_anno_tiger::morph\t1_anno_tiger::pos",
        lines[0]);
    assertEquals("salt:/pcc2/11299#tok_1\tFeigenblatt\tFeigenblatt\tNom.Sg.Neut\tNN", lines[1]);
    assertEquals("salt:/pcc2/11299#tok_143\tFeigenblatt\tFeigenblatt\tDat.Sg.Neut\tNN", lines[2]);
  }

  @Test
  void exportWithSegmentation() throws IOException {
    EventBus eventBus = mock(EventBus.class);
    Writer out = new StringWriter();


    Exception ex = exporter.convertText("utterance0=\"äh fang einfach ma an\"", QueryLanguage.AQL, 0, 0,
        Sets.newSet("dialog.demo"), null, "segmentation=phon0", false, out, eventBus,
        new HashMap<>(), ui);

    assertNull(ex);

    // Compare the generated CSV file with the ground truth
    out.close();
    String[] lines = out.toString().split("\n");
    assertEquals(2, lines.length);
    assertEquals("1_id\t1_span\t1_anno_default_ns::utterance0",
        lines[0]);
    assertEquals(
        "salt:/dialog.demo/dialog.demo#sSpan98\täh ((lacht)) fang einfach ma an\täh fang einfach ma an",
        lines[1]);
  }

}

@@ -2401,32 +2401,906 @@
 lines%5B2%5D);%0A  %7D%0A%0A
+  @Test%0A  void exportMetadata() throws IOException %7B%0A    EventBus eventBus = mock(EventBus.class);%0A    Writer out = new StringWriter();%0A%0A    Exception ex = exporter.convertText(%22tok=%5C%22Feigenblatt%5C%22%22, QueryLanguage.AQL, 5, 5,%0A        Sets.newSet(%22pcc2%22), null, %22metakeys=Genre%22, false, out, eventBus, new HashMap%3C%3E(), ui);%0A%0A    assertNull(ex);%0A%0A    // Compare the generated CSV file with the ground truth%0A    out.close();%0A    String%5B%5D lines = out.toString().split(%22%5Cn%22);%0A    assertEquals(3, lines.length);%0A    assertEquals(%0A        %221_id%5Ct1_span%5Ct1_anno_tiger::lemma%5Ct1_anno_tiger::morph%5Ct1_anno_tiger::pos%5Ctmeta_Genre%22,%0A        lines%5B0%5D);%0A    assertEquals(%22salt:/pcc2/11299#tok_1%5CtFeigenblatt%5CtFeigenblatt%5CtNom.Sg.Neut%5CtNN%5CtPolitik%22,%0A        lines%5B1%5D);%0A    assertEquals(%22salt:/pcc2/11299#tok_143%5CtFeigenblatt%5CtFeigenblatt%5CtDat.Sg.Neut%5CtNN%5CtPolitik%22,%0A        lines%5B2%5D);%0A  %7D%0A%0A
   @Test%0A  void e

Add additional test for metadata