(function () {
	
window.CenHteSession = CenHteSession;

window.HTE_CONFIG_ERROR =
{
	NO_CONFIG_SET		: 0,
	NO_FRAME_SET		: 1,
	NO_HOST_SET			: 2,
	NO_SID				: 3,
	SESSION_NOT_FOUND	: 4	
};

var errorStrings = [
	"No config set.",
	"No frame set.",
	"No host set.",
	"No SID.",
	"Session not found"
];

var errorCallback;

function invokeErrorCb(cb, code) {
	if (cb) {
		cb(code, errorStrings[code]);
	}
}

function genDeviceId() {
	return cenAX.genRandIdent();
}

function CenHteSession() {
	var deviceId = localStorage.getItem("com.censoft.hte.deviceId");

	if (!deviceId) {
		deviceId = genDeviceId();

		localStorage.setItem("com.censoft.hte.deviceId", deviceId);
	}

	this.deviceId = deviceId;
}

CenHteSession.prototype.setErrorCallback = function (cb) {
	errorCallback = cb;
};

CenHteSession.prototype.setHteHost = function (host) {
	this.host = host;
};

CenHteSession.prototype.setHtePort = function (port) {
	this.port = port;
	this.init = false;
};

CenHteSession.prototype.setHteProtocol = function (proto) {
	this.protocol = proto;
}

CenHteSession.prototype.getHtePort = function () {
	return this.port;
};

CenHteSession.prototype.setSessionId = function (sid) {
	this.sid = sid;
};

CenHteSession.prototype.getSessionId = function () {
	return this.sid;
};

CenHteSession.prototype.setDeviceId = function (deviceId) {
	this.deviceId = deviceId;
};

CenHteSession.prototype.setFrame = function (f) {
	this.frame = f;
};

CenHteSession.prototype.setTPXConfig = function (config) {
	this.config = config;
};

CenHteSession.prototype.setNewSessionCallback = function (cb) {
	this.newSessionCallback = cb;
};

CenHteSession.prototype.newSession = function () {
	$.ajax({
		url: "/new_session",
		type: "POST",
		contentType: "application/json",
		dataType: "json",
		context: this,
		data: JSON.stringify({deviceId:this.deviceId})
	})
	.done(function (response) {
		if (response.code == HTEStatus.OK) {
			this.setSessionId(response.content.sessionId);
			this.setHtePort(response.content.port);

			if (this.newSessionCallback && typeof this.newSessionCallback == "function") {
				this.newSessionCallback();
			}

			this.loadConfig(false, true);
		}
		else {
			if (errorCallback) {
				errorCallback(response.message);
			}
		}
	});
};

CenHteSession.prototype.loadConfig = function (disconnectFromHost, connectToHost, errCb) {
	if (!this.config) {
		invokeErrorCb(errCb, HTE_CONFIG_ERROR.NO_CONFIG_SET);
		return;
	}

	if (!this.sid) {
		this.newSession();
		return;
	}

	$.ajax({
		url: "/load_config?session=" + encodeURIComponent(this.sid),
		type: "POST",
		contentType: "application/json",
		dataType: "json",
		context: this,
		data: JSON.stringify({
			disconnectFromHost: disconnectFromHost,
			connectToHost: connectToHost,
			tpx: this.config	
		})
	})
	.done(function (res) {
		if (!this.frame) {
			invokeErrorCb(errCb, HTE_CONFIG_ERROR.NO_FRAME_SET);
			return;
		}

		if (!this.host) {
			invokeErrorCb(errCb, HTE_CONFIG_ERROR.NO_HOST_SET);
			return;
		}

		if (res.code == HTEStatus.OK) {
			if (!this.init) {
				this.frame.src = this.protocol + "//" + this.host + ":" + this.port + "/console.html";
				this.init = true;
			}
		}
		else if (res.code == HTEStatus.SESSION_NOT_FOUND) {
			delete(this.sid);
			this.loadConfig(disconnectFromHost, connectToHost);
		}
		else {
			if (errorCallback) {
				errorCallback(res.message);
			}
		}
	});
};

CenHteSession.prototype.attach = function (errCb) {
	if (!this.frame) {
		invokeErrorCb(errCb, HTE_CONFIG_ERROR.NO_FRAME_SET);
		return;
	}

	if (!this.host) {
		invokeErrorCb(errCb, HTE_CONFIG_ERROR.NO_HOST_SET);
		return;
	}

	if (!this.sid) {
		invokeErrorCb(errCb, HTE_CONFIG_ERROR.NO_SID);
		return;
	}

	$.ajax({
		url: "/get_config_values?session=" + encodeURIComponent(this.sid),
		type: "POST",
		contentType: "application/json",
		dataType: "json",
		context: this,
		data: JSON.stringify(["ConfigName"])
	})
	.done(function (res) {
		if (res.code == HTEStatus.SESSION_NOT_FOUND) {
			delete(this.sid);
			invokeErrorCb(errCb, HTE_CONFIG_ERROR.SESSION_NOT_FOUND);
			return;
		}

		if (!this.init) {
			this.frame.src = this.protocol + "//" + this.host + ":" + this.port + "/console.html";
			this.init = true;
		}
	});
};

CenHteSession.prototype.close = function () {
	$.ajax({
		url: "/close?session=" + encodeURIComponent(this.sid),
		type: "POST",
		contentType: "application/json",
		dataType: "json",
		context: this
	});
};

CenHteSession.prototype.loadRawCapture = function (blob) {
	if (!this.host) {
		alert("HTE host not set");
		return;
	}

	var url = "/load_capture?session=" + encodeURIComponent(this.sid);
	var xhr = new XMLHttpRequest();

	xhr.open("POST", url, true);
	xhr.onload = function (e) {
	};

	xhr.send(blob);
};

})();
