package com.intland.codebeamer.functionaltest.test;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.testng.ScreenShooter;

import com.intland.codebeamer.functionaltest.helper.Helper;
import com.intland.codebeamer.functionaltest.page.*;
import com.intland.codebeamer.functionaltest.page.mystart.MyStartPage;
import com.intland.codebeamer.functionaltest.page.projects.ProjectsPage;
import com.intland.codebeamer.functionaltest.page.systemadmin.ProjectListPage;
import com.intland.codebeamer.functionaltest.page.systemadmin.SystemAdminPage;
import com.intland.codebeamer.functionaltest.page.wiki.ProjectWikiPage;
import org.testng.annotations.*;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

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

@Listeners({ScreenShooter.class})
public class BaseTest {

    public static final String HOST_URL = System.getProperty("codeBeamerUrl");

    public static final String projectName = "test-selenium";
	
    @BeforeClass(alwaysRun = true)
    public void startServer() throws Exception {
        Configuration.baseUrl = System.getProperty("codeBeamerUrl");
        Configuration.reportsFolder = "test-result/reports";
        Configuration.timeout = 10000;
    }

    @BeforeMethod(alwaysRun = true)
    public void handleTestMethodName(Method method) {
        String currentTestName = method.getName();
        String fullMethodName = method.getDeclaringClass().getName().replaceFirst("com.intland.codebeamer.functionaltest.", "") + "." + currentTestName;
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        System.out.println("Running test: <" + fullMethodName + "> at " + dateFormat.format(new Date()));
    }

    public ProjectWikiPage createProjectFromArchive(String archiveName, String projectName, String projectKey) {
        MyStartPage myStartPage = Helper.loginWithSystemAdmin();
        // Create a project
        ProjectsPage projectsPage = myStartPage.clickOnProjects();
        CreateProjectPage createProjectPage = projectsPage.clickOnNewProject();
        createProjectPage.uploadProject(archiveName);
        createProjectPage.clickOnNext();
        createProjectPage.setName(projectName);
        createProjectPage.setKey(projectKey);
        createProjectPage.waitForSecondScreen();
        createProjectPage.clickOnNext();
        ProjectTemplatePage projectTemplatePage = page(ProjectTemplatePage.class);
        if (projectTemplatePage.isProjectAlreadyExists()){
            deleteProject(projectName);
            createProjectFromArchive(archiveName, projectName, projectKey);
            return page(ProjectWikiPage.class);
        }
        createProjectPage.clickOnFinish();
        return page(ProjectWikiPage.class);
    }

    public void deleteProject(String name) {
        MyStartPage myStartPage = Helper.loginWithSystemAdmin();
        SystemAdminPage systemAdminPage = myStartPage.clickOnSystemAdmin();
        ProjectListPage projectListPage = systemAdminPage.clickOnProjectList();
        projectListPage.tickProject(name, true);
        projectListPage.clickOnDeleteProjectsAndConfirm();
        projectListPage.tickProject(name, true);
        projectListPage.clickOnDeleteProjectsAndConfirm();
    }



    public void deleteAllProjects(String regex) {
        MyStartPage myStartPage = Helper.loginWithSystemAdmin();
        SystemAdminPage systemAdminPage = myStartPage.clickOnSystemAdmin();
        ProjectListPage projectListPage = systemAdminPage.clickOnProjectList();
        if (!projectListPage.tickAllProjects(regex)){
            return;
        }
        projectListPage.clickOnDeleteProjectsAndConfirm();
        int removableProjectCt = projectListPage.tickAllRemovedProjects(regex);
        if (removableProjectCt == 0) {
            return;
        }
        projectListPage.clickOnDeleteProjectsAndConfirm(removableProjectCt);
    }
}
