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

import com.codeborne.selenide.ElementsCollection;
import com.intland.codebeamer.functionaltest.page.BasePage;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.support.ui.ExpectedConditions;

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 license agreement
 * you entered into with Intland.
 */
public class AddAssociationDialog extends BasePage {

    public enum AssociationType {
        DEPENDS("depends");

        private final String text;

        AssociationType(String s) {
            text = s;
        }

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

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

    public void selectAssociationType(AssociationType associationType) {
        $("div.sel").click();
        $(By.xpath(".//table[@class='assoc-table']//div[@class='assoc-label' and contains(text(), '" + associationType.text + "')]")).click();
    }

    public void clickOnAdd() {
        $(By.xpath(".//input[@type='submit' and @value='Add']")).click();
    }

    public void clickOnFromHistoryTab() {
        $("#historyTab-tab").click();
    }

    public void selectArtifactByLabelFromHistoryTab(String label, int index, boolean isSelected) {
        clickOnFromHistoryTab();//it's on it by default but better to make sure
        $(By.xpath("(.//table[@id='history_item']//td[.//following-sibling::td//label[contains(text(), '"+label +"')]][1])["+index+"]/input")).setSelected(isSelected);
        executeJavaScript("window.scrollTo(0, 0)");
    }

    public void clickOnFromSearchTab(){
        $("#searchTab-tab").click();
    }

    public void fillSearchFor(CharSequence text){
        $("#searchFilterPattern").sendKeys(text);
    }

    public void clickOnGo(){
        $("input[value=GO]").click();
    }


    public WorkItemsTab clickOnWorkItemsTab(){
        $("#search-issues-tab").click();
        return new WorkItemsTab();
    }

    @Override
    public boolean isPageLoaded() throws UnsupportedOperationException {
        try {
            Wait().withTimeout(5, TimeUnit.SECONDS)
                    .ignoring(NoSuchElementException.class)
                    .until(ExpectedConditions.visibilityOf($(By.xpath("//h3[text()='Association Settings']"))));
        }catch(Exception e){
            return false;
        }
        return true;
    }

    abstract static class Tab {

    }

    public static class WorkItemsTab extends Tab {

        public void selectArtifactByIndexFromWorkItemsTab(int index, boolean isSelected) {
            $(By.xpath("(.//table[@id='searchTrackerItem']/tbody//tr/td[1]/input[@type='checkbox'])["+index+"]")).setSelected(isSelected);
            executeJavaScript("window.scrollTo(0, 0)");
        }

        public ElementsCollection getAllWorkItems(){
            return $$(By.xpath("//table[@id='searchTrackerItem']/tbody//tr"));
        }
    }

}
