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.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"));
    urlShortener.getRepo().save(entry2);

  }



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

    UrlShortenerEntry row1 = GridKt._get(grid, 0);
    assertEquals(entry1, row1);

    UrlShortenerEntry row2 = GridKt._get(grid, 1);
    assertEquals(entry2, row2);
  }


}

@@ -341,16 +341,66 @@
 Vaadin;%0A
+import com.vaadin.shared.data.sort.SortDirection;%0A
 import c
@@ -2439,24 +2439,74 @@
 %22/test2%22));%0A
+    entry2.setTemporaryUrl(URI.create(%22/temp2%22));%0A
     urlShort
@@ -2565,16 +2565,23 @@
 testShow
+AndSort
 Entries(
@@ -2705,32 +2705,69 @@
     
-UrlShortenerEntry row1 =
+assertEquals(2, GridKt._size(grid));%0A    assertEquals(entry1,
  Gri
@@ -2783,16 +2783,17 @@
 grid, 0)
+)
 ;%0A    as
@@ -2812,106 +2812,407 @@
 ntry
-1, row1);%0A%0A    UrlShortenerEntry row2 = GridKt._get(grid, 1);%0A    assertEquals(entry2, row2);%0A  %7D%0A
+2, GridKt._get(grid, 1));%0A%0A    // Sort by the %22Temporary URL%22 column%0A    grid.sort(grid.getColumns().get(3), SortDirection.ASCENDING);%0A    assertEquals(entry1, GridKt._get(grid, 0));%0A    assertEquals(entry2, GridKt._get(grid, 1));%0A%0A    grid.sort(grid.getColumns().get(3), SortDirection.DESCENDING);%0A    assertEquals(entry2, GridKt._get(grid, 0));%0A    assertEquals(entry1, GridKt._get(grid, 1));%0A%0A  %7D
 %0A%0A%7D%0A

Add test for sorting the entries