angular.module('stoute.ng-users-editor', ['stoute.ng-baseApp','xeditable'])

    .directive('usersEditor',function() {
        return {
            restrict: 'E',
            link: function() { },
            //templateUrl: AppSettings.path_to_templates + '/ng-user.html',
            templateUrl: 'app/components/base-app/ng-users-editor/ng-users-editor.html',
            controller: "UsersEditorCtrl",
            scope:{
                fields:'@',
                onSave:'=',
                onDelete:'=',
                onCancel:'='
            },
            transClude:true
        };
    })

    /** @ngInject */
    .controller('UsersEditorCtrl', function($filter,$log,$scope,$log,$rootScope,$translate,AppService,AppSettings) {
        $scope.users = AppSettings.db.users;
        $scope.roles = [];
        angular.forEach(AppSettings.userRoles,function(value){
            $scope.roles.push({value: value, text: value});
        });
        $scope.showRole = function(user) {
            var selected = [];
            if(user.role) {
                selected = $filter('filter')($scope.roles, {value:user.role});
            }
            return selected.length ? selected[0].text : 'Not set';
        };

        $scope.checkName = function(data) {
            if(data === ''){
                alert('name is required');
            }
        };

        $scope.saveUser = function(data,id) {
            $log.log('saveUser');
            var existing;
            for(var k in AppSettings.db.users){
                if(AppSettings.db.users[k].id===id){
                    existing = true;
                    for(var kk in data){
                        AppSettings.db.users[k][kk]=data[kk];
                    }
                }
            }
            if(!existing){
                //fixme
                AppSettings.db.users.push(data);
            }
            AppSettings.db.$save().then(function(){
                $log.log('user saved');
            });
        };

        // add user
        $scope.addUser = function() {
            $scope.inserted = {
                name: '',
                role: null
            };
            $scope.users.push($scope.inserted);
        };

        // remove user
        $scope.removeUser = function(index) {
            var deleteItem = function(){
                AppSettings.db.users.splice(index, 1);
                $scope.users.splice(index, 1);
                AppSettings.db.$save().then(function(){
                    $log.log('removed user: index ='+index);
                });
            };
            $translate('CONFIRM').then(function(tr){
                var msg1 = tr;
                $translate('CONFIRM MESSAGE').then(function(tr2){
                    var msg2 = tr2;
                    $scope.showConfirm(msg1,msg2,deleteItem);
                });
            });
        };

       // $scope.showConfirm = function(title,template,confirmHandler) {
        $scope.showConfirm = function() {
            $log.log('fixme: users editor showConfirm');
            //var confirmPopup = $ionicPopup.confirm({
            //    title: title,
            //    template: template
            //});
            //confirmPopup.then(function(res) {
            //    if(res) {
            //        confirmHandler.call();
            //    } else {
            //        console.log('You are not sure');
            //    }
            //});
        };


    })

;