'use strict';
/**
 * Implements directive for qusion.table component.
 */
angular.module('documents.directives.selector', [])
    .directive('qnDocumentsselectora', [
       
          function () {
              return {
                  require: '?ngModel',
                  replace: true,
                  scope: {
                      options: "=Documentsselectora",
                      ui: "&ui",
                      selectedImages: "=ngModel"
                  },
                templateUrl: '/areas/qusion/scripts/templates/documents/selector.html',
                controller: function ($scope, $timeout, DocumentService, LibraryService) {
                    $scope.selectedLibrary = 0;
                    console.log($scope.options);
                    $scope.loadDocuments = function() {
                        $scope.ticks = new Date().getTime();
                        DocumentService.query($scope.query)
                            .then(function(data) {
                                $scope.documents = data.documents;
                                $scope.library = data.Library;
                            });
                    };
                    
                    DocumentService.libraries().then(function (data) {
                                $scope.libraries = data;
                    });
                    
                    //Upload
                    
                    /**
                    *@property addToExistig
                    *@descriptoin Indicates whether to create a new library or to add documents to an existing library.
                    */
                    $scope.addToExistig = false;

                    /**
                    * @property libraryTitle
                    * @description Title for new library. Default is date in the moment of creating it.
                    */
                    var now = new Date();
                    var month = now.getMonth() + 1;
                    $scope.libraryTitle = now.getDate() + '. ' + month + '. ' + now.getFullYear();

                    /**
                    *
                    */
                    $scope.addToExistingLibrary = function () {
                        $scope.addToExistig = $scope.addToExistig == false;
                    };
                    
                    /**
                     *@property upload
                     *@description upload widget will bee initialized here.
                     */
                    $scope.upload = $('#fileupload').upload({
                        extensions: ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'ppt', 'pptx', 'zip']
                    }).data("qusion-upload");


                    /**
                    *@method startUpload
                    *@description Starts upload process.
                    */
                    $scope.startUpload = function () {
                        if ($scope.addToExistig == false) {
                            var library = {
                                Title: $scope.libraryTitle,
                                Type: "Document"
                            };
                            LibraryService.create(library).then(function (value) {
                                $scope.libraryId = value.ID;
                                $scope.libraries.push(value);
                                $scope.addToExistig = true;
                                $timeout(function () {
                                    $scope.upload.send();
                                }, 100);
                                $scope.loadDocuments();
                            });
                        } else {
                            $scope.upload.send();
                            $scope.loadDocuments();
                        }
                    };

                    $scope.ui.dialogSelect = {
                        width: 700,
                        create: function () {
                            $(".ui-widget-header").hide();
                            $scope.selectionComplete = false;
                            $scope.dialogSelect = $(this);
                        },
                        close: function () {
                            if ($scope.selectionComplete == false) {

                            }
                        }
                    };
                    
                    $scope.$watch('selectedLibrary', function (value) {
                       
                        if (value) {
                            if(value != 0)
                                $scope.query = { libraryId: value };
                            else {
                                $scope.query = { };
                            }
                            $scope.selectedDocuments = [];
                            $scope.loadDocuments();
                        }
                    });

                    $scope.cancelSeletion = function () {
                        $scope.selectionComplete = false;
                        $scope.dialogSelect.dialog("close");
                    };
                    
                    $scope.selectDocuments = function () {
                        $scope.dialogSelect.dialog("open");
                    };

                    $scope.loadDocuments();
                },
                link: function (scope, element, attr, ngModel) {
                    scope.selectedDocuments = [];

                    var getValue = function () {
                        var model = scope.$eval(attr.ngModel);
                        return model;
                    };

                    scope.$watch(getValue, function (value) {
                        if(value) {
                            scope.selectedImages = value;
                        }
                    });
                    
                    scope.doneSelection = function () {
                        scope.selectedDocuments = [];
                        
                        for (var i = 0; i < scope.selectedItems.length; i++) {
                            scope.selectedDocuments.push(scope.selectedItems[i].ID);
                        }
                        
                        scope.selectionComplete = true;
                        ngModel.$setViewValue(scope.selectedDocuments);
                        scope.dialogSelect.dialog("close");
                    };
                }
            };
        }
    ]);