package com.intland.codebeamer.functionaltest.test.tracker.configuration.workflow;

import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.testng.ScreenShooter;
import com.intland.codebeamer.functionaltest.helper.Helper;
import com.intland.codebeamer.functionaltest.page.trackers.CreateEditTrackerItemPage;
import com.intland.codebeamer.functionaltest.page.CreateProjectPage;
import com.intland.codebeamer.functionaltest.page.mystart.MyStartPage;
import com.intland.codebeamer.functionaltest.page.wiki.ProjectWikiPage;
import com.intland.codebeamer.functionaltest.page.projects.ProjectsPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackerConfigurationPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackerItemDetailsPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackerItemListPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackersPage;

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

import org.openqa.selenium.support.ui.Sleeper;
import org.testng.TestException;
import org.testng.annotations.Listeners;

@Listeners(ScreenShooter.class)
public class WorkflowActionsCreateNewSequentialNumberTests {

	String testProjectNamePrefix = "tp testWfActionsCreateNewSequentialNumber";

    public void testWorkflowActionsCreateNewSequentialNumber(String trackerName, String transitionName, String fieldName, String prefixType, String prefixValue) {
		// Login as system admin
    	MyStartPage myStartPage = Helper.loginWithSystemAdmin();
    	
        // Create a project
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    	Date now = new Date();
        ProjectsPage projectsPage = myStartPage.clickOnProjects();
        CreateProjectPage createProjectPage = projectsPage.clickOnNewProject();
        createProjectPage.clickOnNext();
        createProjectPage.waitForSecondScreen();
        createProjectPage.setName(testProjectNamePrefix + " " + dateFormat.format(now));
        createProjectPage.setKey("tp" + System.currentTimeMillis());
        createProjectPage.clickOnNext();
        ProjectWikiPage projectWikiPage = createProjectPage.clickOnFinish();

        // STEP 1
        // Navigate to tracker customization page
        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();
        TrackerConfigurationPage trackerConfigurationPage = trackersPage.navigateToTrackerConfigurationPage(trackerName);

        // If field name starts with "Custom" string, it means that the integer field must be created in the tracker first
        if (fieldName.startsWith("Custom")) {
        	trackerConfigurationPage.clickOnFieldsTab();
        	trackerConfigurationPage.waitForPageToLoad();
        	trackerConfigurationPage.fieldsTab.createCustomIntegerField(fieldName);
        	Selenide.sleep(3000);
        }
        

        // STEP 2
        // Navigate to State Transitions tab
        trackerConfigurationPage.waitForPageToLoad();
        trackerConfigurationPage.clickOnStateTransitionsTab();
        
        // STEP 3
        // Activate editing of transition by using its context menu
        trackerConfigurationPage.stateTransitionsTab.openContextMenuOfTransitionAndClickOnMenuItem(transitionName, "Edit");

        // STEP 4, STEP 5
        // Setup create sequential number action
        if (prefixType.equals("Value")) {
       		trackerConfigurationPage.stateTransitionsTab.addActionCreateNewSequentialNumberSetPrefixToValue(fieldName, prefixValue);
        } else if (prefixType.equals("Copy from")) {
        	trackerConfigurationPage.stateTransitionsTab.addActionCreateNewSequentialNumberSetPrefixToFieldValue(fieldName, prefixValue);
        } else if (prefixType.equals("Compute as")) {
        	trackerConfigurationPage.stateTransitionsTab.addActionCreateNewSequentialNumberSetPrefixToComputedValue(fieldName, prefixValue);
        } else {
        	throw new TestException("This prefix type (" + prefixType + ") is not implemented in this automatic test, so you cannot use it in test parameters");
        }

        // STEP 6
        // Create a tracker item, drive its workflow through the transition tested
        String summary = "test " + trackerName + " item abc";
        TrackerItemListPage trackerItemListPage = trackerConfigurationPage.clickOnTrackerInBreadCrumbBar();
        trackerItemListPage.clickOnTableView();
        CreateEditTrackerItemPage createEditTrackerItemPage = trackerItemListPage.tableView.clickOnNewItem();
        createEditTrackerItemPage.setSummary(summary);
        createEditTrackerItemPage.setDescriptionInMarkupTab("test " + trackerName + " item abc desc");
        TrackerItemDetailsPage trackerItemDetailsPage = createEditTrackerItemPage.clickOnSave();
        trackerItemDetailsPage.clickOnTransitionInStatusContextMenu(transitionName);

        // Asserting that transition changed item field to the defined value
        String referenceValue = "";
        if (prefixType.equals("Value")) {
        	referenceValue = prefixValue + "2";
        } else if (prefixType.equals("Copy from")) {
        	referenceValue = summary + "2";
        } else if (prefixType.equals("Compute as")) {
        	referenceValue = summary + "2";
        } else {
        	throw new TestException("This prefix type (" + prefixType + ") is not implemented in this automatic test, so you cannot use it in test parameters");
        }

        trackerItemDetailsPage.assertThatFieldHasValue(fieldName, referenceValue);
    }

}
