package com.autonomy.abc.find;

import com.autonomy.abc.base.IdolFindTestBase;
import com.autonomy.abc.selenium.find.bi.SunburstView;
import com.autonomy.abc.selenium.find.FindService;
import com.autonomy.abc.selenium.find.IdolFindPage;
import com.autonomy.abc.selenium.find.filters.FilterPanel;
import com.hp.autonomy.frontend.selenium.config.TestConfig;
import com.hp.autonomy.frontend.selenium.util.Waits;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;

import java.util.List;

import static com.hp.autonomy.frontend.selenium.framework.state.TestStateAssert.assertThat;
import static com.hp.autonomy.frontend.selenium.framework.state.TestStateAssert.assumeThat;
import static com.hp.autonomy.frontend.selenium.framework.state.TestStateAssert.verifyThat;
import static com.hp.autonomy.frontend.selenium.matchers.ElementMatchers.checked;
import static org.hamcrest.Matchers.*;

public class SunburstITCase extends IdolFindTestBase {
    private IdolFindPage findPage;
    private SunburstView results;
    private FindService findService;

    public SunburstITCase(final TestConfig config){super(config);}

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

    //TODO: test that checks the total doc number against what's in sunburst centre

    @Test
    public void testSunburstTabShowsSunburst(){
        findService.search("shambolic");
        results.goToSunburst();

        verifyThat("Main results list hidden",getElementFactory().getResultsPage().mainResultsContainerHidden());
        verifyThat("Sunburst element displayed",results.sunburstVisible());
        verifyThat("Parametric selectors appear",results.parametricSelectionDropdownsExist());
    }

    @Test
    public void testParametricSelectors(){
        findService.search("wild horses");
        results.goToSunburst();

        final String firstParametric = filters().parametricField(0).getParentName();
        verifyThat("Default parametric selection is 1st parametric type",firstParametric,equalToIgnoringCase(results.getSelectedFieldName(1)));

        results.parametricSelectionDropdown(2).open();
        verifyThat("1st selected parametric does not appear as choice in 2nd",results.getParametricDropdownItems(2),not(contains(firstParametric)));
    }

    @Test
    public void testParametricSelectorsChangeDisplay(){
        findService.search("cricket");
        results.goToSunburst();

        //only works if you have at least 2 parametric types
        results.parametricSelectionDropdown(1).selectItem(1);
        Waits.loadOrFadeWait();

        filters().showFilters();
        final int correctNumberSegments = SunburstView.expectedParametricValues(filters().parametricField(1)).size();
        assertThat("Correct number ("+correctNumberSegments+") of sunburst segments ",results.numberOfSunburstSegments(),is(correctNumberSegments));
    }

    @Test
    public void testHoveringOverSegmentCausesTextToChange(){
        findService.search("elephant");
        results.goToSunburst();

        filters().showFilters();
        final List<String> bigEnough = SunburstView.expectedParametricValues(filters().parametricField(0));
        results.waitForSunburst();

        for (final WebElement segment : results.findSunburstSegments()) {
            results.segmentHover(segment);
            final String name = results.getSunburstCentreName();
            verifyThat(name, not(isEmptyOrNullString()));
            verifyThat(name, isIn(bigEnough));
        }
    }

    @Test
    public void testHoveringOnGreySegmentGivesMessage(){
        findService.search("elephant");
        results.goToSunburst();

        assumeThat("Some segments not displayable",results.greySunburstAreaExists());
        results.hoverOverTooFewToDisplaySegment();

        verifyThat("Hovering on greyed segment explains why grey",results.getSunburstCentreName(),allOf(containsString("Please refine your search"),containsString("too small to display")));
    }

    @Test
    public void testClickingSunburstSegmentFiltersTheSearch(){
        //needs to search something that only has 2 parametric filter types
        findService.search("churchill");
        results.goToSunburst();


        final String fieldValue = results.hoverOnSegmentGetCentre(1);
        final String fieldName = results.getSelectedFieldName(1);
        LOGGER.info("filtering by " + fieldName + " = " + fieldValue);
        results.getIthSunburstSegment(1).click();

        verifyThat(findPage.getFilterLabels(), hasItem(equalToIgnoringCase(fieldValue)));
        verifyThat(filters().checkboxForParametricValue(fieldName, fieldValue), checked());
        verifyThat("Parametric selection name has changed to another type of filter",results.getSelectedFieldName(1),not(fieldName));

        results.getIthSunburstSegment(1);
        //TODO: wait til desired behaviour known for when runs out of parametric filter types
        //verifyThat("When run out of parametric types to filter by ",,);

    }

    @Test
    public void testSideBarFiltersChangeSunburst(){
        findService.search("lashing");
        results.goToSunburst();

        final String parametricSelectionFirst= results.getSelectedFieldName(1);
        filters().checkboxForParametricValue(0, 0).check();

        results.waitForSunburst();
        assertThat("Parametric selection changed",results.getSelectedFieldName(1),not(is(parametricSelectionFirst)));
    }

    //will probably fail if your databases are different to the testing ones
    @Test
    public void testTwoParametricSelectorSunburst(){
        findService.search("cameron");
        results.goToSunburst();

        results.parametricSelectionDropdown(1).select("SOURCE");
        results.waitForSunburst();
        final int segNumberBefore = results.numberOfSunburstSegments();

        results.parametricSelectionDropdown(2).select("CATEGORY");
        results.waitForSunburst();
        final int segNumberAfter = results.numberOfSunburstSegments();

        assertThat("More segments with second parametric selector",segNumberAfter,greaterThan(segNumberBefore));
    }

    private FilterPanel filters() {
        return getElementFactory().getFilterPanel();
    }
}

@@ -2143,16 +2143,17 @@
 c type%22,
+ 
 firstPar
@@ -2160,35 +2160,27 @@
 ametric,
-equalToIgnoringCase
+ startsWith
 (results

Fix failing test