package com.autonomy.abc.connections;

import com.autonomy.abc.config.HostedTestBase;
import com.autonomy.abc.config.TestConfig;
import com.autonomy.abc.selenium.config.ApplicationType;
import com.autonomy.abc.selenium.connections.ConnectionService;
import com.autonomy.abc.selenium.connections.Credentials;
import com.autonomy.abc.selenium.connections.SecureWebConnector;
import com.autonomy.abc.selenium.connections.WebConnector;
import com.autonomy.abc.selenium.menu.NavBarTabId;
import com.autonomy.abc.selenium.page.connections.ConnectionsPage;
import com.autonomy.abc.selenium.page.connections.NewConnectionPage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Platform;

import static com.autonomy.abc.framework.ABCAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class ConnectionsPageITCase extends HostedTestBase {
    private ConnectionsPage connectionsPage;
    private NewConnectionPage newConnectionPage;
    private ConnectionService connectionService;

    public ConnectionsPageITCase(TestConfig config, String browser, ApplicationType type, Platform platform) {
        super(config, browser, type, platform);
    }

    @Before @Override
    public void baseSetUp() throws InterruptedException {
        regularSetUp();
        hostedLogIn("yahoo");

        body.getSideNavBar().switchPage(NavBarTabId.CONNECTIONS);
        connectionsPage = getElementFactory().getConnectionsPage();
        connectionService = getApplication().createConnectionService(getElementFactory());
        body = getBody();
    }

    @Test
    public void testNavigateToConnectorViaURL(){
        WebConnector webConnector = new WebConnector("http://www.bbc.co.uk","bbc").withDepth(2);
        connectionService.setUpConnection(webConnector);
        connectionService.goToDetails(webConnector);

        String url = getDriver().getCurrentUrl();

        body.getSideNavBar().switchPage(NavBarTabId.ANALYTICS);
        getElementFactory().getAnalyticsPage();

        navigateToConnectionViaURL(url);

        //For completeness try from both halves of the application
        body.getSideNavBar().switchPage(NavBarTabId.KEYWORDS);
        getElementFactory().getKeywordsPage();

        navigateToConnectionViaURL(url);
    }

    @Test
    public void testSecureWebConnector(){
//        WebConnector webConnector = new SecureWebConnector("http://www.facebook.com/settings","facebook");
    }

    private void navigateToConnectionViaURL(String url){
        getDriver().navigate().to(url);
        getElementFactory().getConnectionsDetailPage();
        assertThat(getDriver().getCurrentUrl(), is(url));
    }

    @After
    public void tearDown(){
        connectionService.deleteAllConnections(true);
    }
}

@@ -293,17 +293,26 @@
 ns.C
-redential
+onnectionStatistic
 s;%0Ai
@@ -355,34 +355,27 @@
 ections.
-SecureWebConnector
+Credentials
 ;%0Aimport
@@ -478,16 +478,89 @@
 rTabId;%0A
+import com.autonomy.abc.selenium.page.connections.ConnectionsDetailPage;%0A
 import c
@@ -797,16 +797,16 @@
 tform;%0A%0A
-
 import s
@@ -860,16 +860,124 @@
 rtThat;%0A
+import static com.autonomy.abc.framework.ABCAssert.verifyThat;%0Aimport static org.hamcrest.CoreMatchers.not;%0A
 import s
@@ -2522,18 +2522,70 @@
 ctor()%7B%0A
-//
+        String email = %22matthew.williamson@hpe.com%22;%0A%0A
         
@@ -2616,22 +2616,16 @@
 r = new 
-Secure
 WebConne
@@ -2673,17 +2673,460 @@
 facebook
-%22
+secure%22, new Credentials(email,%22vdPAuTGU%22,email)).withDuration(200);%0A%0A        connectionService.setUpConnection(webConnector);%0A        connectionService.goToDetails(webConnector);%0A%0A        connectionService.updateLastRun(webConnector);%0A        ConnectionStatistics connectionStatistics = webConnector.getStatistics();%0A%0A        verifyThat(connectionStatistics.getDetected(), not(0));%0A        verifyThat(connectionStatistics.getIngested(), not(0)
 );%0A    %7D

test case