package com.autonomy.abc.promotions;

import com.autonomy.abc.Trigger.SharedTriggerTests;
import com.autonomy.abc.config.HostedTestBase;
import com.autonomy.abc.config.TestConfig;
import com.autonomy.abc.selenium.actions.wizard.Wizard;
import com.autonomy.abc.selenium.element.TriggerForm;
import com.autonomy.abc.selenium.menu.NavBarTabId;
import com.autonomy.abc.selenium.page.promotions.HSOCreateNewPromotionsPage;
import com.autonomy.abc.selenium.page.promotions.PromotionsDetailPage;
import com.autonomy.abc.selenium.promotions.PromotionService;
import com.autonomy.abc.selenium.promotions.SearchTriggerStep;
import com.autonomy.abc.selenium.promotions.StaticPromotion;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

import java.util.List;

import static com.autonomy.abc.framework.ABCAssert.assertThat;
import static com.autonomy.abc.framework.ABCAssert.verifyThat;
import static com.autonomy.abc.matchers.ElementMatchers.disabled;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;

public class CreateStaticPromotionsITCase extends HostedTestBase {
    private HSOCreateNewPromotionsPage createPromotionsPage;
    private PromotionService promotionService;
    private Wizard wizard;
    private TriggerForm triggerForm;

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

    public void goToTriggerStep() {
        StaticPromotion promotion = new StaticPromotion("dog", "woof woof", "");
        wizard = promotion.makeWizard(createPromotionsPage);
        wizard.getCurrentStep().apply();
        wizard.next();
        wizard.getCurrentStep().apply();
        wizard.next();
    }

    @Before
    public void setUp() {
        getElementFactory().getSideNavBar().switchPage(NavBarTabId.PROMOTIONS);
        getElementFactory().getPromotionsPage().staticPromotionButton().click();
        createPromotionsPage = getElementFactory().getCreateNewPromotionsPage();
        promotionService = getApplication().createPromotionService(getElementFactory());
    }

    @After
    public void tearDown() {
        promotionService.deleteAll();
    }

    @Test
    public void testAddStaticPromotion() {
        final String title = "static promotion";
        final String content = "This is the body of my static promotion.";
        final String trigger = "horse";
        Wizard wizard = new StaticPromotion(title, content, trigger).makeWizard(createPromotionsPage);

        WebElement continueButton = createPromotionsPage.continueButton();
        verifyThat(createPromotionsPage.getCurrentStepTitle(), is(wizard.getCurrentStep().getTitle()));
        verifyThat(continueButton, disabled());
        createPromotionsPage.documentTitle().setValue("removed");
        verifyThat(continueButton, disabled());
        createPromotionsPage.documentContent().setValue(content);
        verifyThat(continueButton, not(disabled()));
        // cannot just clear as this does not trigger correct JS event
        createPromotionsPage.documentTitle().setValue("a" + Keys.BACK_SPACE);
        verifyThat(continueButton, disabled());
        createPromotionsPage.documentTitle().setValue(title);
        wizard.next();

        // TODO: languages once enabled
        verifyThat(createPromotionsPage.getCurrentStepTitle(), is(wizard.getCurrentStep().getTitle()));
        verifyThat(createPromotionsPage.continueButton(), not(disabled()));
        wizard.getCurrentStep().apply();
        wizard.next();

        verifyThat(createPromotionsPage.getCurrentStepTitle(), is(wizard.getCurrentStep().getTitle()));
        verifyThat(createPromotionsPage.finishButton(), disabled());
        wizard.getCurrentStep().apply();
        verifyThat(createPromotionsPage.finishButton(), not(disabled()));
        wizard.next();

        getElementFactory().getSearchPage();
    }

    @Test
    public void testInvalidTriggers() {
        goToTriggerStep();
        SharedTriggerTests.badTriggersTest(createPromotionsPage.getTriggerForm());
    }

    @Test
    public void testAddRemoveTriggers() {
        goToTriggerStep();

        triggerForm = createPromotionsPage.getTriggerForm();

        assertThat(createPromotionsPage.getCurrentStepTitle(), is(SearchTriggerStep.TITLE));
        verifyThat(triggerForm.getTriggers(), empty());

        SharedTriggerTests.addRemoveTriggers(triggerForm, createPromotionsPage.cancelButton(), createPromotionsPage.finishButton());

        // finish wizard, wait
        wizard.next();
        getElementFactory().getSearchPage();

        PromotionsDetailPage promotionsDetailPage = promotionService.goToDetails("delta");
        assertThat("loaded details page", promotionsDetailPage.promotionTitle().getValue(), containsString("delta"));

        List<String> triggers = triggerForm.getTriggersAsStrings();
        verifyThat(triggers, hasSize(2));
        verifyThat(triggers, hasItems("delta", "epsilon"));
    }
}

@@ -4616,24 +4616,105 @@
 Button());%0A%0A
+        final List%3CString%3E wizardTriggers = triggerForm.getTriggersAsStrings();%0A%0A
         // f
@@ -4882,23 +4882,37 @@
 Details(
-%22delta%22
+wizardTriggers.get(0)
 );%0A     
@@ -5013,23 +5013,37 @@
 sString(
-%22delta%22
+wizardTriggers.get(0)
 ));%0A%0A   
@@ -5060,17 +5060,24 @@
 String%3E 
-t
+createdT
 riggers 
@@ -5070,33 +5070,57 @@
 eatedTriggers = 
-t
+promotionsDetailPage.getT
 riggerForm.getTr
@@ -5105,32 +5105,34 @@
 e.getTriggerForm
+()
 .getTriggersAsSt
@@ -5151,33 +5151,40 @@
      verifyThat(
-t
+createdT
 riggers, hasSize
@@ -5188,9 +5188,29 @@
 ize(
-2
+wizardTriggers.size()
 ));%0A
@@ -5224,25 +5224,32 @@
  verifyThat(
-t
+createdT
 riggers, has
@@ -5249,35 +5249,38 @@
 rs, 
-hasItems(%22delta%22, %22epsilon%22
+everyItem(isIn(wizardTriggers)
 ));%0A

Fix CreateStaticPromotionsITCase.testAddRemoveTriggers