package com.autonomy.abc.topnavbar.notifications;

import com.autonomy.abc.base.HybridIsoTestBase;
import com.autonomy.abc.selenium.analytics.DashboardBase;
import com.autonomy.abc.selenium.application.IsoApplication;
import com.autonomy.abc.selenium.keywords.KeywordFilter;
import com.autonomy.abc.selenium.keywords.KeywordService;
import com.autonomy.abc.selenium.keywords.KeywordsPage;
import com.autonomy.abc.selenium.menu.TopNavBar;
import com.autonomy.abc.selenium.promotions.PromotionService;
import com.autonomy.abc.selenium.promotions.SpotlightPromotion;
import com.hp.autonomy.frontend.selenium.config.TestConfig;
import com.hp.autonomy.frontend.selenium.control.Window;
import com.hp.autonomy.frontend.selenium.element.GritterNotice;
import com.hp.autonomy.frontend.selenium.framework.logging.KnownBug;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;

import static com.hp.autonomy.frontend.selenium.framework.state.TestStateAssert.assertThat;
import static com.hp.autonomy.frontend.selenium.framework.state.TestStateAssert.verifyThat;
import static org.hamcrest.Matchers.*;

public class MultiWindowNotificationsITCase extends HybridIsoTestBase {
    private KeywordService keywordService;
    private PromotionService promotionService;

    private AppWindow first;
    private TopNavBar topNavBar;

    private AppWindow second;
    private TopNavBar topNavBarWindowTwo;

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

    @Before
    public void setUp() {
        keywordService = getApplication().keywordService();
        promotionService = getApplication().promotionService();

        topNavBar = getElementFactory().getTopNavBar();
        first = new AppWindow(getApplication(), getWindow());

        IsoApplication<?> secondApp = IsoApplication.ofType(getConfig().getType());
        Window secondWindow = launchInNewWindow(secondApp);
        second = new AppWindow(secondApp, secondWindow);
        second.activate();
        topNavBarWindowTwo = secondApp.elementFactory().getTopNavBar();
    }

    @After
    public void tearDown() {
        if (hasSetUp()) {
            second.activate();
            topNavBarWindowTwo.closeNotifications();
            keywordService.deleteAll(KeywordFilter.ALL);
            promotionService.deleteAll();
        }
    }

    @Test
    @KnownBug("CSA-1542")
    public void testNotificationsOverTwoWindows() throws InterruptedException {
        first.activate();
        keywordService.goToKeywords();
        topNavBar.notificationsDropdown();
        assertThat(first.countNotifications(), is(0));

        second.activate();
        second.goToKeywords();
        topNavBarWindowTwo.notificationsDropdown();
        assertThat(second.countNotifications(), is(0));

        first.activate();
        keywordService.addSynonymGroup("Animal Beast");
        keywordService.goToKeywords();

        topNavBar.notificationsDropdown();
        assertThat(first.countNotifications(), is(1));
        String windowOneNotificationText = first.mostRecentNotification();

        second.activate();
        assertThat(second.countNotifications(), is(1));
        assertThat(second.mostRecentNotification(), is(windowOneNotificationText));
        topNavBarWindowTwo.notificationsDropdown();
        KeywordsPage keywordsPageWindowTwo = getElementFactory().getKeywordsPage();
        keywordsPageWindowTwo.deleteSynonym("Animal", "Animal");
        topNavBarWindowTwo.notificationsDropdown();
        assertThat(second.countNotifications(), is(2));
        List<String> notificationMessages = second.allNotifications();

        first.activate();
        assertThat(first.countNotifications(), is(2));
        assertThat(first.allNotifications(), contains(notificationMessages.toArray()));

        first.switchToDashboard();
        newBody();
        topNavBar.notificationsDropdown();
        assertThat(first.countNotifications(), is(2));
        assertThat(first.allNotifications(), contains(notificationMessages.toArray()));

        second.activate();

        promotionService.setUpPromotion(new SpotlightPromotion("wheels"), "cars", 3);

        new WebDriverWait(getDriver(), 5).until(GritterNotice.notificationAppears());
        topNavBarWindowTwo.notificationsDropdown();

        assertThat(second.countNotifications(), is(3));
        assertThat(second.mostRecentNotification(), containsString("promotion"));

        notificationMessages = second.allNotifications();

        first.activate();

        assertThat(first.countNotifications(), is(3));
        assertThat(first.allNotifications(), contains(notificationMessages.toArray()));

        int notificationsCount = 3;
        for(int i = 0; i < 6; i += 2) {
            second.activate();
            keywordService.addSynonymGroup(i + " " + (i + 1));
            keywordService.goToKeywords();

            second.openNotifications();
            verifyThat(second.countNotifications(), is(Math.min(++notificationsCount, 5)));
            notificationMessages = second.allNotifications();

            first.activate();
            verifyThat(first.countNotifications(), is(Math.min(notificationsCount, 5)));
            verifyThat(first.allNotifications(), contains(notificationMessages.toArray()));
        }
    }

