(function () {
    function PawDocumentViewerComponent($container, settings, fileTypeIcon) {
        this.$container = $container;
        this.settings = settings;
        this.fileTypeIcon = fileTypeIcon;

        if (paw.utils.value.isSet(this.settings)) {
            this.blob = paw.utils.value.isSet(this.settings.blob) ? this.settings.blob : null;
        }

        this.mockup();
        this.$preloader;
    }

    PawDocumentViewerComponent.prototype.mockup = function () {
        let markup = [
            '<div class="paw-preview-mockup-holder">',
            '   <i class="' + this.fileTypeIcon + '"></i>',
            '</div>'
        ];
        this.$container.html(markup.join('\n'));
    };

    PawDocumentViewerComponent.prototype.preloader = function () {
        let markup = [
            '<div class="paw-preview-preloader">',
            '   <i class="fa fa-spinner fa-spin"></i>',
            '</div>'
        ];
        this.$preloader = $(markup.join('\n'));

        this.$container.html(this.$preloader);
    };

    PawDocumentViewerComponent.prototype.preview = function () {
        var self = this;
        this.preloader();
        return this.render().then(function () {
            return self.compilePreview();
        }).catch(function () {
            if(paw.utils.value.isSet(this.blob)){
                self.previewNotAvailable();
            }
            else{
                self.fileNotFound();
            }
        }).finally(function () {
            if (paw.utils.value.isSet(self.settings) && self.settings.controls) {
                self.createControls();
            }
        });
    };

    PawDocumentViewerComponent.prototype.previewNotAvailable = function () {
        var markup = [
            '<div class="paw-preview-not-available">',
            '   <div class="paw-preview-not-available-label"><span>' + this.settings.previewNotAvailableLabel + '</span></div>',
            '   <div class="paw-preview-download"><a href="' + (this.settings.downloadUrl || this.settings.url) + '" onclick="event.stopPropagation();" target="_blank" class="paw-preview-link"><button type="button"><i class="fa fa-download"></i></button></a></div>',
            '</div>'
        ];
        var $msg = $(markup.join('\n'));
        this.$container.html($msg);
    };

    PawDocumentViewerComponent.prototype.fileNotFound = function () {
        this.$container.empty();
        this.$container.addClass('paw-preview-file-not-found')
    };

    PawDocumentViewerComponent.prototype.render = function () {
        var self = this;
        if (paw.utils.value.notSet(this.settings)) {
            return paw.utils.async.promise();
        }
        if (paw.utils.value.isSet(this.settings.url)) {
            return this.loadDocument().then(function (res) {
                if(!paw.utils.isSet(self.blob)){
                    return async.utils.reject();
                }
                return self.renderDocument();
            });
        }
        else if (paw.utils.value.isSet(this.settings.blob)) {
            return self.renderDocument();
        }
        return paw.utils.async.promise();
    };

    PawDocumentViewerComponent.prototype.loadDocument = function () {
        var self = this;
        return paw.utils.async.getFile({
            url: this.settings.url
        }).then(function (payload) {
            self.blob = payload;
        });
    };

    PawDocumentViewerComponent.prototype.createControls = function () {
        if (paw.utils.value.notSet(this.settings.downloadUrl) || this.settings.downloadUrl.length === 0) {
            return;
        }

        let $toolbarHolder = $('<div class="paw-preview-toolbar-holder"></div>');
        let $toolbar = $('<div class="paw-preview-toolbar"></div>');
        let $downloadBtn = $('<a href="' + this.settings.downloadUrl + '" target="_blank" class="paw-preview-toolbar-link"><button type="button"><i class="fa fa-download"></i></button></a>');
        $toolbar.append('<div style="flex-grow: 2;"></div>');
        $toolbar.append($downloadBtn);
        $toolbarHolder.append($toolbar);

        this.$container.parent().prepend($toolbarHolder);
    };

    PawDocumentViewerComponent.prototype.renderDocument = function () {
        // override in child
    };

    PawDocumentViewerComponent.prototype.compilePreview = function () {
        // override in child
    };

    paw.com.DocumentViewerComponent = PawDocumentViewerComponent;
})(paw);