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.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;

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"));
            }
        }
    }
}

@@ -157,16 +157,61 @@
 ownBug;%0A
+import com.autonomy.abc.framework.RelatedTo;%0A
 import c
@@ -760,16 +760,39 @@
 Is.is;%0A%0A
+@RelatedTo(%22CSA-2091%22)%0A
 public c
@@ -2725,16 +2725,17 @@
 cepts())
+ 
 %7B%0A      
@@ -3304,11 +3304,617 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    @RelatedTo(%22CCUK-3599%22)%0A    public void testRelatedConceptsHighlightButton() %7B%0A        findService.search(%22pancakes%22);%0A        WebElement button = results.highlightRelatedConceptsButton();%0A%0A        verifyThat(results.highlightedSausages(%22%22), empty());%0A        verifyThat(button, not(hasClass(%22active%22)));%0A%0A        button.click();%0A        verifyThat(results.highlightedSausages(%22%22), not(empty()));%0A        verifyThat(button, hasClass(%22active%22));%0A%0A        button.click();%0A        verifyThat(results.highlightedSausages(%22%22), empty());%0A        verifyThat(button, not(hasClass(%22active%22)));%0A    %7D
 %0A%7D%0A

Add test for highlight button behaviour, to cover CCUK-3599