package com.intland.codebeamer.functionaltest.page.common;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.ex.UIAssertionError;
import com.intland.codebeamer.functionaltest.page.trackers.*;
import com.intland.codebeamer.functionaltest.page.trackers.documentview.DocumentViewPage;
import com.intland.codebeamer.functionaltest.page.trackers.reviewdialog.PreviewSelectionStepPage;
import com.intland.codebeamer.functionaltest.page.wiki.PasteDialog;
import com.intland.codebeamer.functionaltest.page.wiki.WikiPropertiesPage;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

import java.util.concurrent.TimeUnit;

import static com.codeborne.selenide.Selenide.*;

/**
 * Copyright by Intland Software
 * <p>
 * All rights reserved.
 * <p>
 * This software is the confidential and proprietary information
 * of Intland Software. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the firstSixProductLicense agreement
 * you entered into with Intland.
 */
public class MoreMenuHelper {

    private static SelenideElement moreMenuLink = $(By.xpath(".//a[text()='more']/following-sibling::img"));

    public enum MoreMenuItem {
        CUSTOMIZE("Customize", TrackersCustomizationPage.class, false),
        IMPORT("Import", ImportingItemsPage.class, false),
        EXPORT_TO_OFFICE("Export to Office", ExportToOfficeDialogPage.class, true),
        EDIT_VIA_OFFICE("Edit via Office", null, false),
        EXPORT_SELECTION("Export selection (and their children) to Office", ExportToOfficeDialogPage.class, true),
        EXPORT_MULTIPLE_PAGES_TO_WORD("Export Multiple Pages to Word", ExportToOfficeDialogPage.class, true),
        TEST_COVERAGE("Test Coverage", TestCoveragePage.class, false),
        TRACEABILITY_BROWSER("Traceability Browser", TraceabilityBrowserPage.class, false),
        RISK_MATRIX_DIAGRAM("Risk Matrix Diagram", RiskMatrixDiagramPage.class, false),
        WORKFLOW_DIAGRAM("Workflow Diagram", WorkflowDiagramPage.class, false),
        NEW_VIEW("New View", NewViewPage.class, false),
        SET_AS_DEFAULT("Set as default", null, false),
        CLEAR_DEFAULT("Clear default", null, false),
        DUPLICATE("Duplicate this View", EditViewPage.class, false),
        REFERENCE_VIEW_SETTINGS("Reference View Settings", TrackerItemReferenceConfigurationDialogPage.class, true),
        NEW_BASELINE("New Baseline", NewBaselineDialogPage.class, false),
        SEND_TO_REVIEW("Send to Review", PreviewSelectionStepPage.class, true),
        SEND_SELECTION_TO_REVIEW("Send selection to Review", SendSelectionChooserDialog.class, false),
        LOCK("Lock", DocumentViewPage.class, false),
        UNLOCK("Unlock", DocumentViewPage.class, false),
        FOLLOW("Follow", DocumentViewPage.class, false),
        UNFOLLOW("Unfollow", DocumentViewPage.class, false),
        ADD_TAG("Tags", AddTagDialog.class, true),

        PROPERTIES("Properties", WikiPropertiesPage.class, false),
        DELETE("Delete...", ConfirmationDialogPage.class, true),
        COPY("Copy", null, false),
        PASTE("Paste", PasteDialog.class, false);


        public final String text;
        public final Class<?> pageObjectClass;
        private final boolean isInIframe;

        MoreMenuItem(String s, Class pageObjectClass, boolean isInIframe) {
            text = s;
            this.pageObjectClass = pageObjectClass;
            this.isInIframe = isInIframe;
        }

        public boolean equalsName(String otherSelector) {
            return (otherSelector == null) ? false : text.equals(otherSelector);
        }

        public boolean isInIframe() {
            return isInIframe;
        }

        public String toString() {
            return this.text;
        }
    }

    public static IsPageLoaded clickOnMoreMenuItem(MoreMenuItem moreMenuItem) {
        Wait().withTimeout(2, TimeUnit.MINUTES)
                .pollingEvery(1, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class)
                .ignoring(UIAssertionError.class)
                .until((WebDriver driver) -> {
                    moreMenuLink.click();
                    sleep(1000);
                    SelenideElement menu = $(By.xpath(".//div[contains(@class,'yuimenu') and contains(@style, 'display: block')]"));
                    //System.out.println(menu);
                    if (menu.isDisplayed()) {
                        menu.findElement(By.partialLinkText(moreMenuItem.text)).click();
                        return menu;
                    } else {
                        return null;
                    }
                });
        return moreMenuItem.pageObjectClass != null ? page((Class<IsPageLoaded>) moreMenuItem.pageObjectClass) : null;
    }

    public static void checkIfMoreMenuItem(MoreMenuItem moreMenuItem, Condition condition) {
        Wait().withTimeout(10, TimeUnit.SECONDS)
                .pollingEvery(1, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class)
                .ignoring(UIAssertionError.class)
                .until((WebDriver driver) -> {
                    moreMenuLink.click();
                    sleep(1000);
                    SelenideElement menu = $(By.xpath(".//div[contains(@class,'yuimenu') and contains(@style, 'display: block')]"));
                    if (menu.isDisplayed()) return menu;
                    else {
                        return null;
                    }
                }).$(By.linkText(moreMenuItem.text)).shouldBe(condition);
    }
}