/*******************************************************************************
 * Copyright 2Fi Business Solutions Ltd.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.core;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "users")
public class User extends BaseEntity<Integer> implements UserDetails {

	private static final long serialVersionUID = -9036483598847846102L;

	/** Company ID */
	@Column(nullable = false)
	private int companyId;

	/** Customer ID, optional, for End User's customers */
	@Column(nullable = true)
	private Integer customerId;

	/** username for login */
	@Column(columnDefinition = "varchar(32)", nullable = false)
	private String username;

	/** password for login */
	@Column(columnDefinition = "varchar(64)", nullable = false)
	private String password;

	/** is user expired? (not implemented) */
	@Column(columnDefinition = "boolean", nullable = false)
	private boolean expired;

	/** is user locked? (not implemented) */
	@Column(columnDefinition = "boolean", nullable = false)
	private boolean locked;

	/** User default locale (en / zh_TW / zh_CN) */
	@Column(columnDefinition = "varchar(5)")
	private String locale;

	/** Full name for display */
	@Column(columnDefinition = "varchar(90)")
	private String fullname;

	/* User profile info (optional fields) START */

	/** First name */
	@Column(columnDefinition = "varchar(45)")
	private String firstname;

	/** Last name */
	@Column(columnDefinition = "varchar(30)")
	private String lastname;

	/** Department */
	@Column(nullable = true)
	private Integer deptId;

	/** Job title */
	@Column(columnDefinition = "varchar(60)")
	private String title;

	/** Email */
	@Column(columnDefinition = "varchar(120)")
	private String email;

	/** Phone 1 */
	@Column(columnDefinition = "varchar(30)")
	private String phone1;

	/** Phone 2 */
	@Column(columnDefinition = "varchar(30)")
	private String phone2;

	/** Remarks */
	@Column(columnDefinition = "varchar(600)")
	private String remarks;

	/* User profile info (optional fields) END */

	/** Transient authorities (populated during user authentication) */
	@Transient
	private List<GrantedAuthority> authorities;

	/*
	 * getters and setters
	 */

	public int getCompanyId() {
		return companyId;
	}

	public void setCompanyId(int companyId) {
		this.companyId = companyId;
	}

	public Integer getCustomerId() {
		return customerId;
	}

	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}

	public String getUsername() {
		return username;
	}

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

	public String getPassword() {
		return password;
	}

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

	public boolean isExpired() {
		return expired;
	}

	public void setExpired(boolean expired) {
		this.expired = expired;
	}

	public boolean isLocked() {
		return locked;
	}

	public void setLocked(boolean locked) {
		this.locked = locked;
	}

	public String getLocale() {
		return locale;
	}

	public void setLocale(String locale) {
		this.locale = locale;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public Integer getDeptId() {
		return deptId;
	}

	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone1() {
		return phone1;
	}

	public void setPhone1(String phone1) {
		this.phone1 = phone1;
	}

	public String getPhone2() {
		return phone2;
	}

	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public void setAuthorities(List<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	/*
	 * Override
	 */

	@Override
	public List<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Override
	public boolean isAccountNonExpired() {
		return !expired;
	}

	@Override
	public boolean isAccountNonLocked() {
		return !locked;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}
