package net.sourceforge.jsurveyor;
import java.util.*;
import java.util.regex.*;

/**
 * Class to model users.
 * The class holds all information about the user.
 * 
 * @version 1.0
 * @author Claus Methmann Christensen
 * @author Anders Spliid Hansen
 * @author Esben Iversen Hansen
 * @author Lars H Jensen
 * @author Marcus Overheu
 * @author Thomas Ramdal
 * @author Peter Thomsen
 */
public class User extends UserComponent {

	/**
	 * Field userName
	 */
	private String userName;
	/**
	 * Field password
	 */
	private String password;
	/**
	 * Field userAccess
	 */
	private AccessControl userAccess;
	/**
	 * Field email
	 */
	private String email;
	/**
	 * Field fullName
	 */
	private String fullName;
	/**
	 * Field city
	 */
	private String city;
	/**
	 * Field country
	 */
	private String country;

	/**
	 * Construct a user with no data, to use the class as a bean.
	 */
	public User() {
		super();
		userAccess = new AccessControl("normal");
	}

	/** Construct a user with all fields except ArrayList refChildren and refParents
	 * 
	 * @param userName String
	 * @param password String
	 * @param userType String
	 * @param email String
	 * @param fullName String
	 * @param city String
	 * @param country String
	 * 
	 */
	public User(
		String userName,
		String password,
		String userType,
		String email,
		String fullName,
		String city,
		String country) {

		super();
		this.userName = userName;
		this.password = PasswordService.getInstance().encrypt(password);
		this.userAccess = new AccessControl(userType);
		this.email = email;
		this.fullName = fullName;
		this.city = city;
		this.country = country;
	}

	/** Construct a user with all fields. Usage only through DataAccess.
	 * @param userName String
	 * @param password String
	 * @param userType String
	 * @param email String
	 * @param fullName String
	 * @param city String
	 * @param country String
	 * @param adminUserGroups ArrayList
	 * @param refParents ArrayList
	 */
	public User(
		String userName,
		String password,
		String userType,
		String email,
		String fullName,
		String city,
		String country,
		ArrayList adminUserGroups,
		ArrayList refParents) {

		super(new ArrayList(), refParents);
		this.userName = userName;
		this.password = password;
		this.userAccess = new AccessControl(userType, adminUserGroups);
		this.email = email;
		this.fullName = fullName;
		this.city = city;
		this.country = country;
	}

	/** 
	 * Returns the username.
	 * @return String
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * Sets the username.
	 * @param userName String
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * Returns the encrypted form of the password.
	 * @return String
	 */
	public String getPassword() {
		return password;
	}

	/** 
	 * Validate the password passed as argument, against the User's.
	 * @param pass String
	 * @return boolean
	 */
	public boolean checkPassword(String pass) {
		return password.equals(pass);
	}

	/** 
	 * Sets the password. 
	 * Encrypts the String passed as argument using PasswordService.
	 * @param password String
	 */
	public void setPassword(String password) {
		this.password = PasswordService.getInstance().encrypt(password);
	}

	/** 
	 * Returns the email.
	 * @return String
	 */
	public String getEmail() {
		return email;
	}

	/** 
	 * Sets the email
	 * @param email String
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Validates the e-mail address. 
	 * Please note that this is a very simple regular expression, and that it will only catch common errors. 
	 * 
	 * For a specification of e-mail and syntax of e-mail adresses, check RFC-2822.
	 * @return boolean
	 */
	public boolean isEmailValid() {
		boolean result;
		if (email == null)
			result = false;
		else {
			Pattern p = Pattern.compile("^[^@ ]+@[^@ ]+\\.[^@ \\.]+$");
			Matcher m = p.matcher(this.email);
			result = m.matches();
		}
		return result;
	}

	/** 
	 * Returns the AccessControl object that belongs to this user.
	 * @return AccessControl
	 */
	public AccessControl getAccessControl() {
		return userAccess;
	}

	/** Returns the fullname.
	 * @return String
	 */
	public String getFullName() {
		return fullName;
	}
	/** Sets the fullname.
	 * @param fullName String
	 */
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	/** Returns the city.
	 * @return String
	 */
	public String getCity() {
		return city;
	}
	/** Sets the city.
	 * @param city String
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/** Returns the country.
	 * @return String
	 */
	public String getCountry() {
		return country;
	}
	/** Sets the country.
	 * @param country String
	 */
	public void setCountry(String country) {
		this.country = country;
	}

	/** Checks that all fields have been set
	 * @return boolean
	 */
	public boolean isAllSet() {
		return isUserNameSet()
			&& isPasswordSet()
			&& isFullNameSet()
			&& isEmailSet()
			&& isCitySet()
			&& isCountrySet();
	}

	/** checks that the userName is set
	 * @return boolean
	 */
	public boolean isUserNameSet() {
		return !(userName == null || userName.length() == 0);
	}

	/** checks that the password is set
	 * @return boolean
	 */
	public boolean isPasswordSet() {
		return !(password == null || password.length() == 0);
	}

	/** checks that the fullName is set
	 * @return boolean
	 */
	public boolean isFullNameSet() {
		return !(fullName == null || fullName.length() == 0);
	}

	/** checks that the email is set
	 * @return boolean
	 */
	public boolean isEmailSet() {
		return !(email == null || email.length() == 0);
	}

	/** checks that the city is set
	 * @return boolean
	 */
	public boolean isCitySet() {
		return !(city == null || city.length() == 0);
	}

	/** checks that the country is set
	 * @return boolean
	 */
	public boolean isCountrySet() {
		return !(country == null || country.length() == 0);
	}

	/** Returns a String representation, consisting of the userName prefixed with an "U". 
	 * @return String
	 */
	public String toString() {
	
		return "U" + userName;

	} // end toString
} // end class