'use strict';

const request = require("request");
const fs = require("fs");
const path = require("path");

function ActivityRecorder(mainProcessController) {
	// const tbServerHost = "http://10.88.187.97";
	const tbServerHost = "http://52.32.246.19";
	const tbServerPort = "8082";

	var _this = this;
	var userName;

	var init = function() {
	};

	var addCommonInfoToPostObj = function(msgObj) {
		var recordDate = new Date();

		msgObj.userName = userName;
		msgObj.date = recordDate.toGMTString();
		msgObj.timestamp = recordDate.getTime();

		return msgObj;
	};

	_this.checkServerAlive = function(errorCallback, successCallback) {
		request
			.get(tbServerHost + ":" + tbServerPort + "/api/v1/echo")
			.on('error', errorCallback)
			.on('response', successCallback);
	};

	_this.getUserPictureInfoFromServer = function(errorCallback, successCallback) {
		request(tbServerHost + ":" + tbServerPort + "/api/v1/picture/user/" + userName, function(error, response, body) {
			if (!error && response.statusCode == 200) {
				// parse JSON-response to object
				var imageInfoObj = JSON.parse(body);

				successCallback(imageInfoObj);
			} else {
				if (error) {
					// server not responding
				} else {
					// invalid response code
				}
			}
		});
	};

	_this.recordUserInfo = function(userInfoObj) {
		// add username & datetime
		userInfoObj = addCommonInfoToPostObj(userInfoObj);
		userInfoObj.type = "research-topic";

		request.post({
			url: tbServerHost + ":" + tbServerPort + "/api/v1/researchtopic",
			formData: userInfoObj
		});
	};

	_this.recordNavigation = function(tabViewId, url) {
		var recordNavigationData = {
			type: 'navigation',
			tabViewId: tabViewId,
			url: url
		};

		// add date & time
		recordNavigationData = addCommonInfoToPostObj(recordNavigationData);

		request.post({
			url: tbServerHost + ":" + tbServerPort + "/api/v1/browsingdata",
			formData: recordNavigationData
		});
	};

	// currently, click events are not posted to server
	// there is no way to differentiate meaningful and meaningless clicks
	// instead, screenshots are sent to server when a navigation is done
	_this.recordClickEvent = function(tabViewId, url, eventData) {

	};

	_this.recordScrollEvent = function(tabViewId, url) {
		var scrollData = {
			type: "scroll",
			tabViewId: tabViewId,
			url: url
		};

		scrollData = addCommonInfoToPostObj(scrollData);

		request.post({
			url: tbServerHost + ":" + tbServerPort + "/api/v1/scroll",
			formData: scrollData
		});
	};

	_this.recordFileDownload = function(itemData) {
		itemData = addCommonInfoToPostObj(itemData);

		request.post({
			url: tbServerHost + ":" + tbServerPort + "/api/v1/download",
			formData: itemData
		});
	};

	_this.uploadScreenshot = function(tabViewId, url, imagePath) {
		var formData = {
			type: 'screenshot',
			tabViewId: tabViewId,
			url: url,
			fileName: path.basename(imagePath),
			imageAttachment: fs.createReadStream(imagePath)
		};

		formData = addCommonInfoToPostObj(formData);

		// post image data
		request.post({
			url: tbServerHost + ":" + tbServerPort + "/api/v1/screenshot",
			formData: formData
		}, function optionalCallback(err, httpResponse, body) {
			if (err) {
				return console.error("upload failed: " + err);
			}

			// go ahead and delete the image from local hard drive
			fs.unlink(imagePath, function(err) {

			});
		});
	};

	_this.recordTrackingStatusChange = function(isTrackingOn) {
		var formData = {
			type: 'tracking-status',
			isTrackingOn: isTrackingOn
		};

		formData = addCommonInfoToPostObj(formData);

		request.post({
			url: tbServerHost + ":" + tbServerPort + "/api/v1/tracking-status",
			formData: formData
		});
	};

	_this.setParticipantUserName = function(newUserName) {
		userName = newUserName;
	};

	init();
}

module.exports = ActivityRecorder;
@@ -3416,36 +3416,46 @@
 kingOn) %7B%0A%09%09var 
-form
+trackingStatus
 Data = %7B%0A%09%09%09type
@@ -3496,33 +3496,103 @@
 On: 
-isTrackingOn%0A%09%09%7D;%0A%0A%09%09form
+new Boolean(isTrackingOn).toString()%09// can't be primitive boolean value%0A%09%09%7D;%0A%0A%09%09trackingStatus
 Data
@@ -3609,36 +3609,46 @@
 onInfoToPostObj(
-form
+trackingStatus
 Data);%0A%0A%09%09reques
@@ -3732,36 +3732,46 @@
 %22,%0A%09%09%09formData: 
-form
+trackingStatus
 Data%0A%09%09%7D);%0A%09%7D;%0A%0A

convert boolean to string for http request