package com.autonomy.abc.selenium.find;

import com.autonomy.abc.selenium.find.application.FindElementFactory;
import com.hp.autonomy.frontend.selenium.application.LoginService;
import com.hp.autonomy.frontend.selenium.application.PageMapper;
import com.hp.autonomy.frontend.selenium.login.LoginPage;
import com.hp.autonomy.frontend.selenium.sso.HSOLoginPage;
import com.hp.autonomy.frontend.selenium.util.AppPage;
import com.hp.autonomy.frontend.selenium.util.ParametrizedFactory;
import org.openqa.selenium.WebDriver;

public class HsodFindElementFactory extends FindElementFactory {
    HsodFindElementFactory(WebDriver driver) {
        super(driver, new PageMapper<>(Page.class));
    }

    @Override
    public LoginPage getLoginPage() {
        return loadPage(LoginPage.class);
    }

    @Override
    public FindTopNavBar getTopNavBar() {
        return new FindTopNavBar(getDriver());
    }

    @Override
    public LoginService.LogoutHandler getLogoutHandler() {
        return getTopNavBar();
    }

    @Override
    public FindPage getFindPage() {
        return loadPage(FindPage.class);
    }

    @Override
    public FindResultsPage getResultsPage() {
        return getFindPage().getResultsPage();
    }

    @Override
    public SimilarDocumentsView getSimilarDocumentsView() {
        return loadPage(SimilarDocumentsView.class);
    }

    private enum Page implements PageMapper.Page {
        LOGIN(new ParametrizedFactory<WebDriver, HSOLoginPage>() {
            @Override
            public HSOLoginPage create(WebDriver context) {
                return new HSOLoginPage(context, new FindHasLoggedIn(context));
            }
        }, HSOLoginPage.class),
        MAIN(new FindPage.Factory(), FindPage.class),
        SIMILAR_DOCUMENTS(new SimilarDocumentsView.Factory(), SimilarDocumentsView.class);

        private final Class<? extends AppPage> pageType;
        private ParametrizedFactory<WebDriver, ? extends AppPage> factory;

        <T extends AppPage> Page(ParametrizedFactory<WebDriver, ? extends T> factory, Class<T> type) {
            pageType = type;
            this.factory = factory;
        }

        @Override
        public Class<?> getPageType() {
            return pageType;
        }

        public Object loadAsObject(WebDriver driver) {
            return this.factory.create(driver);
        }
    }
}

@@ -175,73 +175,8 @@
 ce;%0A
-import com.hp.autonomy.frontend.selenium.application.PageMapper;%0A
 impo
@@ -347,75 +347,8 @@
 ge;%0A
-import com.hp.autonomy.frontend.selenium.util.ParametrizedFactory;%0A
 impo
@@ -521,35 +521,11 @@
 r, n
-ew PageMapper%3C%3E(Page.class)
+ull
 );%0A 
@@ -601,32 +601,70 @@
 urn 
-loadPage(LoginPage.class
+new HSOLoginPage(getDriver(), new FindHasLoggedIn(getDriver())
 );%0A 
@@ -959,31 +959,49 @@
 urn 
-loadPage(FindPage.class
+new FindPage.Factory().create(getDriver()
 );%0A 
@@ -1213,17 +1213,12 @@
 urn 
-loadPage(
+new 
 Simi
@@ -1238,149 +1238,46 @@
 iew.
-class);%0A    %7D%0A%0A    private enum Page implements PageMapper.Page %7B%0A        LOGIN(new ParametrizedFactory%3CWebDriver, HSOLoginPage%3E() %7B%0A  
+Factory().create(getDriver());%0A
     
-  
+%7D%0A%0A
     
@@ -1286,24 +1286,16 @@
 verride%0A
-        
     publ
@@ -1301,843 +1301,166 @@
 lic 
-HSOLoginPage create(WebDriver context) %7B%0A                return new HSOLoginPage(context, new FindHasLoggedIn(context));%0A            %7D%0A        %7D, HSOLogin
+%3CT extends AppPage%3E T load
 Page
-.c
+(C
 lass
-),%0A        MAIN(new FindPage.Factory(), FindPage.class),%0A        SIMILAR_DOCUMENTS(new SimilarDocumentsView.Factory(), SimilarDocumentsView.class);%0A%0A        private final Class%3C? extends AppPage%3E pageType;%0A        private ParametrizedFactory%3CWebDriver, ? extends AppPage%3E factory;%0A%0A        %3CT extends AppPage%3E Page(ParametrizedFactory%3CWebDriver, ? extends T%3E factory, Class%3CT%3E type) %7B%0A            pageType = type;%0A            this.factory = factory;%0A        %7D%0A%0A        @Override%0A        public Class%3C?%3E getPageType() %7B%0A            return pageType;%0A        %7D%0A%0A        public Object loadAsObject(WebDriver driver) %7B%0A            return this.factory.create(driver);%0A        %7D
+%3CT%3E type) %7B%0A        throw new UnsupportedOperationException(%22loadPage does not make sense for a single page application%22);
 %0A   

Simplify HsodFindElementFactory