/*
 * Copyright (C) 2015 IRSTEA
 * All rights reserved.
 */
(function($) {

    var formatFileSize = function(size, precision) {
            if(typeof(precision) === "undefined") {
                precision = 2;
            }
            if(size > 1000000000) {
                return (size/1000000000).toFixed(precision) + ' Gio';
            }
            if(size > 1000000) {
                return (size/1000000).toFixed(precision) + ' Mio';
            }
            if(size > 1000) {
                return (size/1000).toFixed(precision)+ ' Kio';
            }
            return size + ' o';
        },
        formatBitrate = function(rate) {
            return formatFileSize(rate) + '/s';
        };

    $.blueimp.fileupload.prototype._formatFileSize = formatFileSize;
    $.blueimp.fileupload.prototype._formatBitrate = formatBitrate;

    /** Plugin irsteaFileUpload.
     */
    $.fn.irsteaFileUpload = function(options) {

        var $this             = $(this),
            $button           = $this.find('.fileinput-button'),
            $entries          = $this.find('.fileinput-entries'),
            nextIndex         = $entries.find('.fileinput-entry').length,
            createUrl         = options.createUrl,
            uploadPrototype   = options.uploadPrototype,
            downloadPrototype = options.downloadPrototype,
            csrfQuery         = '?token=' + options.csrfToken;

        delete options.createUrl;
        delete options.uploadPrototype;
        delete options.downloadPrototype;
        delete options.csrfToken;

        if(options.acceptFileTypes) {
            options.acceptFileTypes = new RegExp('^' + options.acceptFileTypes + '$');
        }

        var showError = function(entry, message) {
                var $this = $(entry);
                if(message) {
                    $this.find('.error').text(message);
                }
                $this.addClass('alert alert-danger');
                $this.find('.error').show();
                $this.find('.icon')
                    .removeClass('fa-circle-o-notch fa-spin fa-file-o')
                    .addClass('fa-exclamation-triangle');
            },
            updateDisplay = function() {
                var hasEntry = false;
                $entries.find('.fileinput-entry').each(function() {
                    var data = $(this).data('data');
                    if(data && data.files.error) {
                        showError(this, data.files[0].error);
                    }
                    hasEntry = true;
                });
                $button.toggle(options.multiple || !hasEntry);
            };

        $this.find('.size').each(function() {
            $(this).text(formatFileSize($(this).text(), $(this).data('size-precision')));
        });

        updateDisplay();

        // Activation
        $button.fileupload($.extend(
            options,
            {
                type: 'PUT',
                autoUpload: true,
                formData: {},
                multipart: false,
                uploadTemplateId: null,
                downloadTemplateId: null,
                filesContainer: $this.find('.fileinput-entries'),
                dropZone: $this,
                uploadTemplate: function(data) {
                    var rows = $();
                    $.each(data.files, function (index, file) {
                        var row = $(uploadPrototype);
                        row.find('.name').text(file.name);
                        row.find('.size').text(formatFileSize(file.size));
                        if (file.error) {
                            showError(row, file.error);
                        }
                        rows = rows.add(row);
                    });
                    return rows;
                },
                downloadTemplate: function(data) {
                    var rows = $();
                    $.each(data.files, function (index, file) {
                        var row = $(downloadPrototype.replace(/__index__/g, nextIndex++));
                        row.find('.size').text(formatFileSize(file.size));
                        if (file.error) {
                            row.find('.name').text(file.name);
                            showError(row, file.error);
                        } else {
                            row.find('.name a')
                                .text(file.name)
                                .prop('href', file.url + csrfQuery);
                            row.find('.delete')
                                .attr('data-type', file.delete_type)
                                .attr('data-url', file.delete_url + csrfQuery);
                            row.find('input.id')
                                .val(file.id);
                            if(file.icon && file.icon !== 'file') {
                                row.find('.icon')
                                    .removeClass('fa-file-o')
                                    .addClass('fa-file-'+file.icon+'-o');
                            }
                        }
                        rows = rows.add(row);
                    });
                    return rows;
                },
                submit: function (e, data) {
                    var $this = $(this),
                        file = data.files[0];
                    $.post(
                        createUrl,
                        { file: { name: file.name, size: file.size, type: file.type, lastModified: file.lastModified } },
                        function(response) {
                            file.icon = response.icon;
                            data.url = response.put_url + csrfQuery;
                            data.delete_url = response.delete_url;
                            data.delete_type = response.delete_type;
                            data.jqXHR = $this.fileupload('send', data);
                        }
                    )
                        .fail(function(jqXHR, textStatus, errorThrown) {
                            file.error = textStatus === "error" ? errorThrown : ('Error #' + jqXHR.status);
                            data.files.error = true;
                            showError(data.context, file.error);
                        })
                        .always(updateDisplay);
                    return false;
                },
                progress: function (e, data) {
                    if(!data.context || e.isDefaultPrevented()) {
                        return;
                    }
                    var percent = data.loaded / data.total * 100,
                        percentText = percent.toFixed(1);
                    data.context.each(function () {
                        var $this = $(data.context);
                        $this.find('.progress').show();
                        $this.find('.progress-bar').css('width', percent + '%').attr('aria-valuenow', percentText);
                        $this.find('.progress-text').show().html(percentText + '% ('+formatBitrate(data.bitrate)+')');
                    });
                },
                getFilesFromResponse: function(data) {
                    var files = [];
                    $.each(data.files, function(index, file) {
                        files.push($.extend(file, data.result.files[index]));
                    });
                    return files;
                },
            }
        )).bind({
            fileuploadfailed: function (e, data) {
                if(data.delete_url) {
                    $.ajax(data.delete_url + csrfQuery, { type: data.delete_type });
                }
            },
            fileuploadadded: updateDisplay,
            fileuploadfinished: updateDisplay,
            fileuploaddestroyed: updateDisplay,
            fileuploadprocessalways: updateDisplay
        });

        if(options.disabled || options.readonly) {
            $button.fileupload('disable');
        }

    };

})(jQuery);