package cz.cvut.fel.hernaosc.via.sterope.user

import cz.cvut.fel.hernaosc.via.sterope.feed.RssFeed

class User implements Serializable {

	private static final long serialVersionUID = 1

	transient springSecurityService

	String username
	String password
	boolean enabled = true
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

	String firstname
	String lastname
	String email
	
	FacebookUser facebookUser
	
	static hasMany = [
		rssFeeds: RssFeed
		]

	static transients = ['springSecurityService']

	static constraints = {
		username blank: false, unique: true
		password blank: false, password: true, display: false
		firstname blank: true, nullable: true
		lastname blank: true, nullable: true
		email blank: false, nullable: true, email: true
		facebookUser nullable: true
	}

	static mapping = { password column: '`password`' }

	User(String username, String password) {
		this()
		this.username = username
		this.password = password
	}

	@Override
	int hashCode() {
		username?.hashCode() ?: 0
	}

	@Override
	boolean equals(other) {
		is(other) || (other instanceof User && other.username == username)
	}

	@Override
	String toString() {
		username
	}

	Set<Role> getAuthorities() {
		UserRole.findAllByUser(this)*.role
	}

	def beforeInsert() {
		encodePassword()
		if(!firstname) firstname = ""
		if(!lastname) lastname = ""
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
	}
}
