'use strict';

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

GroupListCtrl.resolve = {
    groups: ['$q', 'Group', function ($q, Group) {
        var deferred = $q.defer();

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

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

var GroupCtrl = ['$scope', '$location', 'group', function ($scope, $location, group) {
    $scope.group = group;

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

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

GroupCtrl.resolve = {
    group: ['$q', '$route', 'Group', function ($q, $route, Group) {
        var deferred = $q.defer();

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

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