package com.intland.codebeamer.functionaltest.generator;

import com.codeborne.selenide.Configuration;
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.SystemAdminPage;
import com.intland.codebeamer.functionaltest.page.systemadmin.UsersPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackersPage;
import com.intland.codebeamer.functionaltest.page.wiki.ProjectWikiPage;
import com.intland.codebeamer.functionaltest.test.BaseTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

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

public class EnvironmentGenerator extends BaseTest {

    @Test
    public void generateSeleniumEnvironment() {
        MyStartPage myStartPage = Helper.loginWithUser("bond", "007");

        // Enable Service Desk
        SystemAdminPage systemAdminPage = myStartPage.clickOnSystemAdmin();
        ServiceDeskPage serviceDeskPage = systemAdminPage.clickOnServiceDesk();
        serviceDeskPage.enableServiceDesk();
        serviceDeskPage.clickOnSave();

        // Create users
        UsersPage usersPage = systemAdminPage.clickOnUserAccounts();
        createUser("test-user-sel-1109411", "1109411", "testuser@testuser.ttt", usersPage);
        createUser("support", "support", "support@support.huuu", usersPage);

        // Create a project
        ProjectsPage projectsPage = systemAdminPage.clickOnProjects();
        CreateProjectPage createProjectPage = projectsPage.clickOnNewProject();
        createProjectPage.uploadProject("test-project-selenium.zip");
        createProjectPage.clickOnNext();
        createProjectPage.waitForSecondScreen();
        createProjectPage.clickOnNext();
        ProjectWikiPage projectWikiPage = createProjectPage.clickOnFinish();

        // Date
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-dd-hh-mm-ss");

        // Create GIT repo
        ScmRepositoriesPage scmRepositoriesPage = projectWikiPage.clickOnScmRepositories();
        ScmProjectSettingsPage scmProjectSettingsPage = scmRepositoriesPage.clickOnNewRepository();
        scmProjectSettingsPage.selectGit();
        scmProjectSettingsPage.clickOnConfigure();
        scmProjectSettingsPage.clickOnNext();
        scmProjectSettingsPage.setRepositoryName("git-" + simpleDateFormat.format(new Date()));
        scmProjectSettingsPage.uploadScm("gittestrepo.git");
        RepositoryPage repositoryPage = scmProjectSettingsPage.clickOnFinish();
        repositoryPage.waitForFinish();
        repositoryPage.clickOnPermissions();
        repositoryPage.clickOnProjectAdminClonePermission();
        repositoryPage.clickOnProjectAdminForkPermission();
        String gitRepositoryId = repositoryPage.getRepositoryId();

        // Create SVN repository
        repositoryPage.clickOnScmRepositories();
        scmProjectSettingsPage = scmRepositoriesPage.clickOnNewRepository();
        scmProjectSettingsPage.selectSvn();
        scmProjectSettingsPage.clickOnConfigure();
        scmProjectSettingsPage.clickOnNext();
        scmProjectSettingsPage.setRepositoryName("svn-" + simpleDateFormat.format(new Date()));
        scmProjectSettingsPage.uploadScm("svntestrepo.svn");
        repositoryPage = scmProjectSettingsPage.clickOnFinish();
        repositoryPage.waitForFinish();
        String svnRepositoryId = repositoryPage.getRepositoryId();

        // Create Mercurial repository
        repositoryPage.clickOnScmRepositories();
        scmProjectSettingsPage = scmRepositoriesPage.clickOnNewRepository();
        scmProjectSettingsPage.selectMercurial();
        scmProjectSettingsPage.clickOnConfigure();
        scmProjectSettingsPage.clickOnNext();
        scmProjectSettingsPage.setRepositoryName("hg-" + simpleDateFormat.format(new Date()));
        scmProjectSettingsPage.uploadScm("hgtestrepo.hg");
        repositoryPage = scmProjectSettingsPage.clickOnFinish();
        repositoryPage.waitForFinish();
        String hgRepositoryId = repositoryPage.getRepositoryId();

        // Create test baseline
        BaselinesPage baselinesPage = repositoryPage.clickOnBaselines();
        baselinesPage.clickOnNewBaseline();
        baselinesPage.setName("test baseline 1");
        baselinesPage.setDescription("First baseline");
        baselinesPage.clickOnOk();
        sleep(5000);

        // Get tracker urls
        projectsPage = baselinesPage.clickOnProjects();
        projectsPage.clickOnCompactList();
        projectWikiPage = projectsPage.clickOnProject("test-project-sel-");
        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();

        String testCaseTrackerId = trackersPage.getTrackerId("Test Cases");
        String requirementsTrackerId = trackersPage.getTrackerId("Requirements");
        String requirementTrackerId = trackersPage.getTrackerId("test-tracker-wwf");
        String trackerWithoutWorkflowId = trackersPage.getTrackerId("test-tracker-wowf");
        String releaseTrackerId = trackersPage.getTrackerId("Releases");
        String trackerWithWorkflowId = trackersPage.getTrackerId("test-tracker-wwf-sel15699");

        System.out.println("-DTESTCASES_TRACKER_ID=" + testCaseTrackerId + " -DREQS_TRACKER_ID=" + requirementsTrackerId +
                " -DREQ_TRACKER_ID=" + requirementTrackerId + " -DTRACKER_WITHOUT_WORKFLOW_ID=" + trackerWithoutWorkflowId +
                " -DRELEASES_TRACKER_ID=" + releaseTrackerId + " -DTRACKER_WITH_WORKFLOW_ID="+ trackerWithWorkflowId +
                " -DPROJECT_ID=1" + " -DGIT_REPO_ID="+ gitRepositoryId + " -DSVN_REPO_ID="+ svnRepositoryId +
                " -DHG_REPO_ID="+ hgRepositoryId);
    }

    private void createUser(String userName, String password, String email, UsersPage usersPage) {
        CreateUserPage createUserPage = usersPage.clickOnNewAccount();
        createUserPage.setUserName(userName);
        createUserPage.setPassword(password);
        createUserPage.setConfirmPassword(password);
        createUserPage.setFirstName(password);
        createUserPage.setLastName(password);
        createUserPage.setEmail(email);
        createUserPage.clickOnSave();
    }

    @Test
    public void generateRegressionEnvironment() {
        MyStartPage myStartPage = Helper.loginWithUser("bond", "007");
        SystemAdminPage systemAdminPage = myStartPage.clickOnSystemAdmin();
        UsersPage usersPage = systemAdminPage.clickOnUserAccounts();
        createUser("agiletest", "netuddki", "testelemer@gmail.com", usersPage);
        createUser("collabtest", "netuddki", "testelemer@gmail.com", usersPage);
        createUser("drtest", "netuddki", "testelemer@gmail.com", usersPage);
        createUser("qatest", "netuddki", "testelemer@gmail.com", usersPage);
        createUser("rmtest", "netuddki", "testelemer@gmail.com", usersPage);

    }
}
