'use strict';
/**
 * Implements directive for qusion.table component.
 */
angular.module('videos.directives.selector', [])
    .directive('qnVideosselector', [
       
        function () {
            return {
                require: '?ngModel',
                replace: true,
                scope: {
                    options: "=qnVideosselector",
                    ui: "&ui"
                },
                templateUrl: '/areas/qusion/scripts/templates/videos/selector.html',
                controller: function ($scope, VideoService) {
                    
                    $scope.selectedLibrary = 0;

                    $scope.loadVideos = function() {
                        $scope.ticks = new Date().getTime();
                        VideoService.query($scope.query)
                            .then(function(data) {
                                $scope.videos = data.Videos;
                                $scope.library = data.Library;
                            });
                    };
                    
                    VideoService.libraries().then(function (data) {
                                $scope.libraries = data;
                            });

                    $scope.ui.dialogSelect = {
                        width: 715,
                        create: function () {
                            $(".ui-widget-header").hide();
                            $scope.selectionComplete = false;
                            $scope.dialogSelect = $(this);
                        },
                        close: function () {
                            if ($scope.selectionComplete == false) {
                                $scope.selectedVideos = [];
                            }
                        }
                    };
                    
                    $scope.$watch('selectedLibrary', function (value) {
                       
                        if (value) {
                            $scope.query = { libraryId: value };
                            $scope.selectedVideos = [];
                            $scope.loadVideos();
                        }
                    });
                    

                    
                    $scope.cancelSeletion = function () {
                        $scope.selectionComplete = false;
                        $scope.dialogSelect.dialog("close");
                    };
                    
                    $scope.selectVideos = function () {
                        $scope.dialogSelect.dialog("open");
                    };
                    
                    $scope.$watch('selectedLibrary', function (value) {
                        if (value) {
                            $scope.query = { libraryId: value };
                            $scope.selectedVideos = [];
                            $scope.loadVideos();
                        }
                    });
                    
                    $scope.selectOptions = {
                        multipleSeletcion: true,
                    };

                    $scope.loadVideos();
                },
                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.selectedVideos = [];
                        
                        for (var i = 0; i < scope.selectedItems.length; i++) {
                            scope.selectedVideos.push(scope.selectedItems[i].ID);
                        }
                        
                        scope.selectionComplete = true;
                        ngModel.$setViewValue(scope.selectedVideos);
                        scope.dialogSelect.dialog("close");
                    };
                }
            };
        }
    ]);