package com.intland.codebeamer.functionaltest.helper;

import com.codeborne.selenide.Configuration;
import com.intland.codebeamer.functionaltest.page.LoginPage;
import org.openqa.selenium.By;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

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

@Test
public class Cleanup {

    @BeforeClass
    public static void startServer() throws Exception {
        Configuration.baseUrl = "http://172.20.41.16:8200/cb";
        Configuration.reportsFolder = "test-result/reports";
    }

    @Test
    public void deleteComments() {
        getWebDriver().manage().window().maximize();
        LoginPage loginPage = open("/login.spr", LoginPage.class);
        loginPage.setUsername("bond");
        loginPage.setPassword("007");
        loginPage.clickOnLogin();
        for (int i = 0; i < 300; i++) {
            System.out.println("i: " + i);
            open("/item/1028");
            $(By.id("task-details-attachments-tab")).waitUntil(visible, 15000);
            $(By.id("task-details-attachments-tab")).click();
            $(By.className("even")).hover();
            $(By.className("even")).hover().$(".menuArrowDown").click();
            $(By.partialLinkText("Delete")).click();
            confirm();
        }
    }

    @Test
    public void deleteWikiPages() {
        getWebDriver().manage().window().maximize();
        LoginPage loginPage = open("/login.spr", LoginPage.class);
        loginPage.setUsername("bond");
        loginPage.setPassword("007");
        loginPage.clickOnLogin();
        for (int i = 0; i < 1000; i++) {
            System.out.println("i: " + i);
            open("/wiki/1032");
            $(By.className("jstree-open")).waitUntil(visible, 10000);
//            sleep(1000);
            $(By.className("jstree-anchor")).shouldBe(visible);
            $$(By.className("jstree-anchor")).get(9).click();
            $(byText("Tags:")).waitUntil(visible, 10000);
            sleep(1000);
            $(By.linkText("more")).click();
            $(By.linkText("Delete...")).click();
            $(byText("Yes")).click();
//            sleep(1000);
        }
    }

    @Test
    public void deleteBaselines() {
        getWebDriver().manage().window().maximize();
        LoginPage loginPage = open("/login.spr", LoginPage.class);
        loginPage.setUsername("bond");
        loginPage.setPassword("007");
        loginPage.clickOnLogin();
        open("/project/2");
        $(By.linkText("Baselines")).click();
        for (int i = 0; i < 50; i++) {
            System.out.println("i: " + i);
            $(By.className("deleteLink")).click();
            $(byText("Yes")).click();
            sleep(1000);
        }
    }
}
