'use strict';
/**
     *@class ImagesMainController
     *@description Manipulations with images.
     */
var gImagesMainController = angular.module('images.controllers.gmain', [
    'system'
])
    .controller('gImagesMainController', [
        '$scope',
        '$route',
        '$location',
        'System',
        'ImageService',
        'LibraryService',
        'ClassificationService',
        '$timeout',
        function ($scope, $route, $location, System, ImageService, LibraryService, ClassificationService, $timeout) {
            $(".tooltip").tooltip();

            /**
            * Query object holds entire search data
            */
            $scope.query = $route.current.params || { };

            $scope.sidebarView = 'basic';

            /**
            *@property disableSort
            *@description Indicates  is sorting disabled or not.
            */
            $scope.disableSort = ($scope.query.libraryId ? false : true);


            /**
            *@property isGrid
            *@description Indicates for display images in library or all images.
            */
            $scope.isLibrary = ($scope.query.libraryId ? true : false);

            /**
            * @method isCurrentLibrary
            * @description returns value indicates if selected gallery is same as provided in as argument
            */
            $scope.isCurrentLibrary = function(library) {
                return library.ID == $scope.query.libraryId;
            };

            /**
            * @method isSearch
            * @description returns value indicates if search is runningimages
            */
            $scope.isSearch = function() {
                return Object.keys($scope.query).length !== 0;
            };

            /**
            *@property isGrid
            *@description  Indicates the pictures dispay mode.
            */
            $scope.isGrid = false;

            /**
            *@property isEditMode
            *@description  Indicates whether it is possible to change the title of gallerie.
            */
            $scope.isEditMode = false;

            angular.extend($scope.query, $route.current.params);

          

            /**
            * @method refresh
            * @description Load images by query params.
            */
         
            
            var page = 1;
            $scope.loadMore = function() {
                $scope.ticks = new Date().getTime();
                if ($scope.canLoadMore) {
                    $scope.query.page = ++page;
                    ImageService.query($scope.query)
                        .then(function(data) {
                            $scope.canLoadMore = data.Images.length >= 12;
                            for (var i = 0; i < data.Images.length; i++) {
                                $scope.images.push(data.Images[i]);
                            }
                        });
                }
            };

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

                        timeout = setTimeout(function() {
                            $scope.loadMore();
                           
                        }, 600);
                    }
                }
            };

            /**
            *
            */
            $scope.getSettings = function() {
                ImageService.getSettings()
                    .then(function(data) {
                        $scope.settings = data;
                        console.log($scope.settings);
                        $scope.uploadOption = {
                            extensions: $scope.settings.AllowedExtensions,
                            multiple: false,
                            doneall: function() {
                                $scope.upload.clear();
                                $scope.image = { };
                                $scope.refresh();
                                $scope.dialogEdit.dialog("close");
                                System.Message.success($scope.$resource('utility.settingsSaved'));
                            }
                        };
                    });
            };

            $scope.getSettings();

            /**
            *@property upload
            *@description upload widget will bee initialized here.
            */
            $scope.upload = { };

            /**
            * @method lib
            * @description Loads images for provided library.
            */
            $scope.lib = function(library) {
                $location.url('/images/' + library.ID);
            };

           

            /**
            * @method refresh
            * @description Load libraryes for the user.
            */
            $scope.refresh = function() {
                ImageService.libraries().then(function(data) {
                    $scope.libraries = data;
                    $scope.pager = System.Pager.create({ page: $scope.query.page, size: $scope.settings.GeneralPageSize, total: data.length });
                });
            };

           

            /**
            *@method search
            *@description Search images by params.
            *@param q object with params.
            *@param value indicating if query should be reset before applying new values provided in "q" param.
            */
            $scope.search = function(q, reset) {
                reset ? $scope.query = q : angular.extend($scope.query, q);
                $location.search($scope.query);
            };
            
            /**
            * @method reset
            * @description resets search to its initial value
            */
            $scope.reset = function () {
                $location.search(($scope.query = {}));
            };
            
            $scope.switchSidebar = function (view) {
                $scope.sidebarView = view;
            };

           

            

            /**
            *@method removeLibrary
            *@description Remove library and images form it.
            *@param id Library id.
            */
            $scope.removeLibrary = function(id) {
                var r = confirm($scope.$resource('utility.areYouShure'));
                if (r == true) {
                    ImageService.removeLibrary({ id: id }).then(function (data) {
                        if (data.Errors) {
                            $scope.errors = data.Errors;
                            System.Message.error($scope.$resource('images.not_possible_delete_gallery'));
                        } else {
                            angular.forEach($scope.libraries, function(value, key) {
                                if (value.ID == id) {
                                    System.Message.success($scope.$resource('utility.deleted'));
                                    $scope.libraries.splice(key, 1);
                                    $location.url('/images');
                                }
                            });
                        }
                    });
                }
            };

            /**
           * @method setLockStatus
           * @param id
           * @description Set radnik parametat Locket to true or false. Inidcates whether the radnik can access the system.
           */
            $scope.setLockStatusLista = function (library) {
                ImageService.setLockStatusLista(library.ID).then(function (data) {
                    library.Status = data.Status;

                    if (data.Status == false) {
                        System.Message.success($scope.$resource("utility.changeNotSave"));

                    } else {
                        System.Message.success($scope.$resource("utility.settingsSaved"));
                    }
                });
            };

            $scope.$watch("pager.page", function () {
                if (!$scope.pager || !$scope.pager.page) return;
                $scope.query.page = $scope.pager.page;
                $scope.search($scope.query, false);
            });

            /**
           * @action goToSettings go to location /images/settings
           */
            $scope.goToSettings = function() {
                $location.url('/images/settings');
            };


           
            $scope.refresh();

        }
    ]);