package com.capgemini.reports.workflows;

import com.capgemini.framework.teamcenter.exceptions.NoTeamcenterConnectionException;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.capgemini.reports.AbstractTeamcenterTest;
import com.capgemini.reports.exceptions.NotFoundException;
import com.capgemini.reports.teamcenter.queries.DirectQuery;
import com.capgemini.reports.teamcenter.queries.saved.WorkflowOnChangeQuery;
import com.capgemini.reports.users.SnecmaUser;
import com.capgemini.reports.workflows.Task.TaskState;
import com.teamcenter.soa.client.model.strong.EPMTask;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class TaskTest extends AbstractTeamcenterTest {

    @Test
    public void testBuildDaughterTask() throws NoTeamcenterConnectionException, NotFoundException {
        DirectQuery<EPMTask> taskQuery = new DirectQuery<>(getSession());
        EPMTask task = taskQuery.searchByUID("iOeZxqYwhLofXA"); // iOeZxqYwhLofXA <-- Add TcReleaseStatus Task
        getSession().loadProperties(task,"state","creation_date","last_mod_date");
        Assert.assertNotNull(task);
        Task motherTask = new Task(getSession(), task);
        // State
        Assert.assertEquals(TaskState.COMPLETED, motherTask.getState());
        // No active Tasks in this simple task
        List<Task> activeTasks = motherTask.getActiveChildTasks();
        Assert.assertNotNull(activeTasks);
        Assert.assertTrue(activeTasks.isEmpty());
        // Dates duly created
        Assert.assertNotNull(motherTask.getCreationDate());
        Assert.assertNotNull(motherTask.getLastModDate());
        Assert.assertNotNull(motherTask.getLastModUser());
        // No Signoff users
        Assert.assertNotNull(motherTask.getPendingSignoffsUser());
        Assert.assertTrue(motherTask.getPendingSignoffsUser().isEmpty());
    }

    @Test
    public void testBuildTaskWithValidAndInvalidSignoffs() throws NoTeamcenterConnectionException, NotFoundException {
        DirectQuery<EPMTask> taskQuery = new DirectQuery<>(getSession());
        EPMTask task = taskQuery.searchByUID("jQXZxqYwhLofXA"); // jQXZxqYwhLofXA <-- EPMSelectSignoffTask select-signoff-team
        Assert.assertNotNull(task);
        getSession().loadProperties(task,"state","creation_date","last_mod_date");
        Task motherTask = new Task(getSession(), task);
        // State
        Assert.assertEquals(TaskState.COMPLETED, motherTask.getState());
        // No active Tasks in this simple task
        List<Task> activeTasks = motherTask.getActiveChildTasks();
        Assert.assertNotNull(activeTasks);
        Assert.assertTrue(activeTasks.isEmpty());
        // Mother task is its own parent
        // Dates duly created
        Assert.assertNotNull(motherTask.getCreationDate());
        Assert.assertNotNull(motherTask.getLastModDate());
        Assert.assertNotNull(motherTask.getLastModUser());
        // Signoff users: invalid
        Assert.assertNotNull(motherTask.getPendingSignoffsUser());
        Assert.assertEquals(1, motherTask.getPendingSignoffsUser().size());
        for (SnecmaUser user : motherTask.getPendingSignoffsUser()) {
            Assert.assertNotNull(user);
            Assert.assertEquals("mbo", user.getUserId());
        }
    }


    @Test
    public void testBuild() throws NoTeamcenterConnectionException {
        WorkflowOnChangeQuery query = new WorkflowOnChangeQuery(getSession());
        query.searchIDAndRevision("MOD200478", "AA");
        List<EPMTask> tasksList = query.findAll();
        EPMTask theMotherTask = null;
        for (EPMTask epmTask : tasksList) {
            if ("TEXZxqYwhLofXA".equals(epmTask.getUid())) {
                theMotherTask = epmTask;
            }
        }
        Assert.assertNotNull(theMotherTask);
    }

    @Override
    public PolicyLocation getPropertyPolicyIdentifier() {
        return R13_POLICY_FULL;
    }
}
