angular.module('pos', ['ngRoute'])

    .directive('tabs', function () {
        return {
            restrict: 'A',
            transclude: true,
            scope: { style: "@tabs" },
            controller: [ "$scope", function ($scope) {
                $scope.panels = [];

                $scope.select = function (i) {
                    angular.forEach($scope.panels, function (p) {
                        p.selected = false;
                    });
                    $scope.panels[i].selected = true;
                };

                this.addPanel = function (panel) {
                    if ($scope.panels.length === 0) {
                        panel.selected = true;
                    }
                    $scope.panels.push(panel);
                };
            }],
            template: '<div class="tabbable">' +
                '<ul class="nav nav-{{style||\'pills\'}}">' +
                '<li ng-repeat="panel in panels" ng-class="{active:panel.selected}">' +
                '<a href="" ng-click="select($index)">{{panel.title}}</a>' +
                '</li>' +
                '</ul><br/>' +
                '<div class="tab-content" ng-transclude></div>' +
                '</div>',
            replace: true
        };
    })
    .directive('panel', function () {
        return {
            require: '^tabs',
            restrict: 'A',
            transclude: true,
            scope: { title: "@panel" },
            link: function (scope, element, attrs, tabsCtrl) {
                tabsCtrl.addPanel(scope);
            },
            template: '<div class="tab-panel" ng-show="selected" ng-transclude>' +
                '</div>',
            replace: true
        };
    })
    .directive('item', function() {
        return {
            require: 'ngModel',
            link: function(scope, element, attr, mCtrl) {
                function validItem(value) {
                    if (value.match(/^\d{3}-\d{3}$/gi)) {
                        mCtrl.$setValidity('validItem', true);
                    } else {
                        mCtrl.$setValidity('validItem', false);
                    }
                    return value;
                }
                mCtrl.$parsers.push(validItem);
            }
        };
    })


    .filter("euro", function () {
        return function (num) {
            if (num !== null || num === Number.NaN) {
                return parseFloat(num).toFixed(2).toString().replace(".", ",");
            }
            return "0,00";
        };
    })
    .filter("datum", function () {
        return function (d) {
            if (d !== null) {
                return moment(d).format("DD.MM.YYYY HH:mm:ss");
            }
            return "?";
        };
    })
    .filter("datum2", function () {
        return function (d) {
            if (d !== null) {
                return moment(d).format("DD.MM.YYYY");
            }
            return "?";
        };
    })
    .filter('comma2decimal', [
        function() {
            return function(input) {
                var ret=(input)?input.toString().trim().replace(",","."):null;
                return parseFloat(ret);
            };
        }
    ])
    .filter('decimal2comma', [
        function() {
            return function(input) {
                var ret=(input)?input.toString().replace(".",","):null;
                if(ret){
                    var decArr=ret.split(",");
                    if(decArr.length>1){
                        var dec=decArr[1].length;
                        if(dec===1){ret+="0";}
                    }
                }
                return ret;
            };
        }
    ])
    .directive('price', ['$filter',
        function($filter) {
            return {
                restrict:'A',
                require: 'ngModel',
                link: function(scope, element, attrs, ngModelController) {
                    ngModelController.$parsers.push(function(data) {
                        //convert data from view format to model format

                        data=$filter('comma2decimal')(data);

                        return data;
                    });

                    ngModelController.$formatters.push(function(data) {
                        //convert data from model format to view format

                        data=$filter('decimal2comma')(data);

                        return data;
                    });
                }
            };
        }
    ])


    .config(function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'templates/main.html',
                controller: 'MainCtrl'
            })
            .when('/checkout', {
                templateUrl: 'templates/checkout.html',
                controller: 'CheckoutCtrl'
            })
            .when('/dashboard', {
                templateUrl: 'templates/dashboard.html',
                controller: 'DashboardCtrl'
            })
            .when('/invoice', {
                templateUrl: 'templates/invoice.html',
                controller: 'InvoiceCtrl'
            })
            .when('/pos/:id', {
                templateUrl: 'templates/pos.html',
                controller: 'PosCtrl'
            })
            .when('/seller', {
                templateUrl: 'templates/seller.html',
                controller: 'SellerCtrl'
            })
            .when('/templates/:offset', {
                templateUrl: 'templates/templates.html',
                controller: 'TemplatesCtrl'
            })
            .otherwise({
                redirectTo: '/'
            });
    });


String.prototype.padLeft = function(char, length) {
    return char.repeat(Math.max(0, length - this.length)) + this;
};

moment.locale('de');