package org.corpus_tools.annis.gui.admin.reflinks;

import static com.github.mvysny.kaributesting.v8.LocatorJ._click;
import static com.github.mvysny.kaributesting.v8.LocatorJ._get;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.github.mvysny.kaributesting.v8.GridKt;
import com.github.mvysny.kaributesting.v8.MockVaadin;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.spring.internal.UIScopeImpl;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.TabSheet;
import java.io.IOException;
import java.net.URI;
import java.util.UUID;
import net.jcip.annotations.NotThreadSafe;
import org.corpus_tools.annis.gui.AnnisUI;
import org.corpus_tools.annis.gui.SingletonBeanStoreRetrievalStrategy;
import org.corpus_tools.annis.gui.query_references.UrlShortener;
import org.corpus_tools.annis.gui.query_references.UrlShortenerEntry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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
@NotThreadSafe
class ReferenceLinkEditorTest {

  @Autowired
  private BeanFactory beanFactory;

  private AnnisUI ui;

  private ReferenceLinkEditor panel;

  private UrlShortenerEntry entry2;

  private UrlShortenerEntry entry1;


  @BeforeEach
  void setup() throws IOException {
    UIScopeImpl.setBeanStoreRetrievalStrategy(new SingletonBeanStoreRetrievalStrategy());
    this.ui = beanFactory.getBean(AnnisUI.class);

    MockVaadin.setup(() -> ui);

    _click(_get(Button.class, spec -> spec.withCaption("Administration")));
    TabSheet tab = _get(TabSheet.class);
    panel = _get(ReferenceLinkEditor.class);
    tab.setSelectedTab(panel);

    // Add some example entries
    UrlShortener urlShortener = this.ui.getUrlShortener();
    entry1 = new UrlShortenerEntry();
    entry1.setId(UUID.fromString("4366b0a5-6b27-40fe-ac5d-08e75c9eef51"));
    entry1.setUrl(URI.create("/test1"));
    urlShortener.getRepo().save(entry1);

    entry2 = new UrlShortenerEntry();
    entry2.setId(UUID.fromString("b1912b10-93f3-4018-84e8-6bf7572ee163"));
    entry2.setUrl(URI.create("/test2"));
    entry2.setTemporaryUrl(URI.create("/temp2"));
    urlShortener.getRepo().save(entry2);

  }



  @Test
  void testShowAndSortEntries() throws Exception {
    @SuppressWarnings("unchecked")
    Grid<UrlShortenerEntry> grid = _get(panel, Grid.class);

    assertEquals(2, GridKt._size(grid));
    assertEquals(entry1, GridKt._get(grid, 0));
    assertEquals(entry2, GridKt._get(grid, 1));

    // Sort by the "Temporary URL" column
    grid.sort(grid.getColumns().get(3), SortDirection.ASCENDING);
    assertEquals(entry1, GridKt._get(grid, 0));
    assertEquals(entry2, GridKt._get(grid, 1));

    grid.sort(grid.getColumns().get(3), SortDirection.DESCENDING);
    assertEquals(entry2, GridKt._get(grid, 0));
    assertEquals(entry1, GridKt._get(grid, 1));

  }

}

@@ -171,24 +171,93 @@
 atorJ._get;%0A
+import static com.github.mvysny.kaributesting.v8.LocatorJ._setValue;%0A
 import stati
@@ -590,24 +590,56 @@
 i.TabSheet;%0A
+import com.vaadin.ui.TextField;%0A
 import java.
@@ -3304,16 +3304,676 @@
 d, 1));%0A
-%0A
   %7D%0A
+%0A  @Test%0A  void testFilterByUUID() throws Exception %7B%0A    @SuppressWarnings(%22unchecked%22)%0A    Grid%3CUrlShortenerEntry%3E grid = _get(panel, Grid.class);%0A%0A    TextField filter = _get(grid, TextField.class);%0A%0A    // Set an existing UUID%0A    _setValue(filter, %224366b0a5-6b27-40fe-ac5d-08e75c9eef51%22);%0A    assertEquals(1, GridKt._size(grid));%0A%0A    // Set an invalid UUID, this should not apply the filter%0A    _setValue(filter, %224366b0a5-%22);%0A    assertEquals(2, GridKt._size(grid));%0A    %0A    // Set to a non-existing but valid UUID, this should hide all entries%0A    _setValue(filter, %228307fbb6-f426-433d-9b11-71244b970e0d%22);%0A    assertEquals(0, GridKt._size(grid));%0A%0A  %7D
 %0A%7D%0A

Add test for filtering by UUID