'use strict';

var UserListCtrl = ['$scope', 'users', function ($scope, users) {
    $scope.info = users;
}]

UserListCtrl.resolve = {
    users: ['$q', 'User', function ($q, User) {
        var deferred = $q.defer();

        var successCb = function (result) {
            deferred.resolve(result);
        };

        User.query({}, successCb);
        return deferred.promise;
    }]
};

var UserCtrl = ['$scope', '$location', 'user', function ($scope, $location, user) {
    $scope.user = user;

    $scope.save = function () {
        $scope.user.$save(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/Users');
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

    $scope.delete = function () {
        $scope.user.$delete(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/Users');
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };
    $('#txtName').focus();
}];

UserCtrl.resolve = {
    user: ['$q', '$route', 'User', function ($q, $route, User) {
        var deferred = $q.defer();

        var id = $route.current.params.id;
        var successCb = function (result) {
            deferred.resolve(result);
        };

        User.get({id: id}, successCb);
        return deferred.promise;
    }]
};
