/* ************************************************************************
#
#  DivConq
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2012 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */
package divconq.db.common;

import org.joda.time.DateTime;

import divconq.db.IDatabaseManager;
import divconq.db.UpdateRequest;
import divconq.struct.FieldStruct;
import divconq.struct.RecordStruct;
import divconq.util.StringUtil;

/**
 * Authenticate a user and get a user session token.  This can be run in three modes:
 * 
 * 1) regular sign on with user name and password
 * 2) first sign on with user name, confirmation code and password (when confirm required)
 * 3) recovery mode where user name and recovery code only are used
 * 
 * @author Andy
 *
 */
public class SignOnRequest extends UpdateRequest {
	protected String name = null;
	protected String code = null;
	protected String password = null;
	
	/**
	 * @param v user name used in query
	 */
	public void setUserName(String v) {
		this.name = v;
	}
	
	/**
	 * @param v password entered by user
	 */
	public void setPassword(String v) {
		this.password = v;
	}
	
	/**
	 * @param v recovery code entered by user
	 */
	public void setCode(String v) {
		this.code = v;
	}
	
	/**
	 * Sign on in recovery mode, provide user name here then
	 * set the recovery code using "setCode".
	 * 
	 * @param username entered by user
	 */
	public SignOnRequest(String username) {
		super("dcSignOn");
		this.name = username;
	}
	
	/**
	 * Sign on normally with user name and password
	 * 
	 * @param username entered by user
	 * @param password entered by user
	 */
	public SignOnRequest(String username, String password) {
		super("dcSignOn");
		this.name = username;
		this.password = password;
	}
	
	/**
	 * First sign on to confirm user, includes user name, password
	 * and confirm code.
	 * 
	 * @param username entered by user
	 * @param password entered by user
	 * @param code confirm code entered by user
	 */
	public SignOnRequest(String username, String password, String code) {
		super("dcSignOn");
		this.name = username;
		this.password = password;
		this.code = code;
	}
	
	/* (non-Javadoc)
	 * @see divconq.db.IDatabaseRequest#getDatabaseParams()
	 */
	@Override
	public RecordStruct getDatabaseParams(IDatabaseManager dbm) {
		RecordStruct params = new RecordStruct(
				new FieldStruct("UserName", (this.name != null) ? this.name.trim().toLowerCase() : null),
				new FieldStruct("At", new DateTime())
		);
		
		if (StringUtil.isNotEmpty(this.password)) {
			params.setField("Password", dbm.getObfuscator().hashStringToHex(password.trim()));
		}
		else
			params.setField("RecoverExpire", new DateTime().minusMinutes(30));		// 30 minutes before recovery expires
		
		if (StringUtil.isNotEmpty(this.code))
			params.setField("Code", this.code.trim());
		
		return params;
	}
}