    private void newBody() {
        topNavBar = getElementFactory().getTopNavBar();
    }

    private static class AppWindow {
        private final IsoApplication<?> app;
        private final Window window;
        private final KeywordService keywordService;

        AppWindow(IsoApplication<?> app, Window window) {
            this.app = app;
            this.window = window;
            keywordService = app.keywordService();
        }

        void activate() {
            window.activate();
        }

        void switchToDashboard() {
            app.switchTo(DashboardBase.class);
        }

        void goToKeywords() {
            keywordService.goToKeywords();
        }

        void openNotifications() {
            getNavBar().openNotifications();
        }

        int countNotifications() {
            return getNavBar().getNotifications().countNotifications();
        }

        List<String> allNotifications() {
            return getNavBar().getNotifications().getAllNotificationMessages();
        }

        String mostRecentNotification() {
            return getNavBar().getNotifications().notificationNumber(1).getText();
        }

        private TopNavBar getNavBar() {
            return app.elementFactory().getTopNavBar();
        }
    }
}

@@ -1389,100 +1389,24 @@
 ate 
-TopNavBar topNavBar;%0A%0A    private AppWindow second;%0A    private TopNavBar topNavBarWindowTwo
+AppWindow second
 ;%0A%0A 
@@ -1664,64 +1664,8 @@
 );%0A%0A
-        topNavBar = getElementFactory().getTopNavBar();%0A
     
@@ -1928,107 +1928,8 @@
 w);%0A
-        second.activate();%0A        topNavBarWindowTwo = secondApp.elementFactory().getTopNavBar();%0A
     
@@ -2040,34 +2040,22 @@
         
-topNavBarWindowTwo
+second
 .closeNo
@@ -2370,51 +2370,8 @@
 ();%0A
-        topNavBar.notificationsDropdown();%0A
     
@@ -2484,60 +2484,8 @@
 ();%0A
-        topNavBarWindowTwo.notificationsDropdown();%0A
     
@@ -2663,51 +2663,8 @@
 );%0A%0A
-        topNavBar.notificationsDropdown();%0A
     
@@ -2965,36 +2965,29 @@
         
-topNavBarWindowTwo.n
+second.closeN
 otificat
@@ -2986,32 +2986,24 @@
 otifications
-Dropdown
 ();%0A        
@@ -3147,60 +3147,8 @@
 %22);%0A
-        topNavBarWindowTwo.notificationsDropdown();%0A
     
@@ -3480,70 +3480,8 @@
 ();%0A
-        newBody();%0A        topNavBar.notificationsDropdown();%0A
     
@@ -3824,60 +3824,8 @@
 ());
-%0A        topNavBarWindowTwo.notificationsDropdown();
 %0A%0A  
@@ -4409,48 +4409,8 @@
 );%0A%0A
-            second.openNotifications();%0A
     
@@ -4792,100 +4792,8 @@
  %7D%0A%0A
-    private void newBody() %7B%0A        topNavBar = getElementFactory().getTopNavBar();%0A    %7D%0A%0A
     
@@ -5471,32 +5471,125 @@
 s();%0A        %7D%0A%0A
+        void closeNotifications() %7B%0A            getNavBar().closeNotifications();%0A        %7D%0A%0A
         int coun
@@ -5599,32 +5599,65 @@
 tifications() %7B%0A
+            openNotifications();%0A
             retu

Remove TopNavBar instance variables