package com.intland.codebeamer.functionaltest.page;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.WebDriverRunner;
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.$;
import static com.codeborne.selenide.Selenide.page;
import static com.codeborne.selenide.Selenide.sleep;

public class ScmProjectSettingsPage extends BasePage {
    public void clickOnConfigure() {
        $(byValue("Configure >")).click();
    }

    public RepositoryManagementPage clickOnNext() {
        $(byValue("Next")).click();
        return page(RepositoryManagementPage.class);
    }

    public void clickOnCancel() {
        $(".cancelButton").click();
    }

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

    public RepositoryPage clickOnFinish() {
        WebDriverRunner.getWebDriver().manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
        $(By.id("finishButton")).click();
        $(By.id("files-tab")).waitUntil(visible, 60000);
        WebDriverRunner.getWebDriver().manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
        return page(RepositoryPage.class);
    }

    public void selectGit() {
        $(byValue("git")).click();
    }

    public void setRepositoryName(String repositoryName) {
        $(By.id("repositoryName")).clear();
        $(By.id("repositoryName")).setValue(repositoryName);
    }

    public void selectSvn() {
        $(byValue("svn")).click();
    }

    public void selectMercurial() {
        $(byValue("hg")).click();
    }
}
