/* global define */
define([
    'jquery',
    'underscore',
    'oro/translator',
    'routing',
    'oro/datagrid/mass-action',
    'pim/router',
    'oro/messenger',
    'oro/loading-mask',
    'pim/dialog'
],
function($, _, __, Routing, MassAction, router, messenger, LoadingMask, Dialog) {
    'use strict';

    /**
     * Mass delete action
     *
     * @export  oro/datagrid/mass-delete-action
     * @class   oro.datagrid.MassDeleteAction
     *
     * @extends oro.datagrid.MassAction
     */
    return MassAction.extend({

        /** @type oro.Modal */
        errorModal: undefined,

        /** @type oro.Modal */
        confirmModal: undefined,

        /** @type {Object} */
        config: undefined,

        initialize: function(options) {
            this.config = __moduleConfig;

            MassAction.prototype.initialize.apply(this, arguments);
        },

        /**
         * Displays a confirm dialog and mass delete if action is confirmed.
         */
        execute: function() {
                this.getConfirmDialog();
        },

        /**
         * Get view for confirm modal.
         *
         * @param {Object} data
         *
         * @return {oro.Modal}
         */
        getConfirmDialog: function() {
            this.confirmModal = Dialog.confirmDelete(
                __(this.config.confirmLabel),
                __('pim_common.confirm_deletion'),
                this.doMassDelete.bind(this),
                this.getEntityHint(true)
            );

            return this.confirmModal;
        },

        /**
         * Sends request to mass delete items.
         *
         * @param {Object} data
         */
        doMassDelete: function() {
            let actionParameters = this.getActionParameters();
            actionParameters.actionName = this.route_parameters['actionName'];
            actionParameters.gridName = this.route_parameters['gridName'];

            const loadingMask = new LoadingMask();
            loadingMask.render().$el.appendTo($('.hash-loading-mask')).show();
            
            $.ajax({
                method: 'POST',
                contentType: 'application/json',
                url: Routing.generate('wk_dam_pim_enrich_mass_delete_asset'),
                data: JSON.stringify(actionParameters),
            })
            .then(() => {
                Backbone.history.loadUrl();
                messenger.notify(
                    'success', __('Asset deleted')
                );
                
            })
            .fail(() => {
                messenger.notify(
                    'error', __('Erron in deletetion of Asset Mapping')
                );
            })
            .always(() => {
                loadingMask.hide().$el.remove();
            });
        }
    });
}
);
