poconsole.controller('configCtrl', function($scope, Page, $log, config, devices) {
	Page.setTitle('Configuration');
    Page.setPage('configuration');



    $scope.loadConfig = function(){
        $scope.loading = true;
        var getConfig = config.getConfig();
        getConfig.then(function(data) {
        	$scope.loading = false;
    		$scope.config = data.config;
        });
    };
    $scope.loadConfig();
    $scope.paramError = null;
    var selString = "Select a parameter...";
    $scope.newParam = {
        pSelected: selString,
        parameter: null,
        val: null
    };

    $scope.addParameter = function(){
        if ($scope.newParam.pSelected == selString){
            $scope.paramError = "No parameter selected";
        } else {
            if ($scope.newParam.pSelected == "other") {
                $scope.newParam.parameter = $scope.newParam.pEntry;
            } else {
                $scope.newParam.parameter = $scope.newParam.pSelected;
            }
            config.submitParameter($scope.newParam);
            $scope.loadConfig();
			$scope.newParam.pEntry = "";
			$scope.newParam.val = "";
        }
    };

	$scope.deleteParameter = function(id){
		var deleteParam = config.deleteParameter(id);
		deleteParam.then(function(d){
			$scope.loadConfig();
		});
	};

    $scope.checkLogger = function(){
        $scope.loggerLoading = true;
        var checkLoggerStatus = config.getLoggerStatus();
        checkLoggerStatus.then(function(data){
            $scope.loggerLoading = false;
        	$scope.loggerRunning = data.status;
        });
    };
    $scope.checkLogger();

    $scope.restartLogger = function(){
        var restartLogger = config.restartLogger();
        restartLogger.then(function(data){
        	$scope.checkLogger();
        });
    };

    $scope.getDevices = function(){
        var getDevices = devices.getAllDevices();
        getDevices.then(function(d){
            $scope.devices = d.devices;
        });
    };
    $scope.getDevices();


    $scope.addDevice = function(dev){
        var addDevice = devices.addDevice(dev);
        addDevice.then(function(d){
            $scope.getDevices();
			$scope.newDevice.address = "";
        });
    };

	$scope.deleteDevice = function(id){
		var deleteDevice = devices.deleteDevice(id);
		deleteDevice.then(function(d){
			$scope.getDevices();
		});
	};

    var getDeviceTypes = devices.getDeviceTypes();
    getDeviceTypes.then(function(d){
        $scope.device_types = d.device_types;
    });
});
