'use strict';

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

    $scope.$watch('search', function (value) {
        $scope.filterAccounts(value);
    }, true);

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

    $scope.searchInfo = {
        comparator: {
            'n': {'Col': 'Name', 'Comparator': 'text'},
            't': {'Col': 'Type', 'Comparator': 'text'},
            'a': {'Col': 'IsActive', 'Comparator': 'boolean'},
            'r': {'Col': 'IsReconcilable', 'Comparator': 'boolean'},
            'c': {'Col': 'CostCentre', 'Comparator': 'text'}
        },
        def: 'n',
        sorter: {
            'n': 'Name',
            't': 'Type',
            'a': 'IsActive',
            'r': 'IsReconcilable',
            'c': 'CostCentre'
        }
    };
    $scope.filterAccounts = _.debounce(function (q) {
        if (q !== $scope._search) {
            $scope._search = q;
            if (angular.isUndefined(q) || q === '') {
                $location.path('/Accounts').search('q', null).replace();
            } else {
                $location.path('/Accounts').search({'q': q}).replace();
            }
            $scope.$apply(function () {
                var matches = Tokenizer.parseFilterString(q, $scope.searchInfo);
                $scope.accounts = Tokenizer.doFilter(q, $scope.info, matches);

            });
        }
    }, 350);

    $scope.selected = -1;
    $scope.setSelected = function (index) {
        $scope.selected = Math.min(Math.max(0, index), $scope.accounts.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.accounts.length - 1);
                });
                $("#" + $scope.selected).scrollintoview();
                e.preventDefault();
            }
        },
        'down': function (e) {
            if ($scope.selected < $scope.accounts.length - 1) {
                $scope.$apply(function () {
                    $scope.selected = Math.min(Math.max(0, $scope.selected + 1), $scope.accounts.length - 1);
                });
                $("#" + $scope.selected).scrollintoview();
                e.preventDefault();
            }
        }
    };
}];
AccountListController.resolve = {
    accounts: ['Account', function (Account) {
        return Account.query({}).$promise;
    }]
};

var AccountController = ['$scope', '$location', '$modal', 'account', 'accountTypes', 'costCentres', function ($scope, $location, $modal, account, accountTypes, costCentres) {
    $scope.account = account;

    $scope.accountTypes = accountTypes;
    $scope.costCentres = costCentres;

    $scope.save = function () {
        $scope.account.$save(function () {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/Accounts');
        }, function (data) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

    $scope.delete = function () {
        $scope.account.$delete(function () {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/Accounts');
        }, function (data) {
            $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 Account";
                $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;
}];

AccountController.resolve = {
    account: ['$route', 'Account', function ($route, Account) {
        var id = $route.current.params.id;
        return Account.get({id: id}).$promise;
    }],
    accountTypes: ['AccountType', function (AccountType) {
        return AccountType.query({}).$promise;
    }],
    costCentres: ['CostCentre', function (CostCentre) {
        return CostCentre.query({}).$promise;
    }]
};
