poconsole.controller('configCtrl', function($scope, Page, $log, Config, Device, Tag) {
	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;
        });
    };
    $scope.checkLogger();

    $scope.restartLogger = function(){
        var restartLogger = Config.restartLogger();
        restartLogger.then(function(data){
        	console.log(data);
        });
    };

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


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

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

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

	$scope.deleteAllTagData = function(){
		var deleteAllTags = Tag.clearAllTagData();
		deleteAllTags.then(function(data){
			$log.info(data);
		});
	};
});
