/**
* Provides a service class for the Task domain class.
*
*/
class TaskService {

    boolean transactional = false

    def dateUtilService
    def personService

    /**
    * Determines and returns a possible parent list
    * @param taskInstance The task to use when determining the possible parent list.
    * @returns A list of the possible parents.
    */
    def possibleParentList(taskInstance) {
        def criteria = taskInstance.createCriteria()
        def possibleParentList = criteria {
            and {
                notEqual('trash', true)
                notEqual('id', taskInstance.id)
                taskInstance.subTasks.each() { notEqual('id', it.id) }
                }
        }
    }

    /**
    * Creates a new task with the given params.
    * @param params The params to use when creating the new task.
    * @returns A map containing result.error=true (if any error) and result.taskInstance.
    */
    def create(params) {
        Task.withTransaction { status ->
            def result = [:]
            // Default status to "not started" if not supplied.
            params.taskStatus = params.taskStatus ?: TaskStatus.get(1)
            def taskInstance = new Task(params)
            result.taskInstance = taskInstance

            if(result.taskInstance.parentTask?.trash) {
                status.setRollbackOnly()
                result.taskInstance.errors.rejectValue("parentTask", "task.operationNotPermittedOnTaskInTrash")
                result.error = true
                return result
            }

            if(taskInstance.save()) {
                def taskModification = new TaskModification(person: personService.currentUser(),
                                                        taskModificationType: TaskModificationType.get(1),
                                                        task: taskInstance)

                if(!taskModification.save()) {
                    status.setRollbackOnly()
                    taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
                    result.error = true
                    return result
                }

                // If we get here all went well.
                return result
            }
            else {
                result.error = true
                return result
            }

        } //end withTransaction
    } // end create()

    /**
    * Creates a subTask copying attributes from the parentTask unless otherwise specified.
    * The taskProcedure is only assigned to the sub task if given in params.
    * @param parentTask The parent task to get attributes from, also set as the parent.
    * @param params Overrides the parent task values if specified.
    * @returns A map containing result.error=true (if any error) and result.taskInstance.
    */
    def createSubTask(parentTask, params = [:]) {

        def result = [:]

        //Make our new Task a subTask and set the required properites.
        def p = [:]
        p.parentTask = parentTask
        p.description = params.description ?: parentTask.description
        p.comment = params.comment ?: parentTask.comment

        p.taskGroup = params.taskGroup ?: parentTask.taskGroup
        p.taskStatus = TaskStatus.get(1) // A new subTask must always be "Not Started".
        p.taskPriority = parentTask.taskPriority
        p.taskType = params.taskType ?: parentTask.taskType
        p.leadPerson = params.leadPerson ?: parentTask.leadPerson
        p.primaryAsset = params.primaryAsset ?: parentTask.primaryAsset

        p.targetStartDate = params.targetStartDate ?: parentTask.targetStartDate
        p.targetCompletionDate = params.targetCompletionDate ?: parentTask.targetCompletionDate

        if(params.taskProcedure) p.taskProcedure = params.taskProcedure

                    //Set the assignedPersons
//                     taskInstance.assignedPersons.each() {
// 
//                     def assignedPerson = new AssignedPerson(person: it.person,
//                                                                                             task: subTaskInstance,
//                                                                                             estimatedHour: it.estimatedHour,
//                                                                                             estimatedMinute: it.estimatedMinute).save()
//                     }

        result = create(p)

    } // end createSubTask()

