'use strict';

angular.module('openSenseMapApp')
  .controller('SidebarDownloadCtrl',
	["$scope", "$stateParams", "$http", "OpenSenseBox", "OpenSenseBoxAPI", "leafletData", function($scope, $stateParams, $http, OpenSenseBox, OpenSenseBoxAPI, leafletData){

	$scope.markersFiltered = $scope.$parent.markersFiltered;
	$scope.$watch('$parent.loading', function() {
        $scope.markersFiltered = $scope.$parent.markersFiltered;
    });

	$scope.osemapi = OpenSenseBoxAPI;
	$scope.inputFilter = $scope.inputFilter || {};
	$scope.downloadform = {
		format: 'CSV',
		pleaseWait: false,
		emptyData: false,
		errorOccured: false
	};

	$scope.openDatepicker = function($event) {
		$event.preventDefault();
		$event.stopPropagation();

		// prevent both date pickers from being opened at the same time
		if($event.currentTarget.id === "datepicker1") {
		  $scope.opened1 = true;
		  $scope.opened2 = false;
		} else if($event.currentTarget.id === "datepicker2") {
		  $scope.opened2 = true;
		  $scope.opened1 = false;
		}
	};

	$scope.endingDate = function(numDays){
		$scope.inputFilter.DateTo = new Date();
		$scope.inputFilter.DateFrom = new Date((new Date()).valueOf() - 1000*60*60*24*numDays);
	};

	leafletData.getMap("map_main").then(function(map) {
		$scope.count = $scope.countBbox(map).length;
		map.on('zoomend moveend', function(evt){
			$scope.count = $scope.countBbox(map).length;
		})
	});

	$scope.countBbox = function(map){
		var boxids = [];
		var bbox = map.getBounds();
		angular.forEach($scope.markersFiltered, function(marker, key) {
			if(bbox.contains([marker.lat, marker.lng])) {
				boxids.push(marker.station.id);
			}
		});
		return boxids;
	};

	function stampDownload () {
		try {
			return "-" + new Date().toISOString().replace(/-|:|\.\d*Z/g,"").replace("T","_");
		} catch (e) {
			return "";
		}
	}

	$scope.dataDownload = function(){
		$scope.downloadform.pleaseWait = true;
		leafletData.getMap("map_main").then(function(map) {
			var boxids = $scope.countBbox(map);
			$http.get($scope.osemapi.url+'/boxes/data', {
				params: {
					boxid: boxids.join(','),
					'to-date': $scope.inputFilter.DateTo,
					'from-date': $scope.inputFilter.DateFrom,
					phenomenon: $scope.inputFilter.Phenomenon	
				}
			})
			.success(function(data) {
				console.log(data.length);
				if(data.length>0){
					var blob = new Blob([data],{type:'text/csv;charset=utf-8;'});
					var link = document.createElement('a');
					link.href = window.URL.createObjectURL(blob);
					link.download = "opensensemap_org-download-" + encodeURI($scope.inputFilter.Phenomenon) + stampDownload() +'.csv';
					link.style.visibility = 'hidden';
					document.body.appendChild(link);
					link.click();
					document.body.removeChild(link);
				} else {
					$scope.downloadform.emptyData=true;
				}
				$scope.downloadform.pleaseWait = false;
			})
			.error(function(data, err) {
				$scope.downloadform.pleaseWait = false;
				if(data.length===0 && err === 404){
					$scope.downloadform.emptyData=true;
				} else {
					$scope.downloadform.errorOccured = true;
					console.log(err);
				}
			});
		});
	};

}]);

@@ -1821,16 +1821,195 @@
 %09%09%7D%0A%09%7D%0A%0A
+  function createDateAsUTC(date) %7B%0A    return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));%0A  %7D%0A%0A
 %09$scope.
@@ -2270,32 +2270,48 @@
 %09%09%09%09%09'to-date': 
+createDateAsUTC(
 $scope.inputFilt
@@ -2319,16 +2319,17 @@
 r.DateTo
+)
 ,%0A%09%09%09%09%09'
@@ -2340,16 +2340,32 @@
 -date': 
+createDateAsUTC(
 $scope.i
@@ -2383,16 +2383,17 @@
 DateFrom
+)
 ,%0A%09%09%09%09%09p
@@ -2432,17 +2432,16 @@
 enomenon
-%09
 %0A%09%09%09%09%7D%0A%09

change date to UTC, fixes #168