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

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.SelenideElement;
import com.intland.codebeamer.functionaltest.helper.Helper;
import com.intland.codebeamer.functionaltest.page.BasePage;
import org.openqa.selenium.By;
import org.openqa.selenium.interactions.Actions;

import static com.codeborne.selenide.Condition.*;
import static com.codeborne.selenide.Selectors.byText;
import static com.codeborne.selenide.Selenide.*;
import static com.codeborne.selenide.WebDriverRunner.getWebDriver;

public class TrackerConfigurationPage extends BasePage {

    public GeneralTab generalTab = new GeneralTab();
    public StateTransitionsTab stateTransitionsTab = new StateTransitionsTab();
    public FieldsTab fieldsTab = new FieldsTab();

    public void clickSave() {
    	$(By.name("SAVE")).click();
    	sleep(1000);
    }
    
    public TrackerItemListPage clickOnTrackerInBreadCrumbBar() {
        $(By.className("breadcrumbs-summary")).$(By.tagName("a")).click();
        return page(TrackerItemListPage.class);
    }

    public void clickOnGeneralTab() {
        $(By.id("tracker-customize-general-tab")).click();
    }

    public void clickOnStateTransitionsTab() {
        $(By.id("tracker-customize-transitions-tab")).should(exist).click();
    }

    public void clickOnFieldsTab() {
        $(By.id("tracker-customize-field-properties-tab")).click();
    }

    public class GeneralTab {

        public void clickOnDelete() {
            $(By.name("DELETE")).click();
        }
        
        public void clickVisibleCheckBox() {
        	$(By.id("visibility")).shouldBe(visible).click();
        }
        
        private SelenideElement thisTab = $(By.id("trackerEditorForm"));

        public TrackersPage confirmDelete() {
            $(By.className("ui-dialog-buttonset")).$(byText("Yes")).click();
            $(By.linkText("Releases")).shouldBe(Condition.visible);
            return page(TrackersPage.class);
        }

        public boolean isAvailableAsTemplate() {
            return $(By.id("templateCheckbox")).has(Condition.checked);
        }

        public String getName() {
            return $(By.name("name")).val();
        }

        public String getKey() {
            return $(By.name("keyName")).val();
        }

        public String getDescription() {
            return $(By.id("editor")).val();
        }

        public boolean isUsingWorkflow() {
            return $(By.id("usingWorkflow")).has(Condition.checked);
        }

        public boolean isVisible() {
            return $(By.id("visibility")).has(Condition.checked);
        }

        public void checkIfHasTemplateTracker(String trackerName) {
            thisTab.$$(By.xpath(".//div[@class='information']")).shouldHave(Helper.matchText("This Tracker inherits from " + trackerName +".*"));
        }

        public void checkIfHasNoTemplateTracker() {
            thisTab.$(By.xpath(".//div[@class='information']")).shouldBe(hidden);
        }

        public void checkIfConfigInherited(boolean isInherited) {
            if (isInherited)
                $(By.xpath(".//form[@id='trackerEditorForm']//div[@class='information']")).shouldBe(Condition.visible).has(text("This Tracker inherits from").because("Config should be inherited"));
            else
                $(By.xpath(".//form[@id='trackerEditorForm']//div[@class='information']")).shouldNotBe(Condition.and("", Condition.visible, text("This Tracker inherits from").because("Config should not be inherited")));
        }

    }

    public class StateTransitionsTab {

        public void openContextMenuOfTransitionAndClickOnMenuItem(String transitionName, String menuItemName) {
            Actions builder = new Actions(getWebDriver());
            builder.moveToElement($(By.linkText(transitionName)).closest("tr")).build().perform();
            $(By.linkText(transitionName)).closest("tr").$(By.className("menuArrowDown")).click();
            $$(By.className("context-menu-root")).filterBy(Condition.visible).get(0).$$(By.tagName("span")).filterBy(Condition.exactText(menuItemName)).get(0).click();
        }

