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

@Secured(['ROLE_Manager'])
class PersonController extends BaseAppAdminController {

	def authenticateService

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

	def index = {
		redirect action: list, params: params
	}

	def list = {
		if (!params.max) {
			params.max = 10
		}
		[personList: Person.list(params)]
	}

	def show = {
		def person = Person.get(params.id)
		if (!person) {
			flash.message = "Person not found with id $params.id"
			redirect action: list
			return
		}
		List roleNames = []
		for (role in person.authorities) {
			roleNames << role.authority
		}
		roleNames.sort { n1, n2 ->
			n1 <=> n2
		}
		[person: person, roleNames: roleNames]
	}

	/**
	 * Person delete action. Before removing an existing person,
	 * they should be removed from those authorities which they are involved.
	 */
	def delete = {

		def person = Person.get(params.id)
		if (person) {
			def authPrincipal = authenticateService.principal()
			//avoid self-delete if the logged-in user is an admin
			if (!(authPrincipal instanceof String) && authPrincipal.username == person.loginName) {
				flash.message = "You can not delete yourself, please login as another admin and try again"
			}
			else {
				//first, delete this person from Persons_Authorities table.
				Authority.findAll().each { it.removeFromPersons(person) }
                
                try {
                    person.delete()
                    flash.message = "Person $params.id deleted."
                    redirect(action:list)
                }
                catch(org.springframework.dao.DataIntegrityViolationException e) {
                    flash.message = "Could not delete '$person.loginName' due to database constraints, but all authorities have been removed."
                    redirect(action:show,id:params.id)
                }
			}
		}
		else {
			flash.message = "Person not found with id $params.id"
		}
	}

	def edit = {

		def person = Person.get(params.id)
		if (!person) {
			flash.message = "Person not found with id $params.id"
			redirect action: list
			return
		}

		return buildPersonModel(person)
	}

	/**
	 * Person update action.
	 */
	def update = {

		def person = Person.get(params.id)
		if (!person) {
			flash.message = "Person not found with id $params.id"
			redirect action: edit, id: params.id
			return
		}

		long version = params.version.toLong()
		if (person.version > version) {
			person.errors.rejectValue 'version', "person.optimistic.locking.failure",
				"Another user has updated this Person while you were editing."
            render view: 'edit', model: buildPersonModel(person)
			return
		}

		person.properties = params

        if(params.pass == "") {
            person.pass = "InsertNothingToClearValidation"
        }
        else {
            if (person.validate()) {
                person.password = authenticateService.encodePassword(params.pass)
            }
        }

        if (!person.hasErrors() && person.save()) {
            Authority.findAll().each { it.removeFromPersons(person) }
            addRoles(person)
            flash.message = "Person '$params.id - $params.loginName' updated."
            redirect action: show, id: person.id
        }
        else {
            render view: 'edit', model: buildPersonModel(person)
        }

	}

	def create = {
		[person: new Person(params), authorityList: Authority.list()]
	}

	/**
	 * Person save action.
	 */
	def save = {

		def person = new Person()
		person.properties = params
		person.password = authenticateService.encodePassword(params.pass)
		if (person.save()) {
			addRoles(person)
			redirect action: show, id: person.id
		}
		else {
			render view: 'create', model: [authorityList: Authority.list(), person: person]
		}
	}

	private void addRoles(person) {
		for (String key in params.keySet()) {
			if (key.contains('ROLE') && 'on' == params.get(key)) {
				Authority.findByAuthority(key).addToPersons(person)
			}
		}
	}

	private Map buildPersonModel(person) {

		List roles = Authority.list()
		roles.sort { r1, r2 ->
			r1.authority <=> r2.authority
		}
		Set userRoleNames = []
		for (role in person.authorities) {
			userRoleNames << role.authority
		}
		LinkedHashMap<Authority, Boolean> roleMap = [:]
		for (role in roles) {
			roleMap[(role)] = userRoleNames.contains(role.authority)
		}

		return [person: person, roleMap: roleMap]
	}
}
