document.addEventListener('DOMContentLoaded', function () {
    let body = document.body;
    if (!body.classList.contains('upload-php')) { return; }

    /**
     * Get current view
     */
    let viewList = document.querySelector('.table-view-list');
    if (viewList !== null) {
        let bulkActionTop = document.querySelector('#bulk-action-selector-top');
        let bulkActionBottom = document.querySelector('#bulk-action-selector-bottom');

        /**
         * Add option on Top
         */
        const option1 = document.createElement("option");
        option1.value = "mld-download-files"
        option1.innerText = 'Télécharger les fichiers sélectionnés';
        bulkActionTop.appendChild(option1);

        /**
         * Add option on Bottom
         */
        const option2 = document.createElement("option");
        option2.value = "mld-download-files"
        option2.innerText = 'Télécharger les fichiers sélectionnés';
        bulkActionBottom.appendChild(option2);

        // Ajax Call
        let doAction = document.querySelector('#doaction');
        doAction.addEventListener('click', function (e) {

            if ((bulkActionTop.value || bulkActionBottom.value) !== 'mld-download-files') {
                return;
            }

            e.preventDefault();

            let checkedFiles = document.querySelectorAll('#the-list input:checked');
            if (checkedFiles.length === 0) {
                alert('Aucun fichier sélectionné');
                return;
            }

            let selection = new Array();
            for (let index = 0; index < checkedFiles.length; index++) {
                const element = checkedFiles[index];
                selection.push(element.value);
            }

            jQuery.post({
                url: admin.ajax_url,
                data: {
                    action: "download_files",
                    ids: selection,
                },
                success: function (response) {
                    window.location = response.data;
                },
            });
        })
    } else {

        setTimeout(() => {
            let bulkSelect = document.querySelector('.media-toolbar .select-mode-toggle-button');
            
            if (bulkSelect !== null) {
                bulkSelect.setAttribute('aria-selected', 'false');
                bulkSelect.addEventListener('click', function (e) {
                    let existingDownloadButton = document.querySelector('#mld-download');

                    // Remove old button
                    if (e.target.classList.contains('large-button')){
                        existingDownloadButton.remove();
                    }
                    if (existingDownloadButton === null) {
                        let downloadButton = document.createElement('button');
                        downloadButton.id = 'mld-download'
                        downloadButton.type = 'button';
                        downloadButton.setAttribute('disabled', 'disabled');
                        downloadButton.classList = 'button media-button button-primary button-large delete-selected-button';
                        downloadButton.innerText = 'Télécharger';
                        let counter = document.createElement('span');
                        counter.classList = 'int';
                        downloadButton.appendChild(counter);
                        document.querySelector('.media-toolbar-secondary').insertBefore(downloadButton, document.querySelector('.delete-selected-button'))
                    }
                })
            }

            let singleMedias = document.querySelectorAll('.attachments-wrapper .attachments .attachment');
            if (singleMedias !== null) {
                for (let index = 0; index < singleMedias.length; index++) {
                    const singleMedia = singleMedias[index];
                    singleMedia.addEventListener('click', function (e) {
                        let totalCount = document.querySelectorAll('.attachments-wrapper .attachments .attachment[aria-checked="true"]').length;
                        if (totalCount > 0) {
                            document.querySelector('#mld-download').removeAttribute('disabled');
                            document.querySelector('#mld-download .int').innerText = ' (' + totalCount + ')';
                        } else {
                            document.querySelector('#mld-download').setAttribute('disabled', true);
                            document.querySelector('#mld-download .int').innerText = '';
                        }
                    })
                }
            }
        }, 200);

        // Ajax Call
        window.addEventListener('click', function(e){
            let clickedElementID = e.target.id;
            if (clickedElementID !== 'mld-download'){
                return;
            }
            let filesToDownload = new Array();
            let imagesSelected = document.querySelectorAll('.attachments-wrapper .attachments .attachment[aria-checked="true"]');
            if (imagesSelected.length > 0){
                for (let index = 0; index < imagesSelected.length; index++) {
                    const element = imagesSelected[index];
                    let dataID = element.dataset.id;
                    filesToDownload.push(dataID);
                }
                jQuery.post({
                    url: admin.ajax_url,
                    data: {
                        action: "download_files",
                        ids: filesToDownload,
                    },
                    success: function (response) {
                        window.location = response.data;
                    },
                });
            }
        })
    }
})