'use strict';

var EmployeeListController = ['$scope', '$location', '$routeParams', 'Tokenizer', 'employees', function ($scope, $location, $routeParams, Tokenizer, employees) {
    $scope.search = $routeParams.q || '';
    $scope.info = employees;

    $scope.$watch('search', function (newValue, oldValue) {
        $scope.filterEmployees(newValue);
    }, true);

    $scope.$on('$destroy', function () {
        Tokenizer.doFilter.cache = {};
    });

    $scope.searchInfo = {
        comparator: {
            'n': {'Col': 'Name', 'Comparator': 'text'},
            'c': {'Col': 'Code', 'Comparator': 'numeric'},
            's': {'Col': 'Salary', 'Comparator': 'numeric'},
            'a': {'Col': 'IsActive', 'Comparator': 'boolean'},
            'des': {'Col': 'Designation', 'Comparator': 'text'},
            'dep': {'Col': 'CostCentre', 'Comparator': 'text'}
        },
        def: 'n',
        sorter: {
            'n': 'Name',
            'c': 'Code',
            's': 'Salary',
            'a': 'IsActive',
            'des': 'Designation',
            'dep': 'CostCentre'
        }
    };
    $scope.filterEmployees = _.debounce(function (q) {
        if (q !== $scope._search) {
            $scope._search = q;
            if (angular.isUndefined(q) || q === '') {
                $location.path('/Employees').search('q', null).replace();
            } else {
                $location.path('/Employees').search({'q': q}).replace();
            }
            $scope.$apply(function () {
                var matches = Tokenizer.parseFilterString(q, $scope.searchInfo);
                $scope.employees = Tokenizer.doFilter(q, $scope.info, matches);

            });
        }
    }, 350);

    $scope.selected = -1;
    $scope.setSelected = function (index) {
        $scope.selected = Math.min(Math.max(0, index), $scope.employees.length - 1);
    };
    $scope.shortcuts = {
        'up': function (e) {
            if ($scope.selected > 0) {
                $scope.$apply(function () {
                    $scope.selected = Math.min(Math.max(0, $scope.selected - 1), $scope.employees.length - 1);
                });
                $("#" + $scope.selected).scrollintoview();
                e.preventDefault();
            }
        },
        'down': function (e) {
            if ($scope.selected < $scope.employees.length - 1) {
                $scope.$apply(function () {
                    $scope.selected = Math.min(Math.max(0, $scope.selected + 1), $scope.employees.length - 1);
                });
                $("#" + $scope.selected).scrollintoview();
                e.preventDefault();
            }
        }
    };
}];

EmployeeListController.resolve = {
    employees: ['Employee', function (Employee) {
        return Employee.query({}).$promise;
    }]
};

var EmployeeController = ['$scope', '$routeParams', '$location', 'asDateFilter', '$modal', 'employee', 'costCentres', function ($scope, $routeParams, $location, asDate, $modal, employee, costCentres) {
    $scope.employee = employee;

    $scope.costCentres = costCentres;

    $scope.save = function () {
        $scope.employee.JoiningDate = asDate($scope.employee.JoiningDate);
        $scope.employee.LeavingDate = asDate($scope.employee.LeavingDate);
        $scope.employee.$save(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: u.Code});
            $location.path('/Employees');
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

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

    $scope.confirm = function () {
        var modalInstance = $modal.open({
            backdrop: true,
            templateUrl: '/template/modal/confirm.html',
            controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
                $scope.title = "Delete Employee";
                $scope.body = "Are you sure? This cannot be undone.";
                $scope.isDelete = true;
                $scope.ok = function () {
                    $modalInstance.close();
                };
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            }]
        });
        modalInstance.result.then(function () {
            $scope.delete();
        });
    };
    $scope.foName = true;
}];

EmployeeController.resolve = {
    employee: ['$route', 'Employee', function ($route, Employee) {
        var id = $route.current.params.id;
        return Employee.get({id: id}).$promise;
    }],
    costCentres: ['CostCentre', function (CostCentre) {
        return CostCentre.query({}).$promise;
    }]
};
