package com.intland.codebeamer.functionaltest.page;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.WebDriverRunner;
import com.intland.codebeamer.functionaltest.page.wiki.ProjectWikiPage;
import org.openqa.selenium.By;

import java.io.File;
import java.util.concurrent.TimeUnit;

import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selectors.byTitle;
import static com.codeborne.selenide.Selectors.byValue;
import static com.codeborne.selenide.Selenide.*;

public class CreateProjectPage extends BasePage {

    public enum ProjectTypeOptions {
        NEW_PROJECT("#newProject"),
        DEMO_PROJECT("#demoProject"),
        JIRA_PROJECT("#jiraImport");

        private final String selector;

        ProjectTypeOptions(String s) {
            selector = s;
        }

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

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

    public enum SmartTemplateOptions {
        NONE("--"),
        AGILE_SCRUM("Agile-Scrum"),
        AGILE_WATERFALL_HYBRID("Agile-Waterfall-Hybrid"),
        AUTOMOTIVE("Automotive"),
        AVIONICS("Avionics"),
        MEDICAL("Medical");

        private final String text;

        SmartTemplateOptions(String s) {
            text = s;
        }

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

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


    public void uploadProject(String file) {
        $(byTitle("You can also drag and drop files here")).uploadFile(new File(file));
        sleep(500);
        $(By.className("qq-upload-spinner")).waitUntil(Condition.hidden, 50000);
    }

    public void clickOnNext() {
        $(byValue("Next")).click();
    }

    public void waitForSecondScreen() {
        $(By.id("name")).shouldBe(visible);
    }

    public ProjectWikiPage clickOnFinish() {
        WebDriverRunner.getWebDriver().manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
        $("#finishButton").click();
        $(By.linkText("Wiki")).waitUntil(visible, 120000);
        WebDriverRunner.getWebDriver().manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
        return page(ProjectWikiPage.class);
    }

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

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

    public void setCategory(String category) {
        $(By.id("categoryOther")).setValue(category);
    }

    public void tickIncludingEmpty(boolean isSelected){
        $("#emptyInputToggle").setSelected(isSelected);
    }

    public void tickProjectType(ProjectTypeOptions projectType) {
        $(projectType.selector).click();
    }

    public void selectSmartTemplate(SmartTemplateOptions smartTemplateOption) {
        $("#demoDataPack").selectOption(smartTemplateOption.text);
    }

    public void selectProjectTemplate(String text) {
        $(By.id("templateProjectSelector")).selectOption(text);
    }

    public ElementsCollection errorMessages() {
        return $$("#globalMessages li");
    }

    public ElementsCollection getHiddenCollapsedImportConfigRows(){
        return $$(By.xpath(".//table[@id='importConfigTable']//tr[contains(@style,'none') and contains(@class, 'collapsed')]"));
    }

    public void checkIfNext(Condition condition) {
        $(byValue("Next")).shouldBe(condition);
    }
}
