'use strict';
/**
 * @class VideoUploadController
 * @description Upoading images.
*/

angular.module('videos.controllers.upload', [
    'system'
])
    .controller('VideoUploadController', [
        '$scope',
        '$route',
        '$timeout',
        '$location',
        'System',
        'VideoService',
        'LibraryService',
        function ($scope, $route, $timeout, $location, System, VideoService, LibraryService) {

            $(".tooltip").tooltip();

            /**
            *@property addToExistig
            *@descriptoin Indicates whether to create a new gallery or add images to an existing.
            */
            $scope.addToExistig = false;

            /**
            *@property libraryTitle
            *@description Title for new library. Default is now date.
            */
            var now = new Date();
            var mm = now.getMonth() + 1;
            $scope.libraryTitle = now.getDate() + '. ' + mm + '. ' + now.getFullYear();

            /**
            *
            */
            $scope.addToExistingLibrary = function () {
                $scope.addToExistig = $scope.addToExistig == false;
            };
            
            /**
            *@property view
            */
            $scope.view = 'upload';

            /**
            *@method changeView
            */
            $scope.changeView = function(view) {
                $scope.view = view;
            };
            
            /**
            *@property upload
            *@description upload widget will bee initialized here.
            */
            $scope.upload = $('#fileupload').upload({
                extensions: ['ogg', 'mp4']
            }).data("qusion-upload");


            /**
            * @method refreshLibraries
            * @description Load libraryes for the user.
            */
            $scope.refreshLibraries = function () {
                VideoService.libraries().then(function (data) {
                    $scope.libraries = data;
                });
            };

            /**
            *@method canSubmit
            *@description Indicates whether it is possible to upload videos.
            */
            $scope.canSubmit = function () {
                if ($scope.addToExistig == false) {

                    if ($scope.libraryTitle == '' || $scope.libraryTitle == 'undefined') {
                        return false;
                    } else {
                        return true;
                    }
                } else {
                    if ($scope.libraryID == '') {
                        return false;
                    } else {
                        return true;
                    }
                }
            };

            /**
            *@method startUpload
            *@description Start upload process.
            */
            $scope.startUpload = function () {
                if ($scope.canSubmit() == true) {
                    if ($scope.addToExistig == false) {

                        var library = {
                            Title: $scope.libraryTitle,
                            Type: "Video"
                        };

                        LibraryService.create(library).then(function (value) {
                            $scope.libraryId = value.ID;
                            $scope.libraries.push(value);
                            $scope.addToExistig = true;
                            $timeout(function () {
                                $scope.upload.send();
                            }, 100);

                        });
                    } else {
                        $scope.upload.send();
                    }
                }
            };

            /**
            *@action done
            *@description Redirect to videos library where videos added.
            */
            $scope.done = function () {
                if ($scope.libraryId) {
                    $location.url('/videos/' + $scope.libraryId);
                } else {
                    $location.url('/videos/');
                }
            };
            
            /*******************************************************
            ******                  YouTube                  *******
            *******************************************************/

            /**
            *
            */
            $scope.openYTSelector = function() {
                $scope.ytSelector.dialog("open");
            };
            
            /**
            *
            */
            $scope.selectedItems = [];
            
            /**
            *
            */
            $scope.videos = [];
            
            /**
            *@method SaveVideos
            */
            $scope.saveYTVideos = function() {
                if ($scope.addToExistig == false) {
                    var library = {
                        Title: $scope.libraryTitle,
                        Type: "Video"
                    };

                    LibraryService.create(library).then(function (value) {
                        $scope.libraryId = value.ID;
                        $scope.libraries.push(value);
                        $scope.addToExistig = true;
                        $timeout(function () {
                            $scope.saveVideos($scope.libraryId);
                        }, 100);

                    });
                } else {
                    $scope.saveVideos($scope.libraryId);
                }
            };

            /**
            *
            */
            $scope.saveVideos = function (library) {
                var videos = {
                    LibraryID: library,
                    Videos: $scope.videos
                };

                VideoService.saveYtVideos(videos).then(function (response) {
                    
                    if ($scope.libraryId) {
                        $location.url('/videos/' + $scope.libraryId);
                    } else {
                        $location.url('/videos/');
                    }
                    System.Message.success($scope.$resource('videos.VideosSaved'));
                });
            };

            /**
            *
            */
            $scope.cancelSeletion = function() {
                $scope.ytSelector.dialog("close");
                $scope.selectedItems= [];
                $scope.searchedVideo = [];
                $scope.queryYT = "";
            };

            /**
            *
            */
            $scope.doneSelection = function() {
                angular.forEach($scope.selectedItems, function (value, key) {
                    var isExist = false;
                    for (var i = 0; i < $scope.videos.length; i++) {
                        if (value.FileName == $scope.videos[i].FileName) {
                            isExist = true;
                            console.log(value);
                        }
                    }
                    if(isExist == false) {
                        $scope.videos.push(value);
                    }
                });
                
                $scope.ytSelector.dialog("close");
                $scope.selectedItems = [];
                $scope.searchedVideo = [];
                $scope.queryYT = "";
            };

            /**
            *
            */
            $scope.removeYTVideo = function(index) {
                $scope.videos.splice(index, 1);
            };

            /**
           *@property ytSelector
           */
            $scope.ui.ytSelector = {
                width: 700,
                create: function () {
                    $scope.ytSelector = $(this);
                }
            };

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

            $scope.refreshLibraries();
        }
    ]);