        public void addActionUpdateItemPropertiesSetChoiceFieldToValue(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("select")).selectOption(fieldValue);
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesClearChoiceField(String fieldName) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Clear");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesSetIntegerFieldToValue(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("input")).setValue(fieldValue);
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesClearIntegerField(String fieldName) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Clear");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesIncreaseIntegerFieldByValue(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Increase by");
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("input")).setValue(fieldValue);
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesDecreaseIntegerFieldByValue(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Decrease by");
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("input")).setValue(fieldValue);
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesSetTextFieldToValue(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("input")).setValue(fieldValue);
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesClearTextField(String fieldName) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Clear");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesAppendValueToTextField(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Append");
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("input")).setValue(fieldValue);
            $(By.name("separator")).waitUntil(Condition.visible, 10000).setValue(" ");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesPrependValueToTextField(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Prepend");
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("input")).setValue(fieldValue);
            $(By.name("separator")).waitUntil(Condition.visible, 10000).setValue(" ");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesSetDateFieldToValue(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("select")).selectOption(fieldValue);
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesClearDateField(String fieldName) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Clear");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesSetWikiFieldToValue(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("textarea")).setValue(fieldValue);
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesClearWikiField(String fieldName) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Clear");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesAppendValueToWikiField(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Append");
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("textarea")).setValue(fieldValue);
            $(By.name("separator")).waitUntil(Condition.visible, 10000).setValue(" ");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesPrependValueToWikiField(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Prepend");
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("textarea")).setValue(fieldValue);
            $(By.name("separator")).waitUntil(Condition.visible, 10000).setValue(" ");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesSetMemberFieldToValue(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            Selenide.sleep(2000);
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input")).click();
            Selenide.sleep(2000);
            SelenideElement input = $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input"));
            Selenide.sleep(2000);
            boolean success = false;
            for (int j = 0; j < 5; j++) {
                if (!success) {
                    try {
                        input.clear();
                        input.click();
                        input.setValue(fieldValue);
                        sleep(1000);
                        ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                        if (collection.get(0).$$(By.xpath(".//td[contains(.,'"+fieldValue+"')]")).size() > 0) {
                            collection.get(0).$(By.xpath(".//td[contains(.,'"+fieldValue+"')]")).click();
                            success = true;
                            break;
                        }
                    } catch (Throwable t) {
                    }
                }
            }
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesClearMemberField(String fieldName) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Clear");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesAddValueToMemberField(String fieldName, String fieldValueToAdd) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Add");
            Selenide.sleep(2000);
            SelenideElement input = $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input"));
            boolean success = false;
            for (int j = 0; j < 5; j++) {
                if (!success) {
                    try {
                        input.clear();
                        input.click();
                        input.sendKeys(fieldValueToAdd);
                        sleep(1000);
                        ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                        if (collection.get(0).$$(By.xpath(".//td[contains(.,'"+fieldValueToAdd+"')]")).size() > 0) {
                            collection.get(0).$(By.xpath(".//td[contains(.,'"+fieldValueToAdd+"')]")).click();
                            success = true;
                            break;
                        }
                    } catch (Throwable t) {
                    }
                }
            }

            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesRemoveValueFromMemberField(String fieldName, String fieldValueToRemove) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Remove");
            SelenideElement input = $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input"));
            boolean success = false;
            for (int j = 0; j < 5; j++) {
                if (!success) {
                    try {
                        input.clear();
                        input.click();
                        input.setValue(fieldValueToRemove);
                        sleep(1000);
                        ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                        if (collection.get(0).$$(By.xpath(".//td[contains(.,'"+fieldValueToRemove+"')]")).size() > 0) {
                            collection.get(0).$(By.xpath(".//td[contains(.,'"+fieldValueToRemove+"')]")).click();
                            success = true;
                            break;
                        }
                    } catch (Throwable t) {
                    }
                }
            }
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesRetainValueInMemberField(String fieldName, String fieldValueToRetain) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Retain");
            SelenideElement input = $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input"));
            boolean success = false;
            for (int j = 0; j < 5; j++) {
                if (!success) {
                    try {
                        input.clear();
                        input.click();
                        input.sendKeys(fieldValueToRetain);
                        sleep(1000);
                        ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                        if (collection.get(0).$$(By.xpath(".//td[contains(.,'"+fieldValueToRetain+"')]")).size() > 0) {
                            collection.get(0).$(By.xpath(".//td[contains(.,'"+fieldValueToRetain+"')]")).click();
                            success = true;
                            break;
                        }
                    } catch (Throwable t) {
                    }
                }
            }

            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesSetWorkItemFieldToValue(String fieldName, String fieldValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            Selenide.sleep(2000);
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input")).click();
            Selenide.sleep(2000);
            SelenideElement input = $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input"));
            Selenide.sleep(2000);
            boolean success = false;
            for (int j = 0; j < 5; j++) {
                if (!success) {
                    try {
                        input.clear();
                        input.click();
                        input.setValue(fieldValue);
                        sleep(2000);
                        ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                        if (collection.get(0).$$(By.xpath(".//td[contains(.,'"+fieldValue+"')]")).size() > 0) {
                            collection.get(0).$(By.xpath(".//td[contains(.,'"+fieldValue+"')]")).click();
                            success = true;
                            break;
                        }
                    } catch (Throwable t) {
                    }
                }else break;
            }

            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesClearWorkItemField(String fieldName) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Clear");
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesAddValueToWorkItemField(String fieldName, String fieldValueToAdd) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Add");
            Selenide.sleep(2000);
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input")).click();
            Selenide.sleep(1000);
            SelenideElement input = $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input"));
            Selenide.sleep(2000);
            boolean success = false;
            for (int j = 0; j < 5; j++) {
                if (!success) {
                    try {
                        input.clear();
                        input.click();
                        input.setValue(fieldValueToAdd);
                        sleep(2000);
                        ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                        if (collection.get(0).$$(By.xpath(".//td[contains(.,'"+fieldValueToAdd+"')]")).size() > 0) {
                            collection.get(0).$(By.xpath(".//td[contains(.,'"+fieldValueToAdd+"')]")).click();
                            success = true;
                            break;
                        }
                    } catch (Throwable t) {
                    }
                }else break;
            }
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesRemoveValueFromWorkItemField(String fieldName, String fieldValueToRemove) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Remove");
            SelenideElement input = $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input"));
            boolean success = false;
            for (int j = 0; j < 5; j++) {
                if (!success) {
                    try {
                        input.clear();
                        input.click();
                        input.setValue(fieldValueToRemove);
                        sleep(2000);
                        ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                        if (collection.get(0).$$(By.xpath(".//td[contains(.,'"+fieldValueToRemove+"')]")).size() > 0) {
                            collection.get(0).$(By.xpath(".//td[contains(.,'"+fieldValueToRemove+"')]")).click();
                            success = true;
                            break;
                        }
                    } catch (Throwable t) {
                    }
                }else break;
            }

            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionUpdateItemPropertiesRetainValueInWorkItemField(String fieldName, String fieldValueToRetain) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Update item properties");
            $(By.className("fieldUpdateSelector")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("updateOpSelector")).waitUntil(Condition.visible, 10000).selectOption("Retain");

            SelenideElement input = $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.className("token-input-list-facebook")).$(By.tagName("input"));
            boolean success = false;
            for (int j = 0; j < 5; j++) {
                if (!success) {
                    try {
                        input.clear();
                        input.click();
                        input.sendKeys(fieldValueToRetain);
                        sleep(2000);
                        ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                        if (collection.get(0).$$(By.xpath(".//td[contains(.,'"+fieldValueToRetain+"')]")).size() > 0) {
                            collection.get(0).$(By.xpath(".//td[contains(.,'"+fieldValueToRetain+"')]")).click();
                            success = true;
                            break;
                        }
                    } catch (Throwable t) {
                    }
                }else break;
            }

            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("fieldUpdates")).filter(Condition.visible).get(0).$$(By.tagName("button")).get(1).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionCreateNewSequentialNumberSetPrefixToValue(String fieldName, String prefixValue) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Create a new sequential id/number");
            $(By.className("workflowActionPopup")).waitUntil(Condition.visible, 10000);
            $$(By.name("value")).filterBy(Condition.visible).get(0).selectOption(fieldName);
            $(By.className("valueTypeSelector")).waitUntil(Condition.visible, 10000).selectOption("Value");
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("input")).waitUntil(Condition.visible, 10000).setValue(prefixValue);
            $$(By.className("fieldUpdate")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionCreateNewSequentialNumberSetPrefixToFieldValue(String fieldName, String prefixFieldName) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Create a new sequential id/number");
            $(By.name("value")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("valueTypeSelector")).waitUntil(Condition.visible, 10000).selectOption("Copy from");
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.tagName("select")).waitUntil(Condition.visible, 10000).selectOption(prefixFieldName);
            $$(By.className("fieldUpdate")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void addActionCreateNewSequentialNumberSetPrefixToComputedValue(String fieldName, String computeExpression) {
            $(byText("Actions:")).closest("tr").$(By.tagName("select")).selectOption("Create a new sequential id/number");
            $(By.name("value")).waitUntil(Condition.visible, 10000).selectOption(fieldName);
            $(By.className("valueTypeSelector")).waitUntil(Condition.visible, 10000).selectOption("Compute as");
            $(By.className("valueEditor")).waitUntil(Condition.visible, 10000).$(By.name("computeAs")).waitUntil(Condition.visible, 10000).setValue(computeExpression);
            $$(By.className("fieldUpdate")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("adminTransitionPermissionForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

    }

    public class FieldsTab {

        public void createCustomChoiceField(String fieldName, String[] options) {
            $(By.id("trackerLayoutForm")).$(By.tagName("select")).selectOption("New choice field...");
            $$(By.name("label")).filter(Condition.visible).get(0).setValue(fieldName);
            $$(By.className("ui-dialog-buttonset")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $(By.id("trackerLayoutForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
            page(TrackerConfigurationPage.class).waitForPageToLoad();
            $(By.id("trackerFields")).$$(By.tagName("tr")).last().$(By.linkText("Options")).click();
            for (String optionName : options) {
                $(By.linkText("Add Option...")).shouldBe(Condition.visible).click();
                $(By.className("choiceOptionForm")).$(By.name("name")).setValue(optionName);
                $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(1).$(byText("OK")).click();
            }
            $$(By.className("ui-dialog-buttonset")).filterBy(Condition.visible).get(0).$(byText("OK")).click();
            $(By.id("trackerLayoutForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void createCustomIntegerField(String fieldName) {
            $(By.id("trackerLayoutForm")).$(By.tagName("select")).selectOption("New custom field...");
            $$(By.name("label")).filter(Condition.visible).get(0).setValue(fieldName);
            $$(By.name("type")).filter(Condition.visible).get(0).selectOption("Integer");
            $$(By.className("ui-dialog-buttonset")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $(By.id("trackerLayoutForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void createCustomTextField(String fieldName) {
            $(By.id("trackerLayoutForm")).$(By.tagName("select")).selectOption("New custom field...");
            $$(By.name("label")).filter(Condition.visible).get(0).setValue(fieldName);
            $$(By.name("type")).filter(Condition.visible).get(0).selectOption("Text");
            $$(By.className("ui-dialog-buttonset")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $(By.id("trackerLayoutForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void createCustomDateField(String fieldName) {
            $(By.id("trackerLayoutForm")).$(By.tagName("select")).selectOption("New custom field...");
            $$(By.name("label")).filter(Condition.visible).get(0).setValue(fieldName);
            $$(By.name("type")).filter(Condition.visible).get(0).selectOption("Date");
            $$(By.className("ui-dialog-buttonset")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $(By.id("trackerLayoutForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void createCustomWikiField(String fieldName) {
            $(By.id("trackerLayoutForm")).$(By.tagName("select")).selectOption("New custom field...");
            $$(By.name("label")).filter(Condition.visible).get(0).setValue(fieldName);
            $$(By.name("type")).filter(Condition.visible).get(0).selectOption("Wikitext");
            $$(By.className("ui-dialog-buttonset")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $(By.id("trackerLayoutForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

        public void createCustomMembersField(String fieldName) {
            $(By.id("trackerLayoutForm")).$(By.tagName("select")).selectOption("New choice field...");
            $$(By.name("label")).filter(Condition.visible).get(0).setValue(fieldName);
            $$(By.name("type")).filter(Condition.visible).get(0).selectOption("Members");
            $$(By.className("ui-dialog-buttonset")).filter(Condition.visible).get(0).$(By.tagName("button")).click();
            $(By.id("trackerLayoutForm")).$(By.className("actionBar")).$(By.name("SAVE")).click();
        }

    }

}
