package com.autonomy.abc.selenium.find;

import com.autonomy.abc.selenium.find.application.FindApplication;
import com.autonomy.abc.selenium.find.application.FindElementFactory;
import com.autonomy.abc.selenium.find.results.ResultsView;
import com.autonomy.abc.selenium.find.results.SimilarDocumentsView;
import com.autonomy.abc.selenium.query.AggregateQueryFilter;
import com.autonomy.abc.selenium.query.Query;
import com.autonomy.abc.selenium.query.QueryService;
import com.hp.autonomy.frontend.selenium.element.FormInput;
import com.hp.autonomy.frontend.selenium.util.Waits;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class FindService implements QueryService<ResultsView> {
    private final FindElementFactory elementFactory;
    private final FindPage findPage;

    public FindService(final FindApplication<?> find) {
        elementFactory = find.elementFactory();
        findPage = elementFactory.getFindPage();
    }

    @Override
    public ResultsView search(final String query){
        return search(new Query(query));
    }

    @Override
    public ResultsView search(final Query query) {
        search(elementFactory.getSearchBox(), query.getTerm());
        elementFactory.getFilterPanel().waitForIndexes();
        findPage.filterBy(new AggregateQueryFilter(query.getFilters()));
        return elementFactory.getResultsPage();
    }

    private void search(final FormInput formInput, final String term) {
        formInput.clear();
        formInput.setValue(term);
        Waits.loadOrFadeWait();
        formInput.submit();
    }

    public SimilarDocumentsView goToSimilarDocuments(final int resultNumber) {
        final ResultsView resultsPage = elementFactory.getResultsPage();
        resultsPage.getResult(resultNumber).similarDocuments().click();
        SimilarDocumentsView similarDocuments = elementFactory.getSimilarDocumentsView();
        similarDocuments.waitForLoad();
        return similarDocuments;
    }

    /**
     * @param query The query text
     * @return The URL of the search page for the given query text, relative to the application context path
     */
    public String getQueryUrl(final String query) {
        try {
            return "public/search/query/" + URLEncoder.encode(query, StandardCharsets.UTF_8.name());
        } catch (final UnsupportedEncodingException e) {
            throw new IllegalStateException("UTF-8 not supported", e);
        }
    }
}

@@ -1189,23 +1189,16 @@
         
-search(
 elementF
@@ -1218,18 +1218,30 @@
 rchBox()
-, 
+.setAndSubmit(
 query.ge
@@ -1377,24 +1377,24 @@
 ilters()));%0A
+
         retu
@@ -1440,208 +1440,8 @@
  %7D%0A%0A
-    private void search(final FormInput formInput, final String term) %7B%0A        formInput.clear();%0A        formInput.setValue(term);%0A        Waits.loadOrFadeWait();%0A        formInput.submit();%0A    %7D%0A%0A
     

Remove unnecessary search method