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.TableViewPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackerItemListPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackerItemListPage.ViewMenuIcon;
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 WorkflowActionsUpdateItemPropertiesWorkItemFieldTests {

	String testProjectNamePrefix = "tp testWfActionsUpdItemPropsWorkItemsFld";
	String[] workItemFieldInitialValue = {"test custreq 1", "test custreq 2"};

    public void testWorkflowActionsUpdateItemPropertiesWorkItemField(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();

        // Create tracker items to be selected as values in work item field
        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();
        TrackerItemListPage itemListPage = trackersPage.openTrackerFromLeftTree("Customer Requirement Specifications");
        TableViewPage tableViewPage = (TableViewPage)itemListPage.clickOnViewMenuIcon(ViewMenuIcon.TABLE_VIEW);
        CreateEditTrackerItemPage createEditTrackerItemPage = tableViewPage.tableView.clickOnNewItem();
        createEditTrackerItemPage.setSummary("test custreq 1");
        createEditTrackerItemPage.setDescriptionInMarkupTab("test custreq 1");
        TrackerItemDetailsPage trackerItemDetailsPage = createEditTrackerItemPage.clickOnSave();
        createEditTrackerItemPage = trackerItemDetailsPage.clickOnNewItem();
        createEditTrackerItemPage.setSummary("test custreq 2");
        createEditTrackerItemPage.setDescriptionInMarkupTab("test custreq 2");
        trackerItemDetailsPage = createEditTrackerItemPage.clickOnSave();
        createEditTrackerItemPage = trackerItemDetailsPage.clickOnNewItem();
        createEditTrackerItemPage.setSummary("test custreq 3");
        createEditTrackerItemPage.setDescriptionInMarkupTab("test custreq 3");
        trackerItemDetailsPage = createEditTrackerItemPage.clickOnSave();

        // STEP 1
        // Navigate to tracker customization page
        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.createCustomMembersField(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.addActionUpdateItemPropertiesSetWorkItemFieldToValue(propName, actionValue);
        } else if (action.equals("Clear")) {
        	trackerConfigurationPage.stateTransitionsTab.addActionUpdateItemPropertiesClearWorkItemField(propName);
        } else if (action.equals("Add")) {
        	trackerConfigurationPage.stateTransitionsTab.addActionUpdateItemPropertiesAddValueToWorkItemField(propName, actionValue);
        } else if (action.equals("Remove")) {
        	trackerConfigurationPage.stateTransitionsTab.addActionUpdateItemPropertiesRemoveValueFromWorkItemField(propName, actionValue);
        } else if (action.equals("Retain")) {
        	trackerConfigurationPage.stateTransitionsTab.addActionUpdateItemPropertiesRetainValueInWorkItemField(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
        trackersPage = trackerConfigurationPage.clickOnTrackers();
        TrackerItemListPage trackerItemListPage = trackersPage.openTrackerFromLeftTree(trackerName);
        trackerItemListPage.clickOnTableView();
        createEditTrackerItemPage = trackerItemListPage.tableView.clickOnNewItem();
        createEditTrackerItemPage.setSummary("test " + trackerName + " item abc");
        Selenide.sleep(3000);
        createEditTrackerItemPage.setMemberFieldValue(propName, workItemFieldInitialValue);
        Selenide.sleep(3000);
        createEditTrackerItemPage.setDescriptionInMarkupTab("test " + trackerName + " item abc desc");

        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("Add")) {
        	for(String memberName : workItemFieldInitialValue) {
        		trackerItemDetailsPage.assertThatFieldHasValue(propName, memberName);
        	}
        	trackerItemDetailsPage.assertThatFieldHasValue(propName, actionValue);
        } else if (action.equals("Remove")) {
        	for(String memberName : workItemFieldInitialValue) {
        		if (!memberName.equals(actionValue)) {
        			trackerItemDetailsPage.assertThatFieldHasValue(propName, memberName);
        		}
        	}
        	trackerItemDetailsPage.assertThatFieldHasNoValue(propName, actionValue);
        } else if (action.equals("Retain")) {
        	for(String memberName : workItemFieldInitialValue) {
        		if (!memberName.equals(actionValue)) {
        			trackerItemDetailsPage.assertThatFieldHasNoValue(propName, memberName);
        		}
        	}
        	trackerItemDetailsPage.assertThatFieldHasValue(propName, actionValue);
        } else {
        	throw new TestException("This action (" + action + ") is not implemented in this automatic test, so you cannot use it in test parameters");
        }
    }

}