    /**
    * Creates a new task entry.
    * @param params The params to use when creating the new entry.
    * @returns A map containing result.error=true (if any error), result.entryInstance and result.taskId.
    */
    def createEntry(params) {
        Task.withTransaction { status ->
            def result = [:]
            result.entryInstance = new Entry(params)
            result.entryInstance.enteredBy = personService.currentUser()

            if(result.entryInstance.validate()) {
                def taskInstance = Task.lock(result.entryInstance.task.id)
                result.taskId = result.entryInstance.task.id

                if(!taskInstance) {
                    status.setRollbackOnly()
                    result.entryInstance.errors.rejectValue('task', "entry.task.notFound")
                    result.error = true
                    return result
                }

                if(taskInstance.taskStatus.id == 3) {
                    status.setRollbackOnly()
                    result.entryInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask")
                    result.error = true
                    return result
                }

                // If task status is "Not Started" and entry type is "Work Done" then we create the started modification and set the status.
                if(taskInstance.taskStatus.id == 1 && result.entryInstance.entryType.id == 2) {

                    // Create the "Started" task modification, this provides the "Actual started date".
                    def taskModification = new TaskModification(person: personService.currentUser(),
                                                            taskModificationType: TaskModificationType.get(2),
                                                            task: taskInstance)

                    if(!taskModification.save()) {
                        status.setRollbackOnly()
                        taskInstance.errors.rejectValue("task", "entry.task.failedToSaveTaskModification")
                        result.error = true
                        return result
                    }

                    // Set task status to "In progress".
                    taskInstance.taskStatus = TaskStatus.get(2)

                    if(!taskInstance.save()) {
                        status.setRollbackOnly()
                        result.entryInstance.errors.rejectValue("task", "entry.task.failedToSave")
                        result.error = true
                        return result
                    }
                }

                if(!result.entryInstance.save()) {
                    status.setRollbackOnly()
                    result.error = true
                    return result
                }

                // All went well if we get to here.
                return result
            }
            else {
                result.error = true
                return result
            }

        } //end withTransaction
    } // end create()

    /**
    * Updates an existing task.
    * @param params The params to update for task with id of params.id.
    * @returns A map containing result.error=true (if any error) and result.taskInstance.
    */
    def update(params) {
        Task.withTransaction { status ->
            def result = [:]
            result.taskInstance = Task.get(params.id)
            if(result.taskInstance) {

                // Optimistic locking check.
                if(params.version) {
                    def version = params.version.toLong()
                    if(result.taskInstance.version > version) {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
                        result.error = true
                        return result
                    }
                }

                result.taskInstance.properties = params

                if(result.taskInstance.save()) {
                    def taskModification = new TaskModification(person:personService.currentUser(),
                                                            taskModificationType: TaskModificationType.get(3),
                                                            task: result.taskInstance)
                    if(taskModification.save()) {
                        // All went well.
                        return result
                    }
                    else {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
                        result.error = true
                        return result
                    }
                }
            }
            // Something failed.
            status.setRollbackOnly()
            result.error = true
            return result

        } //end withTransaction
    }  // end update()

    /**
    * Completes an existing task.
    * @param params The params for task with id of params.id.
    * @returns A map containing result.error=true (if any error) and result.taskInstance.
    */
    def complete(params) {
        Task.withTransaction { status ->
            def result = [:]
            result.taskInstance = Task.get(params.id)
            if(result.taskInstance) {

                // Optimistic locking check.
                if(params.version) {
                    def version = params.version.toLong()
                    if(result.taskInstance.version > version) {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
                        result.error = true
                        return result
                    }
                }

                result.taskInstance.taskStatus = TaskStatus.get(3)
                result.taskInstance.taskRecurringSchedule?.enabled = false

                if(result.taskInstance.save()) {
                    def taskModification = new TaskModification(person:personService.currentUser(),
                                                            taskModificationType: TaskModificationType.get(4),
                                                            task: result.taskInstance)

                    if(taskModification.save()) {
                        // All went well.
                        return result
                    }
                    else {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
                        result.error = true
                        return result
                    }
                }
            }
            // Something failed.
            status.setRollbackOnly()
            result.error = true
            return result

        } //end withTransaction
    }  // end complete()

    /**
    * Reopens an existing task.
    * @param params The params for task with id of params.id.
    * @returns A map containing result.error=true (if any error) and result.taskInstance.
    */
    def reopen(params) {
        Task.withTransaction { status ->
            def result = [:]
            result.taskInstance = Task.get(params.id)
            if(result.taskInstance) {

                // Optimistic locking check.
                if(params.version) {
                    def version = params.version.toLong()
                    if(result.taskInstance.version > version) {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
                        result.error = true
                        return result
                    }
                }

                result.taskInstance.taskStatus = TaskStatus.get(2)

                if(result.taskInstance.save()) {
                    def taskModification = new TaskModification(person:personService.currentUser(),
                                                            taskModificationType: TaskModificationType.get(5),
                                                            task: result.taskInstance)
                    if(taskModification.save()) {
                        // All went well.
                        return result
                    }
                    else {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
                        result.error = true
                        return result
                    }
                }
            }
            // Something failed.
            status.setRollbackOnly()
            result.error = true
            return result

        } //end withTransaction
    }  // end reopen()

