'use strict';
/**
 * 
 */
angular.module('qusion.ui.treeselector', [])
    .directive('qnTreeselector', [
        function () {
            return {
                require: '?ngModel',
                replace: true,
                scope: {
                    options: "=qnTreeselector",
                    data: "=qnTreedata",
                    selected: '=ngModel',
                    ui: "&ui",
                    treeOptions: "&treeOptions"
                },
                templateUrl: '/areas/qusion/scripts/templates/pages/selector.html',
                controller: function ($scope) {
                    $scope.selectedPage = [];
                    $scope.treeObject = null;
                    var selected, selectedPage;
                    $scope.treeOptions = {
                        checkbox: true,
                        selectMode: 1,
                        onPostInit: function (dtnode) {
                            var selectedNodes =this.getSelectedNodes(true);
                            
                            selectedPage = $.map(selectedNodes, function (node) {
                               return node.data.title;
                            });
                           
                            $scope.selectedPage = angular.copy(selectedPage);
                           
                        },
                        onSelect: function (flag, node) {
                            var selectedNodes = node.tree.getSelectedNodes();

                            selectedPage = $.map(selectedNodes, function(node) {
                                return node.data.title;
                            });
                            
                            selected = $.map(selectedNodes, function (node) {
                                return node.data.key;
                            });
                            
                            if ($scope.treeOptions.selectMode == 1) {
                                selected = selected[0];
                            }
                        },
                    };

                    angular.extend($scope.treeOptions, $scope.options);


                    $scope.ui.dialogSelect = {
                        width: 700,
                        title: $scope.options.dialogTitle || 'Select page(s)',
                        create: function () {
                            $scope.dialogSelect = $(this);
                        },
                        close: function () {
                            if ($scope.selectionComplete == false) {

                            }
                        }
                    };

                    var preparing = function (data) {

                        
                        angular.forEach(data, function (value, key) {
                            if ($.isArray($scope.selected)) {
                                angular.forEach($scope.selected, function (v, k) {
                                    if (value.key == v) {
                                        value.select = true;
                                    }
                                });
                               
                            }else {
                                if (value.key == $scope.selected) {
                                    value.select = true;
                                    
                                }
                            }
                           
                            if (value.key == $scope.options.unselectable) {
                                value.unselectable = true;
                                value.hideCheckbox = true;
                            }

                            if (value.children) {
                                preparing(value.children);
                            }
                            
                        });
                    };
                    

                    $scope.selectPages = function () {
                        $scope.dialogSelect.dialog("open");
                    };
                    
                    $scope.doneSelection = function () {
                        $scope.selectedPage = angular.copy(selectedPage);
                        $scope.selected =  angular.copy(selected);
                        $scope.dialogSelect.dialog("close");
                    };
                    
                    $scope.cancelSeletion = function () {
                        $scope.dialogSelect.dialog("close");
                    };

                    $scope.$watch('data', function (value) {
                        if(value) {
                            $scope.strane =$scope.data;
                            preparing($scope.strane);
                            $scope.treeOptions.children = $scope.strane;
                        }
                    });
                    
                   
             },
                link: function (scope, element, attr, ngModel) {
                    
                    
                    
                   
                }
            };

        }
    ]);
