(function () {
    'use strict';
    angular
        .module('app-base')
        .directive('itemEditor', itemEditor);

    /** @ngInject */
    function itemEditor(){
        var directive = {
            controller: itemEditorController,
           // controllerAs: 'vm',
            templateUrl: 'app/components/base-app/item-editor/item-editor.html',
            scope:{
                item:'=',
                collection:'=',
                onSave:'=',
                onDelete:'=',
                onCancel:'='
            },
            transClude:true
        };
        return directive;
    }

    /** @ngInject */
    function itemEditorController($scope,$rootScope,AppSettings,$translate){

            console.log('ItemEditorController');

            $scope.render = function(){
                $scope.itemCopy = angular.copy($scope.item);
                $scope.fields = [];
                for(var k in $scope.itemCopy){
                    var exclude = false;
                    for(var kk in AppSettings.excludedFields){
                        if(AppSettings.excludedFields[kk] === k){
                            exclude = true;
                        }
                    }
                    if(exclude!==true){
                        //  if(typeof($scope.itemCopy[k])==='string' && k.match('$$') === false){
                        $scope.fields.push({key:k,value:$scope.itemCopy[k]});
                        // }
                    }
                }
            };
            $scope.$watch('item',function(){
                $scope.render();
            });

            $scope.save = function(){
                for(var k in $scope.fields){
                    $scope.item[$scope.fields[k].key] = $scope.itemCopy[$scope.fields[k].key]; // fixme:
                }
                // fixme: set with attr
                AppSettings.db.$save().then(function(){
                    //$scope.onSave.call();
                    angular.element('#itemEditModal').modal('hide');
                });
            };
            $scope.cancel = function(){
                angular.element('#itemEditModal').modal('hide');
                //window.history.go(-1);
                // $scope.onCancel.call();
            };
            $scope.delete = function(){
                var deleteItem = function(){
                    for(var i = $scope.collection.length - 1; i >= 0; i--) {
                        if($scope.collection[i] === $scope.item) {
                            $scope.collection.splice(i, 1);
                        }
                    }
                    AppSettings.db.$save().then(function(){
                        console.log('item deleted');
                        angular.element('#itemEditModal').modal('hide');
                    });
                };

                $translate('CONFIRM').then(function(tr){
                    deleteItem();
                    //var msg1 = tr;
                    //$translate('CONFIRM MESSAGE').then(function(tr2){
                    //    var msg2 = tr2;
                    //    $scope.showConfirm(msg1,msg2,deleteItem);
                    //});
                });
            };
            // todo: showConfirm
            $scope.showConfirm = function() {
                //$scope.showConfirm = function(title,template,confirmHandler) {
                //var confirmPopup = $ionicPopup.confirm({
                //    title: title,
                //    template: template
                //});
                //confirmPopup.then(function(res) {
                //    if(res) {
                //        confirmHandler.call();
                //    } else {
                //        console.log('You are not sure');
                //    }
                //});
            };
    }
})();
