/* ************************************************************************
#
#  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.api;

import java.util.TimeZone;

import divconq.bus.Message;
import divconq.bus.TimeoutPlan;
import divconq.struct.FieldStruct;
import divconq.struct.RecordStruct;
import divconq.util.StringUtil;

public class ApiBase {
	protected IApiConnector connector = null;
	public String lastError = null;
	public Message lastMessage = null;
	
	public String authToken = null;
	public String userId = null;
	public String first = null;
	public String last = null;
	public String email = null;
	public String locale = null; 
	public String timeZone = null;
	
	public ApiBase(IApiConnector connector) {
		this.connector = connector;
		this.connector.init(this);
	}
	
	public String getLastError() {
		if (StringUtil.isNotEmpty(this.lastError))
			return this.lastError;
		
		if (this.lastMessage != null)
			return this.lastMessage.getFieldAsString("Message");
		
		return null;
	}
	
	public Message getLastMessage() {
		return this.lastMessage;
	}
	
	public String getUserId() {
		return this.userId;
	}
	
	public String getUserFirstName() {
		return this.first;
	}
	
	public String getUserLastName() {
		return this.last;
	}
	
	public String getUserEmail() {
		return this.email;
	}
	
	public String getAuthentationToken() {
		return this.authToken;
	}
	
	public String getLocale() {
		return this.locale;
	}
	
	public String getTimeZone() {
		return this.timeZone;
	}
	
	public boolean signOn(String user, String pass) {
		return this.signOn(user, pass, null);
	}
	
	public boolean signOn(String user, String pass, String code) {
		this.clearAuthentication();
		
		Message msg = new Message();
		msg.setField("Service", "ftcUser");
		msg.setField("Feature", "Authentication");
		msg.setField("Op", "SignOn");
		
		msg.setField("Body",
				new RecordStruct(
					new FieldStruct("UserName", user),
					new FieldStruct("Password", pass),
					new FieldStruct("ConfirmationCode", code)
				)
		);
		
		Message rmsg = this.sendMessage(msg);
		
		if (rmsg == null)
			return false;
	
		RecordStruct att = rmsg.getFieldAsRecord("Body");
		
		if (att == null)
			return false;
		
		this.authToken = att.getFieldAsString("AuthToken");
		this.userId = att.getFieldAsString("UserId");
		
		if (StringUtil.isEmpty(this.authToken) || StringUtil.isEmpty(this.userId))
			return false;

		if (!att.isFieldEmpty("FirstName"))
			this.first = att.getFieldAsString("FirstName");

		if (!att.isFieldEmpty("LastName"))
			this.last = att.getFieldAsString("LastName");

		if (!att.isFieldEmpty("Email"))
			this.email = att.getFieldAsString("Email");

		if (!att.isFieldEmpty("Locale"))
			this.locale = att.getFieldAsString("Locale");

		if (!att.isFieldEmpty("TimeZone"))
			this.timeZone = att.getFieldAsString("TimeZone");
		
		return !rmsg.hasError();
	}
	
	public void clearAuthentication() {
		this.authToken = null;
		this.userId = null;
		this.locale = "en";		// TODO try jvm locale 
		this.timeZone = TimeZone.getDefault().getID();
	}
	
	public boolean signOff() {
		Message msg = new Message();
		msg.setField("Service", "ftcUser");
		msg.setField("Feature", "Authentication");
		msg.setField("Op", "SignOff");
		
		Message rmsg = this.sendMessage(msg);
		
		this.clearAuthentication();
		
		if (rmsg == null)
			return false;
		
		return !rmsg.hasError();
	}
	
	public Message sendMessage(Message msg) {
		return this.sendMessage(msg, TimeoutPlan.Regular);
	}
	
	public Message sendMessage(final Message msg, TimeoutPlan timeoutPlan) {
		Message rmsg = this.connector.sendMessage(msg, timeoutPlan);
		//this.authToken = rmsg.getFieldAsString("AuthToken");
		return rmsg;
	}
}
