package annis.gui.flatquerybuilder;

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

import annis.SingletonBeanStoreRetrievalStrategy;
import annis.gui.AnnisUI;
import annis.gui.querybuilder.QueryBuilderChooser;
import com.github.mvysny.kaributesting.v8.MockVaadin;
import com.vaadin.spring.internal.UIScopeImpl;
import com.vaadin.ui.Button;
import com.vaadin.ui.MenuBar;
import com.vaadin.v7.ui.ComboBox;
import java.util.List;
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")
@WebAppConfiguration
class FlatQueryBuilderTest {


  @Autowired
  private BeanFactory beanFactory;

  private AnnisUI ui;

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

  }

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

  @Test
  void flatQueryBuilderLinguisticSequence() throws InterruptedException {
    ui.getQueryState().setSelectedCorpora(Sets.newSet("pcc2"));

    // Click on the query builder button and select the flat query builder
    _click(_get(Button.class, spec -> spec.withCaption("Query<br />Builder")));

    ComboBox queryBuilderChooser =
        _get(_get(QueryBuilderChooser.class), ComboBox.class);
    queryBuilderChooser.select("Word sequences and meta information");

    // Initialize the query builder
    FlatQueryBuilder queryBuilder = _get(FlatQueryBuilder.class);
    List<Button> initButtons =
        _find(queryBuilder, Button.class, spec -> spec.withCaption("Initialize"));
    assertEquals(3, initButtons.size());
    _click(initButtons.get(0));

    // The button caption should have been changed
    List<MenuBar> addButtons = _find(queryBuilder, MenuBar.class);
    assertEquals(3, addButtons.size());

    // Add a single token by clicking on the first "Add" menu entry
    queryBuilder.addLinguisticSequenceBox("tok");

    SearchBox searchBox = _get(queryBuilder, SearchBox.class);
    _get(searchBox, ComboBox.class).setValue("Feigenblatt");

    // Add a scope
    queryBuilder.addSpanBox("Sent");
    SpanBox spanBox = _get(queryBuilder, SpanBox.class);
    _get(spanBox, ComboBox.class).setValue("s");;

    // Create the AQL query
    _click(_get(queryBuilder, Button.class, spec -> spec.withCaption("Create AQL Query")));

    assertEquals("tok=/Feigenblatt/\n& Sent = \"s\"\n& #2_i_#1",
        ui.getQueryState().getAql().getValue());
  }
}

@@ -2497,62 +2497,68 @@
 d a 
-single token by clicking on the first %22Add%22 menu entry
+two tokens%0A    queryBuilder.addLinguisticSequenceBox(%22tok%22);
 %0A   
@@ -2609,16 +2609,21 @@
 );%0A%0A    
+List%3C
 SearchBo
@@ -2623,16 +2623,17 @@
 earchBox
+%3E
  searchB
@@ -2634,23 +2634,26 @@
 earchBox
+es
  = _
-get
+find
 (queryBu
@@ -2669,32 +2669,74 @@
 archBox.class);%0A
+    assertEquals(2, searchBoxes.size());%0A%0A
     _get(searchB
@@ -2737,16 +2737,25 @@
 earchBox
+es.get(0)
 , ComboB
@@ -2788,16 +2788,78 @@
 blatt%22);
+%0A    _get(searchBoxes.get(1), ComboBox.class).setValue(%22Die%22);
 %0A%0A    //
@@ -3172,16 +3172,28 @@
 enblatt/
+ & tok=/Die/
 %5Cn& Sent
@@ -3209,14 +3209,54 @@
 n& #
-2_i_#1
+1 . #2%5Cn%22%0A        + %22& #3_i_#1%5Cn%22 + %22& #3_i_#2
 %22,%0A 

Test flat query builder with two token (that need a relation)