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

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.testng.TestException;
import org.testng.annotations.Listeners;

@Listeners(ScreenShooter.class)
public class WorkflowActionsUpdateItemPropertiesIntegerFieldTests {

	String testProjectNamePrefix = "tp testWfActionsUpdItemPropsIntegerFld";
	int integerFieldInitialValue = 5;

    public void testWorkflowActionsUpdateItemPropertiesIntegerField(String trackerName, String transitionName, String action, String propName, String actionValue) {
		// 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 property (field) name starts with "Custom" string, it means that the choice field must be created in the tracker first
        if (propName.startsWith("Custom")) {
        	trackerConfigurationPage.clickOnFieldsTab();
        	trackerConfigurationPage.waitForPageToLoad();
        	trackerConfigurationPage.fieldsTab.createCustomIntegerField(propName);
        }

        // 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 item property update, depending on action to do
        if (action.equals("Set to")) {
       		trackerConfigurationPage.stateTransitionsTab.addActionUpdateItemPropertiesSetIntegerFieldToValue(propName, actionValue);
        } else if (action.equals("Clear")) {
        	trackerConfigurationPage.stateTransitionsTab.addActionUpdateItemPropertiesClearIntegerField(propName);
        } else if (action.equals("Increase by")) {
        	trackerConfigurationPage.stateTransitionsTab.addActionUpdateItemPropertiesIncreaseIntegerFieldByValue(propName, actionValue);
        } else if (action.equals("Decrease by")) {
        	trackerConfigurationPage.stateTransitionsTab.addActionUpdateItemPropertiesDecreaseIntegerFieldByValue(propName, actionValue);
        } else {
        	throw new TestException("This action (" + action + ") 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
        TrackerItemListPage trackerItemListPage = trackerConfigurationPage.clickOnTrackerInBreadCrumbBar();
        trackerItemListPage.clickOnTableView();
        CreateEditTrackerItemPage createEditTrackerItemPage = trackerItemListPage.tableView.clickOnNewItem();
        createEditTrackerItemPage.setSummary("test " + trackerName + " item abc");
        createEditTrackerItemPage.setDescriptionInMarkupTab("test " + trackerName + " item abc desc");
        createEditTrackerItemPage.setIntegerFieldValue(propName, integerFieldInitialValue);
        TrackerItemDetailsPage trackerItemDetailsPage = createEditTrackerItemPage.clickOnSave();
        trackerItemDetailsPage.clickOnTransitionInStatusContextMenu(transitionName);

        // Asserting that transition changed item field to the defined value
        if (action.equals("Set to")) {
            trackerItemDetailsPage.assertThatFieldHasValue(propName, actionValue);
        } else if (action.equals("Clear")) {
        	trackerItemDetailsPage.assertThatFieldHasValue(propName, "--");
        } else if (action.equals("Increase by")) {
        	int shouldBeValue = integerFieldInitialValue + Integer.parseInt(actionValue);
        	trackerItemDetailsPage.assertThatFieldHasValue(propName, String.valueOf(shouldBeValue));
        } else if (action.equals("Decrease by")) {
        	int shouldBeValue = integerFieldInitialValue - Integer.parseInt(actionValue);
        	trackerItemDetailsPage.assertThatFieldHasValue(propName, String.valueOf(shouldBeValue));
        } else {
        	throw new TestException("This action (" + action + ") is not implemented in this automatic test, so you cannot use it in test parameters");
        }
    }

}
