'use strict';

var VideoMainController = angular.module('videos.controllers.main', [
    'system'
])
    .controller('VideoMainController', [
        '$scope',
        '$route',
        '$location',
        'System',
        'VideoService',
        'LibraryService',
        'ClassificationService',
        '$timeout',
        function ($scope, $route, $location, System, VideoService, LibraryService, ClassificationService, $timeout) {
            $scope.query = $route.current.params || {};
            $scope.sidebarView = 'basic';
            $(".tooltip").tooltip();
            $scope.disableSort = ($scope.query.libraryId ? false : true);
            $scope.isGrid = false;
            
           
            $scope.isLibrary = ($scope.query.libraryId ? true : false);

            $scope.isCurrentLibrary = function (library) {
                return library.ID == $scope.query.libraryId;
            };
            
            $scope.isSearch = function () {
                return Object.keys($scope.query).length !== 0;
            };
            
            $scope.isEditMode = false;
            
            $scope.refresh = function () {
                $scope.ticks = new Date().getTime();
                VideoService.query($scope.query)
                    .then(function (data) {
                        $scope.isGrid = true;
                        $scope.videos = data.Videos;
                        $scope.library = data.Library;
                       
                        if ($scope.library) {
                            $scope.DisableSort = false;
                        } else {
                            $scope.DisableSort = true;
                        }
                        
                        $scope.canLoadMore = data.Videos.length >= 20;
                    });
                
                ClassificationService.getByAlias("tag")
                   .then(function (data) {
                       $scope.classification = data;
                   });
            }; 
            
            var page = 1;
            $scope.loadMore = function () {
                $scope.ticks = new Date().getTime();  
                if ($scope.canLoadMore) {
                    $scope.query.page = ++page;
                    VideoService.query($scope.query)
                        .then(function (data) {
                            $scope.canLoadMore = data.Videos.length >= 20;
                            for (var i = 0; i < data.Videos.length; i++) {
                                $scope.videos.push(data.Videos[i]);
                            }
                        });
                }
            };

            var timeout;
            $scope.scrollOptions = {
                scrollBottom: function (event, element) {
                    if (element.hasClass('scroller')) {
                        clearTimeout(timeout);                       
                        timeout = setTimeout(function () {
                            $scope.loadMore();
                            $scope.$apply();
                        }, 600);
                    }
                }
            };

            $scope.refreshLibraries = function () {
                VideoService.libraries().then(function (data) {
                    $scope.libraries = data;
                });
            };
            
            $scope.switchSidebar = function (view) {
                $scope.sidebarView = view;
            };
            
            $scope.sortableOptions = {
                connectWith: ".sort-item",
                handle: '.ui-command-drag',
                stop: function (e, ui) {
                    var sorted = [];
                    for (var i = 0; i < $scope.videos.length; i++) {
                        sorted.push($scope.videos[i].ID);
                    }
                    VideoService.organize({ members: sorted }).then(function (data) {
                    });
                },
                disabled: true,
                watchAndRefresh: 'isGrid'
            };
            
            $scope.lib = function (library) {
                $location.url('/videos/' + library.ID);
            };

            $scope.all = function () {
                $location.url('/videos/');
            };
            

            $scope.goToUpload = function () {
                $location.url('/videos/upload');
            };
            

            $scope.goToVideo = function (videoId) {
                $location.url('/video/' + videoId);
            };
            

            $scope.goToSettings = function () {
                $location.url('/videos/settings');
            };
                       

            $scope.searchYouTube = function () {
                VideoService.searchYT({ q: $scope.queryYT }).then(function (response) {
                    $scope.searchedVideo = response;
                });
            };
            

            $scope.search = function (q, reset) {
                reset ? $scope.query = q : angular.extend($scope.query, q);
                $location.search($scope.query);
            };
            

            $scope.reset = function () {
                $location.search(($scope.query = {}));
            };

            $scope.upload = {};
            
            $scope.getSettings = function () {
                VideoService.getSettings()
                    .then(function (data) {
                        $scope.settings = data;
                        $scope.uploadOption = {
                            extensions: $scope.settings.AllowedExtensions.split(','),
                            multiple: false,
                            doneall: function () {
                                $scope.upload.clear();
                                $scope.video = {};
                                $scope.refresh();
                                $scope.videoEdit.dialog("close");
                                System.Message.success($scope.$resource('utility.settingsSaved'));
                            }
                        };
                    });
            };

            $scope.getSettings();
            $scope.refreshLibraries();
            $scope.refresh();
        }
    ]);