'use strict';

var LedgerCtrl = ['$scope', '$routeParams', '$location', '$q', 'dateFilter', 'ledger', 'Ledger', 'Account', function ($scope, $routeParams, $location, $q, dateFilter, ledger, Ledger, Account) {
    $scope.info = ledger;
    $scope.show = function () {
        var id = $scope.info.Ledger.LedgerID;
        var start_date = angular.isDate($scope.info.StartDate) ? dateFilter($scope.info.StartDate, 'dd-MMM-yyyy') : $scope.info.StartDate;
        var finish_date = angular.isDate($scope.info.FinishDate) ? dateFilter($scope.info.FinishDate, 'dd-MMM-yyyy') : $scope.info.FinishDate;
        if (id === $routeParams.id && start_date === $routeParams.StartDate && finish_date === $routeParams.FinishDate) {
            Ledger.get({id: id, StartDate: start_date, FinishDate: finish_date}, function (data) {
                $scope.info = data;
            });
        } else {
            $location.path('/Ledger/' + id).search('StartDate', start_date).search('FinishDate', finish_date);
        }
    };
    $scope.downloadTable = function () {
        var table = $('#gvGrid'),
            html = table.clone().wrap('<div></div>').parent().html();
        html = html.replace(/á/g, '&aacute;');
        window.open('data:application/vnd.ms-excel;charset=UTF-8,' + encodeURIComponent(html));
    };

    if ($routeParams.id) {
        $('#gvGrid').focus();
    } else {
        $('#txtLedger').focus();
    }

    $scope.selected = -1;
    $scope.setSelected = function (index) {
        $scope.selected = index;
    };

    $scope.shortcuts = {
        'up': function () {
            if ($scope.selected > 0) {
                $scope.$apply(function () {
                    $scope.selected = $scope.selected -= 1;
                });
            }
        },
        'down': function () {
            if ($scope.selected < $scope.info.Body.length - 1) {
                $scope.$apply(function () {
                    $scope.selected = $scope.selected += 1;
                });
            }
        },
        'alt+r': function () {
            console.log('alt+r');
        },
        'enter': function () {
            var path = $scope.info.Body[$scope.selected].Url.replace(/^(?:\/\/|[^\/]+)*/, "");
            $scope.$apply(function () {
                $location.path(path).search('StartDate', null).search('FinishDate', null);
            });
        },
        'alt+u': function () {
            console.log('alt+u');
        }
    };

    $scope.accounts = function ($viewValue) {
        var deferred = $q.defer();
        Account.autocomplete({term: $viewValue, count: 20}, function (result) {
            deferred.resolve(result);
        });
        return deferred.promise;
    };
}];

LedgerCtrl.resolve = {
    ledger: ['$q', '$route', 'Ledger', function ($q, $route, Ledger) {
        var deferred = $q.defer();

        var id = $route.current.params.id;
        var start_date = $route.current.params.StartDate;
        var finish_date = $route.current.params.FinishDate;

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

        if (typeof id === 'undefined') {
            Ledger.get({}, successCb);
        } else {
            Ledger.get({id: id, StartDate: start_date, FinishDate: finish_date}, successCb);
        }
        return deferred.promise;
    }]
};


