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

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.ElementsCollection;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.$$;
import static com.codeborne.selenide.Selenide.page;

public class NewTrackerPage {

    public void selectTrackerType(String trackerType) {
        $(By.id("availableTypes")).selectOption(trackerType);
    }

    public void setName(String name) {
        $(By.name("name")).setValue(name);
    }

    public void setKey(String key) {
        $(By.name("keyName")).setValue(key);
    }

    public void setDescription(String description) {
        $(By.id("editor")).setValue(description);
    }

    public TrackerConfigurationPage clickOnSave() {
        $(By.name("SAVE")).click();
        return page(TrackerConfigurationPage.class);
    }

    public TrackersPage clickOnCancel(){
        $(".cancelButton").click();
        return page(TrackersPage.class);
    }

    public void setAvailableAsTemplate(boolean availableAsTemplate) {
        $(By.id("templateCheckbox")).setSelected(availableAsTemplate);
    }

    public void setUsingWorkflow(boolean usingWorkflow) {
        $(By.id("usingWorkflow")).setSelected(usingWorkflow);
    }

    public void selectTemplate(String text) {
        $(By.id("templateTrackerId")).selectOption($$(By.xpath(".//select[@id='templateTrackerId']//option")).filter(text(text)).get(0).getText());
    }

    public void setConfigInherit(boolean usingWorkflow) {
        $(By.id("inheritanceConfig")).setSelected(usingWorkflow);
    }

    public void setVisibility(boolean visibility) {
        $(By.id("visibility")).setSelected(visibility);
    }

    public void clickOnMarkupTab() {
        $(By.id("wiki-markup-tab")).click();
    }

    public ElementsCollection getAllAvailableTypes(){
        return $$(By.xpath(".//select[@id='availableTypes']//option"));
    }

    public void checkIfSave(Condition condition) {
        $(By.name("SAVE")).shouldBe(condition);
    }
}
