(function () {
    'use strict';
    angular
        .module('app-base')
        .directive('drupalNode', drupalNode)
        .directive('drupalNodeTeaser', drupalNodeTeaser)
        .directive('drupalMenu', drupalMenu)
        .controller('drupalNodeController', drupalNodeController)

    /** @ngInject */
    function drupalNode($templateCache,AppSettings) {
        var directive = {
            scope:{
                nid:'=',
                template: '='
            },
            link: function (scope, element, attrs) {
               // todo: dynamic node templates
            },
            controller: drupalNodeController,
            controllerAs: 'vm',
            templateUrl: 'app/components/base-app/drupal/views/node.html'
            //templateUrl: AppSettings.drupal.path_to_templates+'node.html'
        };
        return directive;
    }
    /** @ngInject */
    function drupalNodeTeaser(AppSettings) {
        var directive = {
            scope:{
                nid:'='
            },
            controller: drupalNodeController,
            controllerAs: 'vm',
            templateUrl: 'app/components/base-app/drupal/views/node-teaser.html'
        };
        return directive;
    }
    /** @ngInject */
    function drupalMenu() {
        return {
            restrict: "EA",
            scope: {
                data: "=",
                levels: "@",
                base: "@"
            },
            controller: function ($scope) {
                var vm = this;
                $scope.$on('init-drupal',function(){
                    vm.version = Drupal.version;
                });

                $scope.levels = $scope.levels || 2;
                $scope.tree = [];
                $scope.$watch('data', function () {
                    if ($scope.data === undefined) {return; }
                    angular.forEach($scope.data.tree, function (item) {
                        if (!isArray(item.children)) {
                            var children = [];
                            angular.forEach(item.children, function (child) {
                                children.push(child);
                            });
                            item.children = children;
                        }
                        $scope.tree.push(item);
                    });
                });
                var isArray = function (object) {
                    if (object.constructor === Array) { return true }
                };
            },
            controllerAs: 'vm',
            link: function (scope, element) {

            },
            replace:true,
            template: '<ul class="nav menu"><li ng-repeat="item in tree" ng-class="menu-item"><a href="{{base}}{{item.link.href}}">{{item.link.title}}</a>' +
            '<span ng-if="item.children.length > 0 && levels != 1 "><ul><li ng-repeat="item in item.children" ng-class="menu-item"><a ng-link="{{base}}{{item.link.href}}">{{item.link.title}}</a></li></ul></span>' +
            '</li></ul>',

            // fixme:
            // templateUrl: 'app/components/base-app/drupal/views/menu.html'
        }
    }

    /** @ngInject */
    function drupalNodeController($scope,$log,$element,$attrs,$filter,Drupal) {
        var vm = this;
        vm.version = Drupal.version;

        $scope.node = {};

        $scope.$on('init-drupal',function(){
            vm.version = Drupal.version;
        });

        $scope.$watch('nid', function() {
            if($scope.nid == undefined || $scope.nid == ""){
                return;
            }
            getNode($scope.nid);
        });

        function getNode(id){
            if(isNaN(id)) {
                // uuid
                if(Drupal.model.entities[id]) {
                    $scope.node = Drupal.getEntity(id);
                }
            }else{
                // nid
                if (Drupal.model.nodes[id]) {
                    $scope.node = Drupal.model.nodes[id];
                } else {
                    $scope.node = Drupal.node.get({nid: id});
                }
            }
        }

    }
})();
