package com.autonomy.abc.find;

import com.autonomy.abc.config.FindTestBase;
import com.autonomy.abc.config.TestConfig;
import com.autonomy.abc.framework.KnownBug;
import com.autonomy.abc.framework.RelatedTo;
import com.autonomy.abc.selenium.error.Errors;
import com.autonomy.abc.selenium.find.FindResultsPage;
import com.autonomy.abc.selenium.find.FindService;
import com.autonomy.abc.selenium.find.FindTopNavBar;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;

import static com.autonomy.abc.framework.ABCAssert.assertThat;
import static com.autonomy.abc.framework.ABCAssert.verifyThat;
import static com.autonomy.abc.matchers.ElementMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;

@RelatedTo("CSA-2091")
public class RelatedConceptsITCase extends FindTestBase {
    private FindService findService;
    private FindResultsPage results;

    public RelatedConceptsITCase(TestConfig config) {
        super(config);
    }

    @Before
    public void setUp() {
        findService = getApplication().findService();
        results = getElementFactory().getResultsPage();
    }

    //TODO ALL RELATED CONCEPTS TESTS - probably better to check if text is not("Loading...") rather than not("")
    @Test
    public void testRelatedConceptsHasResults(){
        findService.search("Danye West");
        for (WebElement concept : results.relatedConcepts()) {
            assertThat(concept, hasTextThat(not(isEmptyOrNullString())));
        }
    }

    @Test
    public void testRelatedConceptsNavigateOnClick(){
        String search = "Red";
        findService.search(search);
        WebElement topRelatedConcept = results.relatedConcepts().get(0);
        String concept = topRelatedConcept.getText();

        topRelatedConcept.click();
        FindTopNavBar navBar = getElementFactory().getTopNavBar();
        assertThat(navBar.getAlsoSearchingForTerms(), hasItem(concept));
        assertThat(navBar.getSearchBoxTerm(), is(search));
    }

    @Test
    @KnownBug({"CCUK-3498", "CSA-2066"})
    public void testRelatedConceptsHover(){
        findService.search("Find");
        WebElement popover = results.hoverOverRelatedConcept(0);
        verifyThat(popover, hasTextThat(not(isEmptyOrNullString())));
        verifyThat(popover.getText(), not(containsString("QueryText-Placeholder")));
        verifyThat(popover.getText(), not(containsString(Errors.Search.RELATED_CONCEPTS)));
        results.unhover();
    }

    @Test
    public void testRelatedConceptsInResults(){
        findService.search("Tiger");
        results.highlightRelatedConceptsButton().click();

        for(WebElement relatedConceptLink : results.relatedConcepts()) {
            String relatedConcept = relatedConceptLink.getText();
            for (WebElement relatedConceptElement : results.highlightedSausages(relatedConcept)) {
                if (relatedConceptElement.isDisplayed()) {        //They can become hidden if they're too far in the summary
                    verifyThat(relatedConceptElement, containsTextIgnoringCase(relatedConcept));
                }
                verifyThat(relatedConceptElement, hasTagName("a"));
                verifyThat(relatedConceptElement, hasClass("clickable"));
            }
        }
    }

    @Test
    @RelatedTo("CCUK-3599")
    public void testRelatedConceptsHighlightButton() {
        findService.search("pancakes");
        WebElement button = results.highlightRelatedConceptsButton();

        verifyThat(results.highlightedSausages(""), empty());
        verifyThat(button, not(hasClass("active")));

        button.click();
        verifyThat(results.highlightedSausages(""), not(empty()));
        verifyThat(button, hasClass("active"));

        button.click();
        verifyThat(results.highlightedSausages(""), empty());
        verifyThat(button, not(hasClass("active")));
    }
}

@@ -483,25 +483,128 @@
 elenium.
-WebElemen
+NoSuchElementException;%0Aimport org.openqa.selenium.WebElement;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Lis
 t;%0A%0Aimpo
@@ -721,24 +721,94 @@
 verifyThat;%0A
+import static com.autonomy.abc.matchers.CommonMatchers.containsItems;%0A
 import stati
@@ -1088,16 +1088,51 @@
 esults;%0A
+    private FindTopNavBar navBar;%0A%0A
 %0A    pub
@@ -1352,24 +1352,77 @@
 ultsPage();%0A
+        navBar = getElementFactory().getTopNavBar();%0A
     %7D%0A%0A    /
@@ -2083,75 +2083,8 @@
 ();%0A
-        FindTopNavBar navBar = getElementFactory().getTopNavBar();%0A
     
@@ -4038,32 +4038,32 @@
 (%22%22), empty());%0A
-
         verifyTh
@@ -4104,11 +4104,1111 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testMultipleAdditionalConcepts() %7B%0A        findService.search(%22bongo%22);%0A%0A        List%3CString%3E relatedConcepts = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C 5; i++) %7B%0A            String newConcept = clickFirstNewConcept(relatedConcepts);%0A            verifyThat(navBar.getAlsoSearchingForTerms(), hasItem(newConcept));%0A        %7D%0A        verifyThat(navBar.getSearchBoxTerm(), is(%22bongo%22));%0A        verifyThat(navBar.getAlsoSearchingForTerms(), hasSize(relatedConcepts.size()));%0A        verifyThat(navBar.getAlsoSearchingForTerms(), containsItems(relatedConcepts));%0A    %7D%0A%0A    private String clickFirstNewConcept(List%3CString%3E existingConcepts) %7B%0A        for (WebElement concept : results.relatedConcepts()) %7B%0A            String conceptText = concept.getText();%0A            if (!existingConcepts.contains(conceptText)) %7B%0A                concept.click();%0A                existingConcepts.add(conceptText);%0A                results.unhover();%0A                return conceptText;%0A            %7D%0A        %7D%0A        throw new NoSuchElementException(%22no new related concepts%22);%0A    %7D
 %0A%7D%0A

Add test for multiple additional related concepts