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

var ImageUploadController = angular.module('images.controllers.upload', [
    'system'
])
    .controller('ImageUploadController', [
        '$scope',
        '$route',
        '$timeout',
        '$location',
        'System',
        'ImageService',
        'LibraryService',
        function ($scope, $route, $timeout, $location, System, ImageService, 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 upload
            *@description upload widget will bee initialized here.
            */
            $scope.upload = $('#fileupload').upload({
                extensions: ['gif', 'jpg', 'png']
            }).data("qusion-upload");


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

            /**
            *@method canSubmit
            *@description Indicates whether it is possible to upload pictures.
            */
            $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: "Image",
                            Status: 0
                        };

                        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 images library where images added.
            */
            $scope.done = function () {
                if ($scope.libraryId == 'undefinde') {
                    $location.url('/images/');

                } else {
                    $location.url('/images/' + $scope.libraryId);
                }
            };

            $scope.refreshLibraries();
        }
    ]);
