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

import com.vaadin.data.Binder;
import com.vaadin.data.Binder.Binding;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.components.grid.HeaderRow;
import java.net.URI;
import java.util.Date;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.corpus_tools.annis.gui.AnnisUI;
import org.corpus_tools.annis.gui.query_references.UrlShortener;
import org.corpus_tools.annis.gui.query_references.UrlShortenerEntry;

public class ReferenceLinkEditor extends Panel {

  private static final long serialVersionUID = 6191359393713574090L;
  private final Grid<UrlShortenerEntry> grid;
  private final TextField txtFilterId;

  public ReferenceLinkEditor() {
    grid = new Grid<>();
    grid.setSizeFull();
    
    Binder<UrlShortenerEntry> binder = grid.getEditor().getBinder();

    Column<UrlShortenerEntry, UUID> idColumn = grid.addColumn(UrlShortenerEntry::getId);
    idColumn.setCaption("UUID");
    TextField txtUUID = new TextField();
    Binding<UrlShortenerEntry, String> idBinding =
        binder.bind(txtUUID, entry -> entry.getId().toString()
            , (entry, value) -> {
              entry.setId(UUID.fromString(value));
    });
    idColumn.setEditorBinding(idBinding);
    Column<UrlShortenerEntry, Date> createdColumn = grid.addColumn(UrlShortenerEntry::getCreated);
    createdColumn.setCaption("Timestamp");
    Column<UrlShortenerEntry, String> ownerColumn = grid.addColumn(UrlShortenerEntry::getOwner);
    ownerColumn.setCaption("Created by");
    Column<UrlShortenerEntry, URI> temporaryColumn =
        grid.addColumn(UrlShortenerEntry::getTemporaryUrl);
    temporaryColumn.setCaption("Temporary URL");
    Column<UrlShortenerEntry, URI> urlColumn = grid.addColumn(UrlShortenerEntry::getUrl);
    urlColumn.setCaption("URL");
    
    HeaderRow filterRow = grid.appendHeaderRow();
    
    txtFilterId = new TextField();
    txtFilterId.setPlaceholder("Filter by UUID");
    txtFilterId.setWidthFull();
    txtFilterId.addValueChangeListener((e) -> {
      refreshItems();
    });

    filterRow.getCell(idColumn).setComponent(txtFilterId);

  }
  
  private void refreshItems() {
    if (getUI() instanceof AnnisUI) {
      AnnisUI annisUI = (AnnisUI) getUI();
      final UrlShortener shortener = annisUI.getUrlShortener();
      Stream<UrlShortenerEntry> items = shortener.list().stream();
      if(!txtFilterId.getValue().isEmpty()) {
        items = items.filter((item) -> item.getId().toString().contains(txtFilterId.getValue()));
      }
      grid.setItems(items.collect(Collectors.toList()));

    }
  }

  @Override
  public void attach() {
    super.attach();

    
    refreshItems();
    setContent(grid);
  }

}

@@ -305,16 +305,52 @@
 et.URI;%0A
+import java.net.URISyntaxException;%0A
 import j
@@ -364,16 +364,16 @@
 l.Date;%0A
-
 import j
@@ -498,16 +498,78 @@
 nnisUI;%0A
+import org.corpus_tools.annis.gui.components.ExceptionDialog;%0A
 import o
@@ -1184,298 +1184,9 @@
 %22);%0A
-    TextField txtUUID = new TextField();%0A    Binding%3CUrlShortenerEntry, String%3E idBinding =%0A        binder.bind(txtUUID, entry -%3E entry.getId().toString()%0A            , (entry, value) -%3E %7B%0A              entry.setId(UUID.fromString(value));%0A    %7D);%0A    idColumn.setEditorBinding(idBinding);
 %0A
+
     
@@ -1319,24 +1319,25 @@
 imestamp%22);%0A
+%0A
     Column%3CU
@@ -1459,24 +1459,25 @@
 eated by%22);%0A
+%0A
     Column%3CU
@@ -1622,24 +1622,735 @@
 rary URL%22);%0A
+    TextField txtTemporary = new TextField();%0A    Binding%3CUrlShortenerEntry, String%3E temporaryBinding =%0A        binder.bind(txtTemporary, entry -%3E %7B%0A          if (entry.getTemporaryUrl() == null) %7B%0A            return %22%22;%0A          %7D else %7B%0A            return entry.getTemporaryUrl().toString();%0A          %7D%0A        %7D, (entry, value) -%3E %7B%0A          if (value == null %7C%7C value.isEmpty()) %7B%0A            entry.setTemporaryUrl(null);%0A          %7D else %7B%0A            try %7B%0A              entry.setTemporaryUrl(new URI(value));%0A            %7D catch (URISyntaxException ex) %7B%0A              ExceptionDialog.show(ex, getUI());%0A            %7D%0A          %7D%0A%0A        %7D);%0A    temporaryColumn.setEditorBinding(temporaryBinding);%0A%0A%0A
     Column%3CU
@@ -2712,24 +2712,24 @@
 );%0A    %7D);%0A%0A
-
     filterRo
@@ -2776,16 +2776,96 @@
 erId);%0A%0A
+    grid.getEditor().setEnabled(true);%0A    grid.getEditor().setBuffered(true);%0A%0A
   %7D%0A  %0A 

Allow to change the temporary URL