angular.module("robware").controller('status', ["$scope", "statusService", "$interval", function($scope, statusService, $interval) {
	$scope.chartData=[];
	var chartDataId=0;

	function reloadUptime() {
		statusService.getUptime().then(function(data) {
			$scope.uptime = data;
			$scope.haveUptime = true;
		});
	}
	$scope.haveUptime = false;
	$scope.uptime = {};
	reloadUptime();
	$interval(reloadUptime, 60000);

	function reloadSystemInfo() {
		statusService.getSystemInfo().then(function(data) {
			if ($scope.chartData.length>=50)
				$scope.chartData.shift();
			$scope.chartData=$scope.chartData.concat([{id: chartDataId, CPU: data.load[0]*100, Mem: data.memUsage, Temp: data.cpuTemp}]);
			chartDataId++;
			$scope.load = data.load;
			$scope.cpuTemp = data.cpuTemp;
			$scope.memory = {
				free: data.memFree,
				used: data.memUsage,
				total: data.memTotal
			};
			$scope.haveSystemInfo = true;
		});
	}
	$scope.haveSystemInfo = false;
	$scope.load = {};
	$scope.loadHistory = [];
	$scope.cpuTemp = 0;
	$scope.systemInfoRefreshInterval = 5;
	var systemRefresh = $interval(reloadSystemInfo, $scope.systemInfoRefreshInterval * 1000);
	reloadSystemInfo();
	$scope.changeSystemInfoRefreshInterval = function(amount) {
		$scope.systemInfoRefreshInterval += amount;
		if ($scope.systemInfoRefreshInterval < 1)
			return;
		$interval.cancel(systemRefresh);
		systemRefresh = $interval(reloadSystemInfo, $scope.systemInfoRefreshInterval * 1000);
	};

	function reloadProcesses() {
		statusService.getProcesses().then(function(data) {
			$scope.processes = data;
			$scope.haveProcesses = true;
		});
	}
	$scope.haveProcesses = false;
	$scope.processses = {};
	$scope.processRefreshInterval = 10;
	var processRefesh = $interval(reloadProcesses, $scope.processRefreshInterval * 1000);
	reloadProcesses();
	$scope.changeProcesRefreshInterval = function(amount) {
		$scope.processRefreshInterval += amount;
		if ($scope.processRefreshInterval < 1)
			return;
		$interval.cancel(processRefesh);
		processRefesh = $interval(reloadProcesses, $scope.processRefreshInterval * 1000);
	};
}]);