    /**
    * Move a task to the trash.
    * @param params The params for task with id of params.id.
    * @returns A map containing result.error=true (if any error) and result.taskInstance.
    */
    def trash(params) {
        Task.withTransaction { status ->
            def result = [:]
            result.taskInstance = Task.get(params.id)
            if(result.taskInstance) {

                // Optimistic locking check.
                if(params.version) {
                    def version = params.version.toLong()
                    if(result.taskInstance.version > version) {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
                        result.error = true
                        return result
                    }
                }

                result.taskInstance.trash = true
                result.taskInstance.taskRecurringSchedule?.enabled = false

                if(result.taskInstance.save()) {
                    def taskModification = new TaskModification(person:personService.currentUser(),
                                                            taskModificationType: TaskModificationType.get(6),
                                                            task: result.taskInstance)
                    if(taskModification.save()) {
                        // All went well.
                        return result
                    }
                    else {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
                        result.error = true
                        return result
                    }
                }
            }
            // Something failed.
            status.setRollbackOnly()
            result.error = true
            return result

        } //end withTransaction
    }  // end trash()

    /**
    * Restore a task from the trash.
    * @param params The params for task with id of params.id.
    * @returns A map containing result.error=true (if any error) and result.taskInstance.
    */
    def restore(params) {
        Task.withTransaction { status ->
            def result = [:]
            result.taskInstance = Task.get(params.id)
            if(result.taskInstance) {

                // Optimistic locking check.
                if(params.version) {
                    def version = params.version.toLong()
                    if(result.taskInstance.version > version) {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
                        result.error = true
                        return result
                    }
                }

                result.taskInstance.trash = false

                if(result.taskInstance.save()) {
                    def taskModification = new TaskModification(person:personService.currentUser(),
                                                            taskModificationType: TaskModificationType.get(7),
                                                            task: result.taskInstance)
                    if(taskModification.save()) {
                        // All went well.
                        return result
                    }
                    else {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
                        result.error = true
                        return result
                    }
                }
            }
            // Something failed.
            status.setRollbackOnly()
            result.error = true
            return result

        } //end withTransaction
    }  // end restore()

    /**
    * Approve a task.
    * @param params The params for task with id of params.id.
    * @returns A map containing result.error=true (if any error) and result.taskInstance.
    */
    def approve(params) {
        Task.withTransaction { status ->
            def result = [:]
            result.taskInstance = Task.get(params.id)
            if(result.taskInstance) {

                // Optimistic locking check.
                if(params.version) {
                    def version = params.version.toLong()
                    if(result.taskInstance.version > version) {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
                        result.error = true
                        return result
                    }
                }

                result.taskInstance.approved = true

                if(result.taskInstance.save()) {
                    def taskModification = new TaskModification(person:personService.currentUser(),
                                                            taskModificationType: TaskModificationType.get(8),
                                                            task: result.taskInstance)
                    if(taskModification.save()) {
                        // All went well.
                        return result
                    }
                    else {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
                        result.error = true
                        return result
                    }
                }
            }
            // Something failed.
            status.setRollbackOnly()
            result.error = true
            return result

        } //end withTransaction
    }  // end approve()

    /**
    * Remove a previously given approval from a task.
    * @param params The params for task with id of params.id.
    * @returns A map containing result.error=true (if any error) and result.taskInstance.
    */
    def renegeApproval(params) {
        Task.withTransaction { status ->
            def result = [:]
            result.taskInstance = Task.get(params.id)
            if(result.taskInstance) {

                // Optimistic locking check.
                if(params.version) {
                    def version = params.version.toLong()
                    if(result.taskInstance.version > version) {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
                        result.error = true
                        return result
                    }
                }

                result.taskInstance.approved = false

                if(result.taskInstance.save()) {
                    def taskModification = new TaskModification(person:personService.currentUser(),
                                                            taskModificationType: TaskModificationType.get(9),
                                                            task: result.taskInstance)
                    if(taskModification.save()) {
                        // All went well.
                        return result
                    }
                    else {
                        status.setRollbackOnly()
                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
                        result.error = true
                        return result
                    }
                }
            }
            // Something failed.
            status.setRollbackOnly()
            result.error = true
            return result

        } //end withTransaction
    }  // end renegeApproval()

} // end TaskService
