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

import com.autonomy.abc.selenium.element.*;
import com.autonomy.abc.selenium.util.ElementUtil;
import com.autonomy.abc.selenium.util.Waits;
import com.hp.autonomy.frontend.selenium.element.ModalView;
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.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

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

public class PromotionsDetailPage extends AppElement implements AppPage {
    public PromotionsDetailPage(WebDriver driver) {
        super(new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.className("wrapper-content"))), driver);
        waitForLoad();
    }

    @Override
    public void waitForLoad() {
        new WebDriverWait(getDriver(), 10)
                .withMessage("Failed to load Promotions Detail Page")
                .until(ExpectedConditions.visibilityOfElementLocated(By.className("promotion-match-terms")));
    }

    public Dropdown editMenu() {
        return new Dropdown(findElement(By.className("extra-functions")), getDriver());
    }

    @Deprecated
    public void delete() {
        System.err.println("PromotionDetailsPage.delete no longer works, see CSA-1619");
        final Dropdown editMenu = editMenu();
        editMenu.open();
        editMenu.getItem("Delete").click();
        final ModalView deleteModal = ModalView.getVisibleModalView(getDriver());
        deleteModal.findElement(By.cssSelector(".btn-danger")).click();
    }

    public Editable promotionTitle() {
        return new InlineEdit(findElement(By.className("promotion-title-edit")), getDriver());
    }

    public String getPromotionType() {
        return findElement(By.cssSelector(".promotion-view-name")).getText();
    }

    public Dropdown spotlightTypeDropdown() {
        return new Dropdown(findElement(By.className("promotion-view-name-dropdown")), getDriver());
    }

    public Editable pinPosition() {
        final WebElement group = ElementUtil.getParent(findElement(By.cssSelector(".promotion-position-edit")));
        return new Editable() {
            @Override
            public String getValue() {
                return group.getText();
            }

            @Override
            public WebElement editButton() {
                return group.findElement(By.cssSelector(".inline-edit-open-form"));
            }

            @Override
            public void setValueAsync(String value) {
                int initialValue = Integer.valueOf(getValue());
                editButton().click();
                int desiredValue = Integer.valueOf(value);
                changeValue(initialValue, desiredValue);
                findElement(By.cssSelector(".hp-check")).click();
            }

            private void changeValue(int initialValue, int desiredValue) {
                WebElement button;
                int repeats;
                if (desiredValue > initialValue) {
                    button = group.findElement(By.cssSelector(".plus"));
                    repeats = desiredValue - initialValue;
                } else {
                    button = group.findElement(By.cssSelector(".minus"));
                    repeats = initialValue - desiredValue;
                }
                for (int i=0; i<repeats; i++) {
                    button.click();
                }
            }

            @Override
            public void setValueAndWait(String value) {
                setValueAsync(value);
                waitForUpdate();
            }

            @Override
            public void waitForUpdate() {
                new WebDriverWait(getDriver(), 20).withMessage("waiting for pin position to update").until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".promotion-position-edit .fa-refresh")));
            }

            @Override
            public WebElement getElement() {
                return group;
            }
        };
    }

    public String getLanguage() {
        return findElement(By.className("promotion-language")).getText();
    }

    public WebElement addMoreButton() {
        return findElement(By.className("add-more-promoted-documents"));
    }

    public List<WebElement> dynamicPromotedList(){
        return new WebDriverWait(getDriver(),10).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(".query-search-results div:not(.hide)>h3")));
    }

    public List<String> getDynamicPromotedTitles(){
        waitForDynamicLoadIndicatorToDisappear();
        final List<String> docTitles = new ArrayList<>(getVisibleDynamicPromotedTitles());

        while (ElementUtil.isEnabled(promotedQueryPaginationButton(Pagination.NEXT))) {
            switchPromotedQueryPage(Pagination.NEXT);
            docTitles.addAll(getVisibleDynamicPromotedTitles());
        }

        return docTitles;
    }

    private List<String> getVisibleDynamicPromotedTitles() {
        final List<String> docTitles = new ArrayList<>();

        for (final WebElement docTitle : dynamicPromotedList()) {
            if(!docTitle.getText().equals("Search for something...")) {
                docTitles.add(docTitle.getText());
            }
        }

        return docTitles;
    }

    public List<String> getPromotedTitles() {
        waitForPromotedTitlesToLoad();
        return ElementUtil.getTexts(promotedList());
    }

    private List<WebElement> promotedList() {
        return new WebDriverWait(getDriver(), 10).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(".promoted-documents-list h3")));
    }

    private void waitForPromotedTitlesToLoad() {
        new WebDriverWait(getDriver(), 20).until(ExpectedConditions.refreshed(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver input) {
                List<WebElement> docs = input.findElements(By.cssSelector(".promoted-documents-list h3"));
                return !(docs.isEmpty() || docs.get(0).getText().contains("Unknown Document"));
            }
        }));
    }

    private WebElement promotedQueryPaginationButton(Pagination pagination) {
        return pagination.findInside(promotedQueryPagination());
    }

    private WebElement promotedQueryPagination() {
        return findElement(By.cssSelector(".query-search-results .pagination-nav"));
    }

    private void switchPromotedQueryPage(Pagination pagination) {
        promotedQueryPaginationButton(pagination).click();
        waitForDynamicLoadIndicatorToDisappear();
    }

    private void waitForDynamicLoadIndicatorToDisappear() {
        Waits.loadOrFadeWait();
        new WebDriverWait(getDriver(), 30)
                .withMessage("Dynamic promoted results didn't load")
                .until(ExpectedConditions.invisibilityOfElementLocated(By.className("fa-spin")));
    }

    public WebElement promotedDocument(final String title) {
        return findElement(By.cssSelector("ul.promoted-documents-list")).findElement(By.xpath(".//a[contains(text(), '" + title + "')]/../.."));
    }

    public Removable removablePromotedDocument(final String title) {
        return new LabelBox(promotedDocument(title), getDriver());
    }

    public Editable staticPromotedDocumentTitle() {
        return new InlineEdit(findElement(By.className("static-promotion-title-edit")), getDriver());
    }

    public Editable staticPromotedDocumentContent() {
        return new InlineEdit(findElement(By.className("static-promotion-content-edit")), getDriver());
    }

    public Editable queryText() {
        return new InlineEdit(findElement(By.className("promotion-query-edit")), getDriver());
    }

    public void viewDocument(String title) {
        for(WebElement document : promotedList()){
            if(document.getText().equals(title)){
                document.click();
            }
        }
    }

    public PromotionsDetailTriggerForm getTriggerForm() {
        return new PromotionsDetailTriggerForm(findElement(By.className("promotion-match-terms-wrapper")), getDriver());
    }
}

@@ -189,68 +189,8 @@
 ts;%0A
-import com.hp.autonomy.frontend.selenium.element.ModalView;%0A
 impo
@@ -1203,544 +1203,8 @@
  %7D%0A%0A
-    public Dropdown editMenu() %7B%0A        return new Dropdown(findElement(By.className(%22extra-functions%22)), getDriver());%0A    %7D%0A%0A    @Deprecated%0A    public void delete() %7B%0A        System.err.println(%22PromotionDetailsPage.delete no longer works, see CSA-1619%22);%0A        final Dropdown editMenu = editMenu();%0A        editMenu.open();%0A        editMenu.getItem(%22Delete%22).click();%0A        final ModalView deleteModal = ModalView.getVisibleModalView(getDriver());%0A        deleteModal.findElement(By.cssSelector(%22.btn-danger%22)).click();%0A    %7D%0A%0A
     

Remove deprecated method PromotionsDetailPage.delete