'use strict';
/**
     *@class ImagesSelectorController
     *@description Selection images.
     */
var ImagesSelectorController = angular.module('images.controllers.selector', [
    'system'
])
    .controller('ImagesSelectorController', [
        '$scope',
        '$route',
        '$location',
        'System',
        'ImageService',
        'LibraryService',
        '$timeout',
        function ($scope, $route, $location, System, ImageService, LibraryService, $timeout) {

            /**
            *
            */
            $scope.images = [];
            
            /**
            *
            */
            $scope.selectedIds = [];
            
            /**
            *
            */
            $scope.refresh = function () {
                $scope.ticks = new Date().getTime();
                ImageService.query($scope.query)
                    .then(function (data) {
                        $scope.isGrid = true;
                        $scope.images = data.Images;
                        $scope.library = data.Library;

                        if ($scope.library) {
                            $scope.DisableSort = false;
                        } else {
                            $scope.DisableSort = true;
                        }
                    });
            };
            
            /**
            *
            */
            $scope.refreshLibraries = function () {
                ImageService.libraries().then(function (data) {
                    $scope.libraries = data;
                });
            };
            
            
            /**
            *
            */
            $scope.selectImages = function () {
                $scope.dialogSelect.dialog("open");
            };

            /**
            *
            */
            $scope.ui.dialogSelect = {
                width: 715,
                title: $scope.$resource('images.choose_images'),
                create: function () {
                    $(".ui-widget-header").hide();
                    $scope.selectionComplete = false;
                    $scope.dialogSelect = $(this);
                },
                close: function () {
                    if ($scope.selectionComplete == false) {
                        $scope.selectedIds = [];

                    }
                }
            };

            /**
            *
            */
            $scope.$watch('selectedLibrary', function (value) {
                if (value) {
                    $scope.query = { libraryId: value };
                    $scope.selectedIds = [];
                    $scope.refresh();
                }
            });

            /**
            *
            */
            $scope.doneSelection = function () {
                $scope.selectionComplete = true;
                $scope.dialogSelect.dialog("close");
            };

            /**
            *
            */
            $scope.cancelSeletion = function () {
                $scope.selectionComplete = false;
                $scope.dialogSelect.dialog("close");
            };

            $scope.refresh();
            $scope.refreshLibraries();


            //if ($file->move($path, $fileName))
            //{
            //    return Response::json(array('filelink' => $path . '/' . $fileName));
            //}

        }
    ]);

