poconsole.factory('Device', function($q, $http, $log){
    var service = {};
    service.getAllDevices = function(){
        var deferred = $q.defer();
        $http.get('/api/devices').success(function(data) {
            console.log({device:data.objects});
        	deferred.resolve({
    			devices:data.objects
    		});
        });
        return deferred.promise;
    };

    service.getDevice = function(id){
        var deferred = $q.defer();
        $http.get('/api/devices/'+ id).success(function(data) {
        	deferred.resolve({
    			devices:data
    		});
        });
        return deferred.promise;
    };

    service.addDevice = function(d){
        $log.info(d);
        var deferred = $q.defer();
        $http.post('/api/devices', {
            address: d.address,
            device_type_id: d.device_type._id
        }).success(function(data){
            deferred.resolve({
                data:data
            });
        });
        return deferred.promise;
    };

    service.updateDevice = function(d){
        var deferred = $q.defer();
        $http.put('/api/devices/' + d._id, {
            address: d.address,
            device_type: d.device_type._id
        }).success(function(data){
            deferred.resolve({
                data:data
            });
        });
        return deferred.promise;
    };

    service.deleteDevice = function(id){
        var deferred = $q.defer();
        var url = '/api/devices/' + id;
        $http.delete(url).success(function(data) {
            deferred.resolve({
                data:data
            });
        });
        return deferred.promise;
    };

    service.getDeviceTypes = function(){
        var deferred = $q.defer();
        $http.get('/api/device_types').success(function(data) {
        	deferred.resolve({
    			device_types:data.objects
    		});
        });
        return deferred.promise;
    };


    return service;
});
