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

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.ex.UIAssertionError;
import com.intland.codebeamer.functionaltest.page.BasePage;
import com.intland.codebeamer.functionaltest.page.common.IsPageLoaded;
import com.intland.codebeamer.functionaltest.page.common.MoreMenuHelper;
import com.intland.codebeamer.functionaltest.page.trackers.documentview.DocumentViewPage;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.concurrent.TimeUnit;

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

public class TrackerItemListPage extends BasePage {

    public boolean isPageLoaded() throws UnsupportedOperationException {
        try {
            Wait().withTimeout(5, TimeUnit.SECONDS)
                    .ignoring(NoSuchElementException.class)
                    .until(ExpectedConditions.visibilityOf($(".viewsMenu")));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public SelenideElement moreMenuLink() {
        return $(By.xpath(".//a[text()='more']/following-sibling::img"));
    }


    public enum ViewMenuIcon {
        TEST_COVERAGE("Test Coverage", TestCoveragePage.class),
        TRACEABILITY_BROWSER("Traceability Browser", TraceabilityBrowserPage.class),
        KANBAN_BOARD("Kanban Board", KanbanBoardPage.class),
        TABLE_VIEW("Table view", TableViewPage.class),
        PLAN_VIEW("Plan", PlanViewPage.class),
        DOCUMENT_VIEW("Document view", DocumentViewPage.class),
        DASHBOARD("Dashboard", DashboardViewPage.class);

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

        ViewMenuIcon(String s, Class<?> pageObjectClass) {
            text = s;
            this.pageObjectClass = pageObjectClass;
        }

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

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

    public BasePage clickOnViewMenuIcon(ViewMenuIcon viewMenuIcon) {
        $(By.xpath("//a[@title='" + viewMenuIcon.text + "']")).should(Condition.exist).click();
        return page((Class<BasePage>) viewMenuIcon.pageObjectClass);
    }

    public IsPageLoaded clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem moreMenuItem) {
       return MoreMenuHelper.clickOnMoreMenuItem(moreMenuItem);
    }

    public void checkIfMoreMenuItem(MoreMenuHelper.MoreMenuItem moreMenuItem, Condition condition) {
        MoreMenuHelper.checkIfMoreMenuItem(moreMenuItem, condition);
    }

    public void checkIfView(ViewMenuIcon viewMenuIcon, Condition hidden) {
        $(By.xpath("//a[@title='" + viewMenuIcon.text + "']")).shouldBe(hidden);
    }

    public TableView tableView = new TableView();

    //TODO refactor
    public void clickOnTableView() {
        $(By.className("actionMenuBar")).$(byTitle("Table view")).click();
    }

    public class TableView {

        public CreateEditTrackerItemPage clickOnNewItem() {
            $(By.linkText("New Item")).click();
            return page(CreateEditTrackerItemPage.class);
        }
    }

}
