package com.intland.codebeamer.functionaltest.test;

import com.intland.codebeamer.functionaltest.helper.Helper;
import com.intland.codebeamer.functionaltest.page.*;
import com.intland.codebeamer.functionaltest.page.common.MoreMenuHelper;
import com.intland.codebeamer.functionaltest.page.trackers.NewTrackerPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackerConfigurationPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackerItemListPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackersPage;
import org.junit.Assert;
import org.testng.annotations.*;

import static com.codeborne.selenide.Selenide.*;
import static org.testng.Assert.assertEquals;

/**
 * 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 productLicense agreement
 * you entered into with Intland.
 */
public class TrackerTests extends BaseTest {
    private final String trackerProjectName = "TrackerProject";
    private final String projectKey = "TRP";
    private final String archiveName = "tracker-project.zip";

    @DataProvider(name = "TrackerTypeProvider")
    public static Object[][] trackerTypes() {
        return new Object[][]{
                {"Bug", false, false, false, false, false, null},
                {"Change Request", false, false, true, true, false, null},
                {"Component", false, true, false, true, true, "Components"},
                {"Configuration Items", false, true, true, false, false, null},
                {"Contact", true, false, false, false, false, null},
                {"Issue", true, false, true, false, false, null},
                {"Platform", true, true, false, false, false, null},
                {"Release", true, true, true, true, false, null},
                {"Requirement", false, false, false, true, true, "Requirements"},
                {"Risk", false, false, false, false, false, null},
                {"Task", false, false, true, true, false, null},
                {"Team", false, true, false, true, true, "Teams"},
                {"Test Case", false, true, true, true, true, "Test Cases"},
                {"Test Configuration", true, false, false, true, false, null},
                {"Test Run", true, false, true, false, false, null},
                {"Test Set", true, true, false, false, false, null},
                {"Time Recording", true, true, true, false, false, null},
                {"User Story", false, false, false, true, true, "User Stories"}
        };
    }

    @Test(dataProvider = "TrackerTypeProvider", groups = {"create"})
    public void testCreateTracker(final String trackerType, final boolean availableAsTemplate, final boolean usingWorkflow, final boolean visibility,
                                  final boolean usingTemplate, final boolean inheritConfig, final String templateName) {
        // Login codeBeamer Test Instance as a Project Admin.
        Helper.loginWithSystemAdmin();

        // Open trackers page
        TrackersPage trackersPage = open("/project/Selenium/tracker", TrackersPage.class);

        // Click on "New Tracker" link.
        NewTrackerPage newTrackerPage = trackersPage.clickOnNewTracker();

        // 	Create a new ${type} tracker with option "Available as availableAsTemplate" ${availableAsTemplate}, "Workflow" ${usingWorkflow} and "Visibility" ${visibility}.
        newTrackerPage.selectTrackerType(trackerType);
        newTrackerPage.setName(trackerType);
        newTrackerPage.setKey(trackerType);
        newTrackerPage.clickOnMarkupTab();
        newTrackerPage.setDescription(trackerType);
        newTrackerPage.setAvailableAsTemplate(availableAsTemplate);
        if (usingTemplate) {
            newTrackerPage.selectTemplate(projectName);
            newTrackerPage.setConfigInherit(inheritConfig);
        }
        newTrackerPage.setUsingWorkflow(usingWorkflow);
        newTrackerPage.setVisibility(visibility);
        TrackerConfigurationPage trackerConfigurationPage = newTrackerPage.clickOnSave();

        trackerConfigurationPage.clickOnGeneralTab();
        assertEquals(trackerConfigurationPage.generalTab.getName(), trackerType);
        assertEquals(trackerConfigurationPage.generalTab.getKey(), trackerType);
        assertEquals(trackerConfigurationPage.generalTab.getDescription(), trackerType);
        assertEquals(trackerConfigurationPage.generalTab.isAvailableAsTemplate(), availableAsTemplate, "availableAsTemplate");
        // Workflow is not available in Team trackers
        if ("Team".equals(trackerType)) {
            assertEquals(trackerConfigurationPage.generalTab.isUsingWorkflow(), false);
        } else {
            assertEquals(trackerConfigurationPage.generalTab.isUsingWorkflow(), usingWorkflow, "usingWorkflow");
        }
        assertEquals(trackerConfigurationPage.generalTab.isVisible(), visibility, "isVisible");
        trackerConfigurationPage.generalTab.checkIfConfigInherited(inheritConfig);
        if (inheritConfig) {
            trackerConfigurationPage.generalTab.checkIfHasTemplateTracker(templateName);
        } else {
            trackerConfigurationPage.generalTab.checkIfHasNoTemplateTracker();
        }

    }

