package com.autonomy.abc.search;

import com.autonomy.abc.config.ABCTestBase;
import com.autonomy.abc.config.TestConfig;
import com.autonomy.abc.framework.KnownBug;
import com.autonomy.abc.framework.RelatedTo;
import com.autonomy.abc.query.QueryTestHelper;
import com.autonomy.abc.selenium.application.ApplicationType;
import com.autonomy.abc.selenium.error.Errors;
import com.autonomy.abc.selenium.language.Language;
import com.autonomy.abc.selenium.query.LanguageFilter;
import com.autonomy.abc.selenium.query.Query;
import com.autonomy.abc.selenium.search.SearchPage;
import com.autonomy.abc.selenium.search.SearchService;
import org.junit.Before;
import org.junit.Test;

import java.io.Serializable;
import java.net.MalformedURLException;

import static com.autonomy.abc.framework.ABCAssert.assertThat;
import static com.autonomy.abc.matchers.StringMatchers.containsString;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.not;

public class SearchErrorMessageITCase extends ABCTestBase {
    private SearchService searchService;

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

    @Before
    public void setUp() throws MalformedURLException {
        searchService = getApplication().searchService();
    }

    @Test
    @KnownBug("CCUK-3741")
    public void testSearchParentheses() {
        new QueryTestHelper<>(searchService).mismatchedBracketQueryText();
    }

    @Test
    @KnownBug({"IOD-8454", "CCUK-3741"})
    @RelatedTo("CCUK-3747")
    public void testSearchQuotationMarks() {
        Serializable error;
        if (getConfig().getType().equals(ApplicationType.HOSTED)) {
            error = Errors.Search.INVALID;
        } else {
            error = Errors.Search.QUOTES;
        }
        new QueryTestHelper<>(searchService).mismatchedQuoteQueryText(error);
    }


    @Test
    @KnownBug("CCUK-3741")
    @RelatedTo("CCUK-3747")
    public void testQueriesWithNoTerms() {
        Serializable booleanError;
        Serializable emptyError;
        if(getConfig().getType().equals(ApplicationType.HOSTED)) {
            booleanError = Errors.Search.INVALID;
            emptyError = Errors.Search.INVALID;
        } else {
            booleanError = Errors.Search.OPENING_BOOL;
            emptyError = Errors.Search.NO_TEXT;
        }

        new QueryTestHelper<>(searchService).booleanOperatorQueryText(booleanError);
        new QueryTestHelper<>(searchService).emptyQueryText(emptyError);
    }

    @Test
    public void testQueryAnalysisForBadQueries() {
        for (final String term : QueryTestHelper.NO_TERMS) {
            Query query = new Query(term).withFilter(new LanguageFilter(Language.ENGLISH));
            String error = searchService.search(query).getKeywordError();
            assertThat(error, not(isEmptyOrNullString()));
            assertThat(error, containsString(Errors.Keywords.NO_TERMS));
        }
    }

    @Test
    public void testAllowSearchOfStringsThatContainBooleansWithinThem() {
        new QueryTestHelper<>(searchService).hiddenQueryOperatorText();
    }
}

@@ -254,70 +254,8 @@
 er;%0A
-import com.autonomy.abc.selenium.application.ApplicationType;%0A
 impo
@@ -297,16 +297,16 @@
 Errors;%0A
+
 import c
@@ -454,60 +454,8 @@
 ry;%0A
-import com.autonomy.abc.selenium.search.SearchPage;%0A
 impo
@@ -1393,36 +1393,8 @@
 %22%7D)%0A
-    @RelatedTo(%22CCUK-3747%22)%0A
     
@@ -1446,96 +1446,92 @@
     
-Serializable error;%0A        if (getConfig().getType().equals(ApplicationType.HOSTED)) %7B%0A
+@RelatedTo(%22CCUK-3747%22)%0A        Serializable error = getApplication().isHosted() ?%0A 
     
@@ -1530,39 +1530,34 @@
  ?%0A             
-error =
+  
  Errors.Search.I
@@ -1566,46 +1566,10 @@
 ALID
-;%0A        %7D else %7B%0A            error =
+ :
  Err
@@ -1587,26 +1587,16 @@
 QUOTES;%0A
-        %7D%0A
         
@@ -1714,36 +1714,8 @@
 1%22)%0A
-    @RelatedTo(%22CCUK-3747%22)%0A
     
@@ -1765,134 +1765,97 @@
     
-Serializable booleanError;%0A        Serializable emptyError;%0A        if(getConfig().getType().equals(ApplicationType.HOSTED)) %7B
+@RelatedTo(%22CCUK-3747%22)%0A        Serializable booleanError = getApplication().isHosted() ?
 %0A   
@@ -1859,38 +1859,27 @@
             
-booleanError =
+   
  Errors.Sear
@@ -1892,154 +1892,142 @@
 ALID
-;%0A            emptyError = Errors.Search.INVALID;%0A        %7D else %7B%0A            booleanError = Errors.Search.OPENING_BOOL;%0A            emptyError =
+ : Errors.Search.OPENING_BOOL;%0A        Serializable emptyError = getApplication().isHosted() ?%0A                Errors.Search.INVALID :
  Err
@@ -2045,26 +2045,16 @@
 NO_TEXT;
-%0A        %7D
 %0A%0A      

Use isHosted in SearchErrorMessage tests