import org.codehaus.groovy.grails.plugins.springsecurity.Secured

class TaskDetailedController extends BaseController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

//     def list = {
//         params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
//         def taskInstanceActives = Task.findAllByIsActive( true ).list( params )
// //         def taskInstanceList = taskInstanceActives.list( params )
//         return [ taskInstanceList: taskInstanceList, taskInstanceTotal: taskInstanceActives.count() ]
//     }

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ taskInstanceList: Task.list( params ), taskInstanceTotal: Task.count() ]
    }

//     def show = {
//         def taskInstance = Task.get( params.id )
// 
//         if(!taskInstance) {
//             flash.message = "Task not found with id ${params.id}"
//             redirect(action:list)
//         }
//         else { 
//             def taskProcedureInstance = TaskProcedure.get(taskInstance?.taskProcedure?.id)
//             return [ taskInstance : taskInstance,  taskProcedureInstance: taskProcedureInstance] 
// 
//         }
//     }

    def show = {
        def taskInstance = Task.get( params.id )

        if(!taskInstance) {
            flash.message = "Task not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            def taskProcedureInstance = TaskProcedure.get(taskInstance.taskProcedure?.id)
            def taskProcedureExits = new Boolean("true")
            def showTaskTab = new String("true")

            if(!taskProcedureInstance) {
                taskProcedureExits = false
            }
            return [ taskInstance : taskInstance,
                            taskProcedureInstance: taskProcedureInstance,
                            taskProcedureExits: taskProcedureExits,
                            showTaskTab: showTaskTab] 
        }
    }

    def delete = {
        def taskInstance = Task.get( params.id )
        if(taskInstance) {
            try {
                taskInstance.isActive = false
                flash.message = "Task ${params.id} has been set to inactive."
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Task ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Task not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def taskInstance = Task.get( params.id )

        if(!taskInstance) {
            flash.message = "Task not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            def criteria = taskInstance.createCriteria()
            def results = criteria {
                and {
                    notEqual('id', taskInstance.id)
                    }
            }
            return [ taskInstance : taskInstance, possibleParentList: results ]
        }
    }

    def update = {
        def taskInstance = Task.get( params.id )
        if(taskInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(taskInstance.version > version) {
                    
                    taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
                    render(view:'edit',model:[taskInstance:taskInstance])
                    return
                }
            }
            taskInstance.properties = params
            if(!taskInstance.hasErrors() && taskInstance.save()) {
                flash.message = "Task ${params.id} updated"
                redirect(action:show,id:taskInstance.id)
            }
            else {
                render(view:'edit',model:[taskInstance:taskInstance])
            }
        }
        else {
            flash.message = "Task not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def taskInstance = new Task()
        taskInstance.properties = params
        return ['taskInstance':taskInstance]
    }

    def save = {
        def taskInstance = new Task(params)
        if(!taskInstance.hasErrors() && taskInstance.save()) {
            flash.message = "Task ${taskInstance.id} created"
            redirect(action:show,id:taskInstance.id)
        }
        else {
            render(view:'create',model:[taskInstance:taskInstance])
        }
    }

    //Generate a new TaskProcedure for a taskInstance.
    def generateProcedure = {
        def taskProcedureInstance = new TaskProcedure()
        taskProcedureInstance.properties = params

        def taskInstance = Task.get(params.taskInstance.id)
        def taskProcedureExits = new Boolean("true")
        def showProcedureTab = new String("true")

        if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save()) {
            taskProcedureInstance.addToTasks(taskInstance)
            flash.message = "Procedure created, you can now add steps to the procedure"
//             redirect(action:show, id:params.taskInstance.id)
        }
        else {
            taskProcedureExits = false
        }

        render(view:'show',model:[ taskInstance : taskInstance,  
                                                        taskProcedureInstance: taskProcedureInstance, 
                                                        taskProcedureExits: taskProcedureExits,
                                                        showProcedureTab: showProcedureTab])
    }

    def findProcedure = {
        redirect(controller:"taskProcedureDetailed", action:"list", id:params.taskInstance.id) 
    }
        
}
