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

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.WebDriverRunner;
import com.intland.codebeamer.functionaltest.page.BasePage;
import com.intland.codebeamer.functionaltest.page.common.ConfirmationDialogPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

/**
 * 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 ProjectListPage extends BasePage {

    public void tickProject(String name, boolean selected) {
        $(By.xpath(".//td[text()=\"" + name + "\"]/preceding-sibling::td[2]/input[@type='checkbox']")).setSelected(selected);
    }

    public void clickOnDeleteProjectsAndConfirm(int removableProjectCt) {
        $(By.xpath(".//input[@value='Delete Projects...']")).should(Condition.visible).click();
        ConfirmationDialogPage confirmationDialogPage = page(ConfirmationDialogPage.class);
        WebDriverRunner.getWebDriver().manage().timeouts().pageLoadTimeout(removableProjectCt * 20, TimeUnit.SECONDS);
        confirmationDialogPage.clickOnYes();
        $(By.linkText("Continue")).waitUntil(Condition.visible, removableProjectCt * 30000).click();
        WebDriverRunner.getWebDriver().manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    }

    public void clickOnDeleteProjectsAndConfirm() {
       clickOnDeleteProjectsAndConfirm(1);
    }

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

    public boolean tickAllProjects(String regex) {
        boolean wasProjectFound = false;
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher;
        for (SelenideElement project : getAllProjects()) {
            matcher = pattern.matcher(project.find(By.xpath(".//td[3]")).getText());
            if (matcher.matches() && !isRemovedProject(project)) {
                project.find(By.xpath(".//td[1]/input")).click();
                wasProjectFound = true;
            }
        }
        return wasProjectFound;
    }

    public int tickAllRemovedProjects(String regex) {
        int foundProjectCt = 0;
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = null;
        for (SelenideElement project : getAllProjects()) {
            matcher = pattern.matcher(project.find(By.xpath(".//td[3]")).getText());
            if (matcher.matches() && isRemovedProject(project)) {
                project.find(By.xpath(".//td[1]/input")).click();
                foundProjectCt++;
            }
        }
        return foundProjectCt;
    }

    private boolean isRemovedProject(SelenideElement project) {
        return project.getText().contains("Removed");
    }

    public String getProjectId(String name) {
        return $(By.xpath(".//td[text()='" + name + "']/preceding-sibling::td[1]")).getText();
    }
}
