( function($){
    "use strict";

    var prefix                    = 'gpls-wmfw-watermark-image-for-wordpress';
    var prefixUnderscored         = 'gpls_wmfw_watermark_image_for_wordpress';
    var localize_vars             = window[ prefixUnderscored + '_localize_vars' ];
    var classesPrefix             = 'gpls-wmfw';

    $(document).on( 'ready', function() {

        setupFontFileUploader();
        // Upload custom font file.

        $(document).on( 'click', '.available-fonts-toggle', (e) => {
            $('.available-fonts').collapse('toggle');
        });
    });


    function setupFontFileUploader() {
        $('#custom-fonts-file-wrapper').dmUploader({
            url: localize_vars.ajaxUrl,
            extraData: function(id) {
                return {
                    action: localize_vars.uploadFontFileAction,
                    nonce: localize_vars.nonce,
                }
            },
            fieldName: 'file',
            extFilter: ['ttf'],
            dataType: 'json',
            onNewFile: function() {
                resetFontNotice();
            },
            onDragEnter: function() {
                this.addClass('active');
            },
            onDragLeave: function() {
                this.removeClass('active');
            },
            onBeforeUpload: function( id ) {
                $('.main-loader').removeClass('d-none');
            },
            onUploadSuccess: function( id, resp ) {
                if ( resp.data.result === true && resp.data.fonts ) {
                    updateFontsList( resp.data.fonts );
                }
                addFontNotice( resp.data.message, resp.data.status );
            },
            onUploadError: function( id, xhr, status, errorThrown ) {
                addFontNotice( xhr.responseText, 'error' );
            },
            onUploadComplete: function( id ) {
                $('.main-loader').addClass('d-none');
            },
            onFileTypeError: function( file ) {
                addFontNotice( localize_vars.labels.only_ttf, 'error' );
            },
            onFileExtError: function( file ) {
                addFontNotice( localize_vars.labels.only_ttf, 'error' );
            }
        });
    }

    /**
     * Upload Custom Fon File.
     */
    function updateFontsList( fonts ) {
        var fontsList = $('.available-fonts');
        fontsList.empty();
        for ( let font in fonts ) {
            var fontHTML = '<li class="font-item row list-group-item d-flex flex-row"><div class="col-6 font-title">' + fonts[ font ].title + '</div><div class="col-6 font-file-name">' + fonts[ font ].name + '</div></li>';
            fontsList.append( fontHTML );
        }
    }

    function resetFontNotice() {
        var notice = $('.custom-font-file-notice');
        notice.removeAttr('class');
        notice.addClass('d-none custom-font-file-notice notice');
    }

    function addFontNotice( message, type ) {
        resetFontNotice();
        var notice = $('.custom-font-file-notice');
        notice.addClass('notice-' + type );
        notice.removeClass('d-none');
        notice.find('.notice-message').html( message );
    }

})(jQuery);
