﻿
var KnowhereUserService = function (baseUrl, personID, websiteID) {
	this.BaseUrl = baseUrl;
	this.PersonID = personID;
	this.WebsiteID = websiteID;
	var that = this;

	function GetJson(partUrl, params, callback) {
		var url = that.BaseUrl + partUrl + '?personid=' + that.PersonID + '&websiteid=' + websiteID + '&now=' + new Date();
		var jsonData = Sys.Serialization.JavaScriptSerializer.serialize(params);
		$.ajax({
			type: "POST",
			contentType: "application/json; charset=utf-8",
			url: url,
			data: jsonData,
			dataType: "json",
			success: function (result) {
				if (callback != undefined) callback.apply(this, Array(result));
			},
			error: function (result) {
				//alert('Could not connect to service ' + partUrl + ': ' + result.responseText);
			}
		});
	}

	function FormatDate(d) {
		var year = (d.getYear() < 1000) ? d.getYear() + 1900 : d.getYear();
		return year + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
	}

	return {
		CurrentDate: function (callback) {
			GetJson('getcurrentdate', {}, callback);
		},

		Debug: function (result) {
			var desc = '';
			for (var i in result) {
				desc = desc + i + ': ' + result[i] + '\n';
			}
			return desc;
		},

		FormatDateFromService:function(d){
			return d;
		},

		GetWeatherRegions: function(callback){
			GetJson('GetWeatherRegions', {}, callback);
		},

		GetUnitStatistics: function(gpsUnitID, callback){
			GetJson('GetUnitStatistics', {gpsUnitID: gpsUnitID}, callback);
		},

		GetWeather: function(mapAreaID, forDate, callback){
			GetJson('GetWeather', {mapAreaID: mapAreaID, forDate: forDate}, callback);
		},

		GetDemographics: function(gpsUnitID, callback){
			GetJson('GetDemographics', {gpsUnitID: gpsUnitID}, callback);
		},

		GetMessageImageUrl: function(messageID, pixelWidth, pixelHeight, callback){
			GetJson('GetMessageImageUrl', {messageID: messageID, pixelWidth: pixelWidth, pixelHeight: pixelHeight}, callback);
		},

		UpdateDemographicValue: function(gpsUnitID, demographicID, demographicValueID, callback){
			GetJson('UpdateDemographicValue', { gpsUnitID: gpsUnitID, demographicID: demographicID, demographicValueID: demographicValueID }, callback);
		},

		GetMapAreaDetails: function(mapAreaID, callback){
			GetJson('GetMapAreaDetails', { mapAreaID: mapAreaID }, callback);
		},

		GetRandomPushMessage: function(gpsUnitID, markMessagesAsSent, onlyMessagesFromPersonID, callback){
			GetJson('GetRandomPushMessage', { gpsUnitID: gpsUnitID, markMessagesAsSent: markMessagesAsSent, onlyMessagesFromPersonID: onlyMessagesFromPersonID }, callback);
		},

		GetMessagesFromNearbyAreas: function (gpsUnitID, messageType, withinXMetres, maximumPrice, callback) {
			GetJson('GetMessagesFromNearbyAreas', { gpsUnitID: gpsUnitID, type: messageType, withinXMetres: withinXMetres, maximumPrice: maximumPrice }, callback);
		},

		GetMessagesFromNearbyAreasIncludingDescription: function (gpsUnitID, messageType, withinXMetres, maximumPrice, callback) {
			GetJson('GetMessagesFromNearbyAreasIncludingDescription', { gpsUnitID: gpsUnitID, type: messageType, withinXMetres: withinXMetres, maximumPrice: maximumPrice, includeFullDescription: true }, callback);
		},

		GetMessagesFromNearbyAreasIncludingDescription2: function (gpsUnitID, messageType, withinXMetres, maximumPrice, currentLat, currentLon, callback) {
			GetJson('GetMessagesFromNearbyAreasIncludingDescription2', { gpsUnitID: gpsUnitID, type: messageType, withinXMetres: withinXMetres, maximumPrice: maximumPrice, includeFullDescription: true, currentLat: currentLat, currentLon: currentLon }, callback);
		},

		GetUnitProximity: function(gpsUnitID, mapAreaID, callback){
			GetJson('GetUnitProximity', { gpsUnitID: gpsUnitID, mapAreaID: mapAreaID }, callback);
		},

		GetProductDetails: function (messageID, gpsUnitID, mapAreaID, callback) {
			GetJson('GetProductDetails', { messageID: messageID, gpsUnitID: gpsUnitID, mapAreaID: mapAreaID }, callback);
		},

		GetGpsUnitByKey: function (key, callback) {
			GetJson('GetGpsUnitByKey', { key: key }, callback);
		},

		SignUp: function (email, name, callback) {
			GetJson('SignUp', { email: email, name: name }, callback);
		},

		SignIn: function (userName, password, callback) {
			GetJson('SignIn', { userName: userName, password: password }, callback);
		},

		SaveLocation: function (gpsUnitID, lat, lon, callback) {
			var now = FormatDate(new Date());
			GetJson('savelocation', { gpsUnitID: gpsUnitID, latitude: lat, longitude: lon, time: now }, callback);
		},

		GetLocation: function (gpsUnitID, callback) {
			GetJson('getlocation', { gpsUnitID: gpsUnitID }, callback);
		}
	}

};
