'user strict';
angular.module('qusion.ui.updateFile', [
])
    .directive('qnUpdatefile', function () {
        return {
            restrict: 'A',
            replate: true,
            link: function ($scope, elem, attr) {

                elem.hide();
                var manualuploader = null;

                var settings = {
                    galleryType: 'Image',
                    validation: {
                        allowedExtensions: ['jpeg', 'jpg', 'gif', 'png'],
                        sizeLimit: 51200000
                    },
                    request: {
                        forceMultipart: true,
                        endpoint: "/api/Image/Update"
                    }
                };

                $scope.$on('startUpdateImage', function (event, image) {
                    manualuploader.setParams({ title: image.Title, ID: image.ID });
                    manualuploader.uploadStoredFiles();
                });

                var getOptions = function () {
                    return angular.extend({}, settings, $scope.$eval(attr.qnUpload));
                };

                var init = function (value) {
                    var options = getOptions();
                    $scope.getLibraries(options.galleryType);
                    if (value) {
                        $scope.galleryType = options.galleryType;
                        if (manualuploader == null) {
                            manualuploader = new qq.FineUploader({
                                element: elem[0],
                                autoUpload: false,
                                request: options.request,
                                multiple: false,
                                validation: options.validation,
                                callbacks: {
                                    onSubmit: function (id, fileName) {
                                        
                                    },
                                    onComplete: function (id, fileName, responseJson) {
                                        if (responseJson.success) {
                                            $scope.$broadcast("closeDialog");
                                        }
                                        
                                    }
                                },
                                template: '<div class="qq-uploader">' +
                                    '<pre class="qq-upload-drop-area"><span>{dragZoneText}</span></pre>' +
                                    '<div class="qq-upload-button btn btn-info" style="width:auto;">{uploadButtonText}</div>' +
                                    '<span class="qq-drop-processing"><span>{dropProcessingText}</span></span>' +
                                    '<ul class="qq-upload-list" style="margin-top: 10px; text-align: center;"></ul>' +
                                  '</div>',
                                fileTemplate: '<li>' +
                                    '<span class="qq-upload-spinner"></span>' +
                                    '<span class="qq-upload-finished"></span>' +
                                    '<span class="qq-upload-file"></span>' +
                                    '<span class="qq-upload-size"></span>' +
                                    '<span class="qq-progress-bar"></span>' +
                                    '<a class="qq-upload-cancel" href="#">{cancelButtonText}</a>' +
                                    '<a class="qq-upload-retry" href="#">{retryButtonText}</a>' +
                                    '<a class="qq-upload-delete" href="#">{deleteButtonText}</a>' +
                                    '<span class="qq-upload-status-text">{statusText}</span>' +
                                    '</li>',
                                debug: false,
                                text: {
                                    uploadButton: '<i class="icon-upload icon-white"></i> Dodaj sliku'
                                },
                                showMessage: function (message) {
                                }
                            });
                            elem.show();
                        }
                    }
                };
                $scope.$watch(getOptions, init, true);
            }
        };

    });

