'use strict';

angular.module('google-agenda').factory('GoogleAgenda', ['$http',
	function($http) {

		var calendars;




		return {

			//returns a list of all the accessible calendars
			calendarList: function(next) {
				$http.get('/google-calendar/list').success(function (result) {
					calendars = result.items;
					next(calendars);
				});
			},
			calendarGet: function(calendarId, next) {
                if (calendars) {
                    next(calendars[calendars.map(function (e) { return e.id}).indexOf(calendarId)]);
                }
			},

			//returns a list of items for the current agenda
			//Parameters:
			//agendaId
			//min and max time
			eventsList: function (calendarId, timeMin, timeMax, next) {
				$http.get('https://www.googleapis.com/calendar/v3/calendars/' + calendarId + '/events?timeMin=' + timeMin + '&timeMax=' + timeMax + '&singleEvents=true&orderBy=startTime&key=AIzaSyAsF-msdca-Qgss5VvUQTf2QrlBW-eNApU').success(function (agendaItems) {
					next(agendaItems.items);
				});
			},
			eventGet: function(calendarId, eventId, next) {
				$http.get('https://www.googleapis.com/calendar/v3/calendars/' + calendarId + '/events/'+ eventId + '?key=AIzaSyAsF-msdca-Qgss5VvUQTf2QrlBW-eNApU').success(function (event) {
					next(event);
				});
			}

		};
	}
]);