package com.autonomy.abc.search;

import com.autonomy.abc.config.HostedTestBase;
import com.autonomy.abc.config.TestConfig;
import com.autonomy.abc.framework.ABCAssert;
import com.autonomy.abc.selenium.config.ApplicationType;
import com.autonomy.abc.selenium.element.Checkbox;
import com.autonomy.abc.selenium.menu.TopNavBar;
import com.autonomy.abc.selenium.page.search.SearchPage;
import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runners.Parameterized;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Platform;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.Collections;

import static com.thoughtworks.selenium.SeleneseTestBase.assertNotEquals;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

public class SearchPageHostedITCase extends HostedTestBase {
	public SearchPageHostedITCase(final TestConfig config, final String browser, final ApplicationType appType, final Platform platform) {
		super(config, browser, appType, platform);
	}

	private SearchPage searchPage;
	private TopNavBar topNavBar;

	@Parameterized.Parameters
	public static Iterable<Object[]> parameters() throws IOException {
		final Collection<ApplicationType> applicationTypes = Collections.singletonList(ApplicationType.HOSTED);
		return parameters(applicationTypes);
	}

	@Before
	public void setUp() throws MalformedURLException {
		topNavBar = body.getTopNavBar();
		topNavBar.search("example");
		searchPage = getElementFactory().getSearchPage();
	}


	@Test
	public void testIndexSelection() {
		topNavBar.search("car");
		searchPage.selectLanguage("English");
		searchPage.selectAllIndexesOrDatabases(getConfig().getType().getName());
		//TODO add a matcher
		assertThat("All databases not showing", searchPage.allIndexesCheckbox().isChecked(), is(true));

		for(Checkbox checkbox : searchPage.indexList()){
			assertThat(checkbox.isChecked(),is(true));
		}

		searchPage.allIndexesCheckbox().toggle();

		searchPage.selectIndex("news_eng");
		assertThat("Database not showing", searchPage.indexCheckbox("news_eng").isChecked(), is(true));
		final String wikiEnglishResult = searchPage.getSearchResult(1).getText();
		searchPage.deselectIndex("news_eng");

		searchPage.selectIndex("news_ger");
		assertThat("Database not showing", searchPage.indexCheckbox("news_ger").isChecked(), is(true));
		final String wookiepediaResult = searchPage.getSearchResult(1).getText();
		assertNotEquals(wookiepediaResult, wikiEnglishResult);

		searchPage.selectIndex("wiki_chi");
		assertThat(searchPage.indexCheckbox("news_ger").isChecked(), is(true));
		assertThat(searchPage.indexCheckbox("wiki_chi").isChecked(),is(true));
		assertThat("Result not from selected databases", searchPage.getSearchResult(1).getText(), anyOf(is(wookiepediaResult), is(wikiEnglishResult)));
	}

	@Test
	public void testParametricSearch() {
		searchPage.selectAllIndexesOrDatabases(getConfig().getType().getName());
		topNavBar.search("*");

	}


	@Test
	public void testAuthor(){
		new WebDriverWait(getDriver(), 4).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[text()[contains(.,'news_eng')]]"))).click();

		searchPage.findElement(By.xpath("//label[text()[contains(.,'Public')]]/../i")).click();

		LoggerFactory.getLogger(SearchPageHostedITCase.class).info("Searching for: 'fruit'");
		topNavBar.search("fruit");
		searchPage.waitForSearchLoadIndicatorToDisappear();
		Assert.assertNotEquals(searchPage.getText(), contains("Haven OnDemand returned an error while executing the search action"));

		String author = "RUGBYBWORLDCUP.COM";

		searchPage.openParametricValuesList();

		int results = searchPage.filterByAuthor(author);

		((JavascriptExecutor) getDriver()).executeScript("scroll(0,-400);");

		searchPage.loadOrFadeWait();
		searchPage.waitForSearchLoadIndicatorToDisappear();
		searchPage.loadOrFadeWait();

		ABCAssert.assertThat(searchPage.searchTitle().findElement(By.xpath(".//..//span")).getText(), is("(" + results + ")"));

		searchPage.getSearchResult(1).click();

		for(int i = 0; i < results; i++) {
			ABCAssert.assertThat(new WebDriverWait(getDriver(), 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//th[text()[contains(.,'Author')]]/..//li"))).getText(), is(author.toLowerCase()));
			getDriver().findElement(By.className("fa-chevron-circle-right")).click();
		}

		getDriver().findElement(By.className("fa-close")).click();

		searchPage.loadOrFadeWait();

		searchPage.filterByAuthor(author); //'Unfilter'

		searchPage.loadOrFadeWait();
		searchPage.waitForSearchLoadIndicatorToDisappear();
		searchPage.loadOrFadeWait();

		author = "YLEIS";

		results = searchPage.filterByAuthor(author);

		((JavascriptExecutor) getDriver()).executeScript("scroll(0,-400);");

		searchPage.loadOrFadeWait();
		searchPage.waitForSearchLoadIndicatorToDisappear();
		searchPage.loadOrFadeWait();

		ABCAssert.assertThat(searchPage.searchTitle().findElement(By.xpath(".//..//span")).getText(), is("(" + results + ")"));

		searchPage.getSearchResult(1).click();

		for(int i = 0; i < results; i++) {
			ABCAssert.assertThat(new WebDriverWait(getDriver(), 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//th[text()[contains(.,'Author')]]/..//li"))).getText(), is("Yleis"));
			getDriver().findElement(By.className("fa-chevron-circle-right")).click();
		}

		getDriver().findElement(By.className("fa-close")).click();
	}

}

@@ -1119,16 +1119,73 @@
 ontains;
+%0Aimport static org.hamcrest.Matchers.equalToIgnoringCase;
 %0A%0Apublic
@@ -3418,336 +3418,96 @@
 %7B%0A%09%09
-new WebDriverWait(getDriver(), 4).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(%22//label%5Btext()%5Bcontains(.,'news_eng')%5D%5D%22))).click();%0A%0A%09%09searchPage.findElement(By.xpath(%22//label%5Btext()%5Bcontains(.,'Public')%5D%5D/../i%22)).click();%0A%0A%09%09LoggerFactory.getLogger(SearchPageHostedITCase.class).info(%22Searching for: 'fruit'%22);
+searchPage.findElement(By.xpath(%22//label%5Btext()%5Bcontains(.,'Public')%5D%5D/../i%22)).click();%0A
 %0A%09%09t
@@ -3738,22 +3738,12 @@
  = %22
-RUGBYBWORLDCUP
+FIFA
 .COM
@@ -4413,31 +4413,34 @@
 (), 
-is(author.toLowerCase()
+equalToIgnoringCase(author
 ));%0A

update testAuthor test case