    @AfterMethod(groups = {"create"})
    public void afterMethod(){
        TrackerConfigurationPage trackerConfigurationPage = page(TrackerConfigurationPage.class);
        trackerConfigurationPage.generalTab.clickOnDelete();
        trackerConfigurationPage.generalTab.confirmDelete();
        sleep(1000);
        TrashPage trashPage = trackerConfigurationPage.clickOnTrash();
        trashPage.clickOnEmptyTrash();
        confirm();
        sleep(1000);
    }

    @BeforeGroups(groups = {"delete"})
    public void createDeleteTrackersProject() {
        createProjectFromArchive(archiveName, trackerProjectName, projectKey);
    }

    @Test(groups = {"delete"})
    public void testDeleteAndRestoreSimpleTracker() {
        Helper.loginWithSystemAdmin();
        String trackerName = TrackersPage.TrackerTypes.REQUIREMENTS.toString();
        TrackersPage trackersPage = deleteTracker(trackerName);
        TrashPage trashPage = trackersPage.clickOnTrash();
        trashPage.selectTracker(trackerName);
        trashPage.clickOnRestore();
        trackersPage = trashPage.clickOnTrackers();
        Assert.assertTrue(trackersPage.isTrackerVisible(trackerName));
    }

    @Test(groups = {"delete"})
    public void testDeleteAndDeleteBigTracker() {
        Helper.loginWithSystemAdmin();
        String trackerName = "BigTracker";
        TrackersPage trackersPage = deleteTracker(trackerName);
        TrashPage trashPage = trackersPage.clickOnTrash();
        trashPage.selectTracker(trackerName);
        trashPage.clickOnDelete();
    }

    @Test(groups = {"delete"})
    public void testDeleteTemplateTracker() {
        Helper.loginWithSystemAdmin();
        String trackerName = TrackersPage.TrackerTypes.BUGS.toString();
        TrackersPage trackersPage = deleteTracker(trackerName);
        TrashPage trashPage = trackersPage.clickOnTrash();
        trashPage.selectTracker(trackerName);
        trashPage.clickOnRestore();
        trackersPage = trashPage.clickOnTrackers();
        Assert.assertTrue(trackersPage.isTrackerVisible(trackerName));
    }

    @Test(groups = {"delete"})
    public void testDeleteEmptyTracker() {
        Helper.loginWithSystemAdmin();
        deleteTracker(TrackersPage.TrackerTypes.TEST_RUNS.toString());
    }

    private TrackersPage deleteTracker(String trackerName) {
        TrackersPage trackersPage = open(HOST_URL + "/project/TRP/tracker", TrackersPage.class);
        TrackerItemListPage trackerItemListPage = trackersPage.openTrackerFromLeftTree(trackerName);
        trackerItemListPage.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.CUSTOMIZE);
        TrackerConfigurationPage trackerConfigurationPage = page(TrackerConfigurationPage.class);
        trackerConfigurationPage.clickOnGeneralTab();
        trackerConfigurationPage.generalTab.clickOnDelete();
        trackersPage = trackerConfigurationPage.generalTab.confirmDelete();
        sleep(1000);
        return trackersPage;
    }

    @AfterGroups(groups = {"delete"})
    public void deleteDeleteTrackersProject() {
        deleteProject(trackerProjectName);
    }

}
