'use strict';
/**
 * @controller DocumentUploadController
 * @description Manipulations with documents.
 */
angular.module('documents.controllers.upload', [
    'system'
])
    .controller('DocumentUploadController', [
        '$scope',
        '$route',
        '$location',
        '$timeout',
        'System',
        'DocumentService',
        'LibraryService',
        function($scope, $route, $location, $timeout, System, DocumentService, LibraryService) {

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

            /**
            *@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;
            };

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

            /**
            *@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);
                    });
                } else {
                    $scope.upload.send();
                }
            };

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

            $scope.refreshLibraries();
        }
    ]);