import MediaUpload from "../../Utils/MediaUpload";

class SoundsCpt {
    init() {
        if (!jQuery('#upload_sound_btn')[0]) {
            return;
        }
        this._handlePlaylists();
        this._handleEvents();
    }

    _handleEvents() {
        const _this = this;
        jQuery('#upload_sound_btn').on('click', function(event) {
            event.preventDefault();

            MediaUpload.upload(false, function(attachments) {
                if (attachments.length > 0) {
                    const { id, filename, url } = attachments[0];
                    jQuery('#sound_id').val(id);
                    jQuery('.sound_file_name').html(filename);
                    jQuery('.sound_file_name').attr('url', url);
                }
                _this._handlePopulate();
            }, window?.OXYLEAF_ADMIN_SETTINGS?.labels?.select_background_sound, ['audio'], window?.OXYLEAF_ADMIN_SETTINGS?.labels?.add_background_sound);
        });

        jQuery('#remove_sound_btn').on('click', function(event) {
            event.preventDefault();
            if (confirm(window?.OXYLEAF_ADMIN_SETTINGS?.labels?.deleted_background_sound)) {
                jQuery('#sound_id').val('');
                jQuery('.sound_file_name').html('');
                _this._handlePopulate();
            }
        })

        jQuery('#upload_sound_icon_btn').on('click', function(event) {
            event.preventDefault();

            MediaUpload.upload(false, function(attachments) {
                if (attachments.length > 0) {
                    const { id, filename, url } = attachments[0];
                    console.log('attachments', attachments[0]?.sizes?.thumbnail?.url);
                    jQuery('#icon_id').val(id);
                    jQuery('.sound_icon_filename').html(filename);
                    jQuery('.sound_icon_filename').attr('url', url);
                    jQuery('.icon_preview_ui img').attr('src', url);
                    jQuery('.icon_preview_ui').show();
                }
                _this._handlePopulateIcon();
            }, window?.OXYLEAF_ADMIN_SETTINGS?.labels?.select_sound_image, ['image'], window?.OXYLEAF_ADMIN_SETTINGS?.labels?.add_sound_image);
        });

        jQuery('#remove_sound_icon_btn').on('click', function(event) {
            event.preventDefault();
            if (confirm(window?.OXYLEAF_ADMIN_SETTINGS?.labels?.deleted_background_sound)) {
                jQuery('#icon_id').val('');
                jQuery('.sound_icon_filename').html('');
                _this._handlePopulateIcon();
            }
        })
        this._handlePopulateIcon();
    }

    _handlePopulate() {
        const sound_id = jQuery('#sound_id').val();
        if (sound_id && sound_id !== '') {
            jQuery('#remove_sound_btn').removeClass('d-none');
            jQuery('.sound_file_name_ui').removeClass('d-none');
        } else {
            jQuery('#remove_sound_btn').addClass('d-none');
            jQuery('.sound_file_name_ui').addClass('d-none');
        }
    }

    _handlePopulateIcon() {
        const icon_id = jQuery('#icon_id').val();
        if (icon_id && icon_id !== '') {
            jQuery('#remove_sound_icon_btn').removeClass('d-none');
            jQuery('.sound_icon_filename_ui').removeClass('d-none');
            jQuery('.icon_preview_ui').show();
        } else {
            jQuery('#remove_sound_icon_btn').addClass('d-none');
            jQuery('.sound_icon_filename_ui').addClass('d-none');
            jQuery('.icon_preview_ui').hide();
        }
    }

    _handlePlaylists() {
        jQuery('.playlist-select').select2({
            placeholder: window?.OXYLEAF_ADMIN_SETTINGS?.labels?.select_playlists,
            allowClear: true,
        });
    }
}
export default SoundsCpt;