package models.eSecurePlay1;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import play.data.validation.Required;
import play.db.jpa.GenericModel;

@Entity
public class EsAccount extends GenericModel implements
		net.eclettica.esecure.models.EsAccount {

	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "uuid")
	public String uuid;

	@Required
	@Basic(optional = false)
	public String password;

	@Transient
	@Required
	@play.data.validation.Equals(value = "password")
	public String repasswd;

	@Required
	@Basic(optional = false)
	@Column(nullable = false)
	public String username;

	@Required
	@Basic(optional = true)
	@Column(nullable = true)
	public String userGroup;

	@ManyToMany(cascade = { CascadeType.DETACH })
	private Set<EsRole> roles = new HashSet<EsRole>();

	public void setRoles(Set<EsRole> roles) {
		this.roles = roles;
	}

	public Set<EsRole> getRoles() {
		return this.roles;
	}

	public EsAccount() {
		this.uuid = null; // UUID.randomUUID().toString();
		this.password = "";
		this.username = "";
		this.userGroup = "";

	}

	public EsAccount(String password, String username, String group) {
		this.password = password;
		this.username = username;
		this.userGroup = group;
		this.uuid = null;// UUID.randomUUID().toString();
	}
	
	
	@PrePersist
	@PreUpdate
	public void preSave() {
		if (this.uuid == null)
			this.uuid = UUID.randomUUID().toString();
	}

	@Override
	public String getUsername() {

		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;

	}

	@Override
	public String getPassword() {

		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;

	}

	@Override
	public String getUuid() {

		return this.uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getId() {

		return this.uuid;
	}

	public void setId(String uuid) {
		this.uuid = uuid;
	}

	@Override
	public String getUserGroup() {
		// TODO Auto-generated method stub
		return this.userGroup;
	}

	public void setUserGroup(String usergroup) {
		this.userGroup = usergroup;

	}

	@Override
	public boolean equals(Object other) {
		if (!(other instanceof net.eclettica.esecure.models.EsAccount))
			return false;
		net.eclettica.esecure.models.EsAccount ot = (net.eclettica.esecure.models.EsAccount) other;

		return this.getUuid().equals(ot.getUuid());

	}

    public static models.eSecurePlay1.EsAccount findByUsername(String username) {
        if(username == null || username.isEmpty()){
            return null;
        }
        return find("username=?", username).first();
    }

}
