jQuery(document).ready(function($) {
    var pluginUrl = yvpd_vars.yvpd_url;
    var globalConf = window.vpdGlobalConfig || {};
    var autoSkinEnabled = (yvpd_vars.auto_skin === '1');
    function initVPDPlayer($el) {
        var playerID = $el.attr('id');
        if (!playerID || videojs.getPlayer(playerID)) return;
        var attrData = $el.attr('data-settings');
        var localSettings = attrData ? JSON.parse(attrData) : {};
        var settings = $.extend({}, globalConf, localSettings);
        settings.t_play = 1;
        settings.t_big_play = 1;
        settings.t_progress = 1;
        var originalSrc = $el.find('source').attr('src') || $el.attr('src') || '';
        $el.removeAttr('controls');
        $el.attr('crossorigin', 'anonymous');
        var options = {
            controls: true,
            autoplay: false,
            loop: false,
            muted: false,
            fluid: true,
            aspectRatio: '16:9',
            playbackRates: [1],
            bigPlayButton: true,
            techOrder: ['html5'],
            controlBar: { children: [] } 
        };
        var player = videojs(playerID, options); // <- ESTA ES LA LÍNEA QUE DEBES BUSCAR

        // --- AGREGA ESTE BLOQUE JUSTO DEBAJO ---
        // Verifica si t_click_play está activado en los ajustes. 
        // Si es 0 (como forzamos en la versión gratis), desactiva la interacción de clic en el video.
        if (settings.t_click_play !== 1) {
            player.on('loadedmetadata', function() {
                var tech = player.tech({ IWillNotUseThisInPlugins: true });
                if (tech && tech.el()) {
                    tech.el().style.pointerEvents = 'none';
                }
            });
        }
        // --- FIN DEL BLOQUE A AGREGAR ---
        player.on('fullscreenchange', function() {
            if (player.isFullscreen()) {
                player.userActive(true); 
                var resizeInterval = setInterval(function() {
                    window.dispatchEvent(new Event('resize'));
                }, 100); 
                setTimeout(function() {
                    clearInterval(resizeInterval);
                }, 600);
            }
        });
        player.on('userinactive', function() {
            $(player.controlBar.el()).css({
                'opacity': '1',
                'visibility': 'visible',
                'pointer-events': 'auto',
                'transition': 'opacity 0.2s ease, visibility 0.2s ease'
            });
        });
        player.one('loadedmetadata', function() {
            if (this.videoHeight() > this.videoWidth()) {
                player.addClass('yvpd-is-short');
                $el.closest('.yvpd-player-wrapper').addClass('yvpd-wrapper-short');
            }
        });
        player.ready(function() {
            var cb = player.controlBar;
            cb.addChild('playToggle', {});
            cb.addChild('progressControl', {}); 
            cb.addChild('remainingTimeDisplay', {});
        });
    }
    $('.video-js').each(function() { initVPDPlayer($(this)); });
    if (autoSkinEnabled) {
        $('video:not(.no-mejs)').not('.wp-video-shortcode').each(function() {
            var $nat = $(this);
            var src = $nat.attr('src') || $nat.find('source').attr('src');
            if (src) {
                var currentHost = window.location.hostname;
                try {
                    var videoUrl = new URL(src, window.location.href);
                    if (videoUrl.hostname && videoUrl.hostname !== currentHost) return;
                } catch(e) {}
                if ($nat.closest('.yvpd-player-wrapper').length) return;
                var id = 'yvpd_auto_' + Math.floor(Math.random() * 9999);
                var forcedStyle = '';
                if (globalConf.global_width && globalConf.global_unit) {
                    forcedStyle = 'width: ' + globalConf.global_width + globalConf.global_unit + ' !important;';
                    if (globalConf.global_unit === 'px') forcedStyle += ' max-width: 100%;';
                }
                var $w = $('<div class="yvpd-player-wrapper yvpd-auto-forced" style="' + forcedStyle + '"></div>');
                var masterStyle = $('.yvpd-player-wrapper[style*="width"]').first().attr('style');
                if (masterStyle) $w.attr('style', masterStyle);
                var settingsString = JSON.stringify(window.vpdGlobalConfig || {});
                var $vid = $('<video id="' + id + '" class="video-js vjs-default-skin no-mejs" preload="metadata" crossorigin="anonymous"></video>');
                $vid.attr('data-settings', settingsString);
                $vid.append('<source src="' + src + '" type="video/mp4">');
                if ($nat.is('[autoplay]')) $vid.attr('autoplay', '');
                if ($nat.is('[loop]')) $vid.attr('loop', '');
                if ($nat.is('[muted]')) $vid.attr('muted', '');
                if ($nat.attr('poster')) $vid.attr('poster', $nat.attr('poster'));
                var $block = $nat.closest('.wp-block-video');
                if ($block.length) {
                    if (forcedStyle) $block.attr('style', forcedStyle);
                    $block.replaceWith($w.append($vid));
                } else {
                    $nat.replaceWith($w.append($vid));
                }
                initVPDPlayer($vid);
            }
        });
    }
    document.addEventListener('mousemove', function(e) {
        if (typeof videojs !== 'undefined') {
            var players = videojs.getAllPlayers();
            for (var i = 0; i < players.length; i++) {
                if (players[i].isFullscreen && players[i].isFullscreen()) {
                    players[i].userActive(true);
                }
            }
        }
    }, true);
    document.addEventListener('touchstart', function(e) {
        if (typeof videojs !== 'undefined') {
            var players = videojs.getAllPlayers();
            for (var i = 0; i < players.length; i++) {
                if (players[i].isFullscreen && players[i].isFullscreen()) {
                    players[i].userActive(true);
                }
            }
        }
    }, true);
});