package com.autonomy.abc.selenium.page.promotions;

import com.autonomy.abc.selenium.actions.wizard.OptionWizardStep;
import com.autonomy.abc.selenium.element.FormInput;
import com.autonomy.abc.selenium.promotions.DynamicPromotion;
import com.autonomy.abc.selenium.promotions.SearchTriggerStep;
import com.hp.autonomy.frontend.selenium.util.AppElement;
import com.hp.autonomy.frontend.selenium.util.AppPage;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class CreateNewPromotionsBase extends AppElement implements AppPage {

	public CreateNewPromotionsBase(final WebDriver driver) {
		super(new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.className("wrapper-content"))), driver);
	}

	@Deprecated
	public WebElement continueButton(final WizardStep dataStep) {
		return continueButton();
	}


	public String getCurrentStepTitle() {
		return findElement(By.cssSelector(".current-step-pill .current-step-title")).getText();
	}

	public enum WizardStep {
		TYPE("type"),
		PROMOTION_TYPE("promotionType"),
		RESULTS("results"),
		TRIGGER("triggers");


		private final String title;

		WizardStep(final String title) {
			this.title = title;
		}

		public String getTitle() {
			return title;
		}
	}

	private WebElement getVisibleElement(By by) {
		return new WebDriverWait(getDriver(), 5).until(ExpectedConditions.visibilityOfElementLocated(By.className("current-step"))).findElement(by);
	}

	public WebElement continueButton() {
		return getVisibleElement(By.className("next-step"));
	}

	public WebElement finishButton() {
		return getVisibleElement(By.className("finish-step"));
	}

	public WebElement cancelButton() {
		return getVisibleElement(By.className("cancel-wizard"));
	}

	@Deprecated
	public WebElement cancelButton(final WizardStep dataStep) {
		return cancelButton();
	}

	public FormInput triggerBox() {
		return new FormInput(findElement(By.cssSelector("input[name='words']")), getDriver());
	}

	public void addSearchTrigger(final String searchTrigger) {
		triggerBox().setValue(searchTrigger);

		try {
			loadOrFadeWait();
			waitUntilClickableThenClick(triggerAddButton());
		} catch (final Exception e) {
			System.out.println("could not click trigger button with trigger " + searchTrigger);
		}
	}

	public void removeSearchTrigger(final String searchTrigger) {
		waitUntilClickableThenClick(By.xpath(".//span[contains(text(), '" + searchTrigger + "')]/i"));
	}

	public List<String> getSearchTriggersList() {
		final List<String> searchTriggerList = new ArrayList<>();

		for (final WebElement trigger : findElements(By.cssSelector(".remove-word"))) {
			searchTriggerList.add(getParent(trigger).getText());
		}

		return searchTriggerList;
	}

	public WebElement triggerAddButton() {
		return findElement(By.cssSelector(".trigger-words-form")).findElement(By.xpath(".//i[contains(@class, 'fa-plus')]/.."));
	}

	public String getTriggerError() {
		try {
			return findElement(By.className("help-block")).getText();
		} catch (NoSuchElementException e) {
			return null;
		}
	}

	public WebElement spotlightType(final String type ) {
		return getParent(findElement(By.cssSelector("[data-option='" + type + "']")));
	}

	// "visited" by the promotion
	public List<com.autonomy.abc.selenium.actions.wizard.WizardStep> getDynamicWizardSteps(DynamicPromotion promotion) {
		return Arrays.asList(
				new OptionWizardStep(this, "Spotlight type", promotion.getSpotlightType().getOption()),
				new SearchTriggerStep(this, promotion.getTrigger())
		);
	}

	/*
	public void addSpotlightPromotion(final String spotlightType, final String searchTrigger, final String type) {
		promotionType("SPOTLIGHT").click();
		loadOrFadeWait();
		continueButton(WizardStep.TYPE).click();
		loadOrFadeWait();
		if (type.equals("On Premise")) {
			spotlightType(spotlightType).click();
			loadOrFadeWait();
			continueButton(WizardStep.PROMOTION_TYPE).click();
			loadOrFadeWait();
		}
		addSearchTrigger(searchTrigger);
		loadOrFadeWait();
		finishButton().click();
		loadOrFadeWait();
	}*/

	public WebElement promotionType(final String promotionType) {
		return getParent(findElement(By.cssSelector("[data-option='" + promotionType + "']")));
	}

	@Override
	public void waitForLoad() {
		new WebDriverWait(getDriver(),30).until(ExpectedConditions.visibilityOfElementLocated(By.className("pd-wizard")));
	}
}

@@ -107,24 +107,84 @@
 WizardStep;%0A
+import com.autonomy.abc.selenium.actions.wizard.WizardStep;%0A
 import com.a
@@ -223,16 +223,16 @@
 mInput;%0A
-
 import c
@@ -1096,116 +1096,8 @@
 %09%7D%0A%0A
-%09@Deprecated%0A%09public WebElement continueButton(final WizardStep dataStep) %7B%0A%09%09return continueButton();%0A%09%7D%0A%0A%0A
 %09pub
@@ -1229,280 +1229,8 @@
 %09%7D%0A%0A
-%09public enum WizardStep %7B%0A%09%09TYPE(%22type%22),%0A%09%09PROMOTION_TYPE(%22promotionType%22),%0A%09%09RESULTS(%22results%22),%0A%09%09TRIGGER(%22triggers%22);%0A%0A%0A%09%09private final String title;%0A%0A%09%09WizardStep(final String title) %7B%0A%09%09%09this.title = title;%0A%09%09%7D%0A%0A%09%09public String getTitle() %7B%0A%09%09%09return title;%0A%09%09%7D%0A%09%7D%0A%0A
 %09pri
@@ -1716,111 +1716,8 @@
 %09%7D%0A%0A
-%09@Deprecated%0A%09public WebElement cancelButton(final WizardStep dataStep) %7B%0A%09%09return cancelButton();%0A%09%7D%0A%0A
 %09pub
@@ -3099,16 +3099,16 @@
 omotion%0A
+
 %09public 
@@ -3116,49 +3116,8 @@
 ist%3C
-com.autonomy.abc.selenium.actions.wizard.
 Wiza

Remove old CreateNewPromotionsBase.WizardStep