/**
 * Sticky Footer Player
 *
 * Self-contained player with its own <audio> element.
 * Visible on page load. Close removes it for this page load only.
 * Controls (volume, speed, download) rendered conditionally by PHP.
 */
(function() {
    'use strict';

    var stickyEl = document.getElementById('mementor-tts-sticky-footer');
    if (!stickyEl) return;

    var audio     = stickyEl.querySelector('audio');
    if (!audio) return;

    var playBtn   = stickyEl.querySelector('.mementor-tts-sticky-play');
    var iconPlay  = stickyEl.querySelector('.mementor-tts-sticky-icon-play');
    var iconPause = stickyEl.querySelector('.mementor-tts-sticky-icon-pause');
    var timeEl    = stickyEl.querySelector('.mementor-tts-sticky-time');
    var durationEl = stickyEl.querySelector('.mementor-tts-sticky-duration');
    var progressBar = stickyEl.querySelector('.mementor-tts-sticky-progress-bar');
    var progressContainer = stickyEl.querySelector('.mementor-tts-sticky-progress-container');
    var closeBtn  = stickyEl.querySelector('.mementor-tts-sticky-close');
    var volumeBtn = stickyEl.querySelector('.mementor-tts-sticky-volume');
    var speedBtn  = stickyEl.querySelector('.mementor-tts-sticky-speed');

    function formatTime(seconds) {
        if (isNaN(seconds) || !isFinite(seconds)) return '0:00';
        var m = Math.floor(seconds / 60);
        var s = Math.floor(seconds % 60);
        return m + ':' + (s < 10 ? '0' : '') + s;
    }

    function showPauseIcon() {
        iconPlay.style.display  = 'none';
        iconPause.style.display = '';
    }

    function showPlayIcon() {
        iconPlay.style.display  = '';
        iconPause.style.display = 'none';
    }

    // --- Audio events ---

    audio.addEventListener('timeupdate', function() {
        if (!audio.duration) return;
        if (progressBar) progressBar.style.width = (audio.currentTime / audio.duration) * 100 + '%';
        if (timeEl) timeEl.textContent = formatTime(audio.currentTime);
    });

    audio.addEventListener('loadedmetadata', function() {
        if (durationEl) durationEl.textContent = formatTime(audio.duration);
    });

    audio.addEventListener('play',  function() { showPauseIcon(); });
    audio.addEventListener('pause', function() { showPlayIcon();  });

    audio.addEventListener('ended', function() {
        showPlayIcon();
        if (progressBar) progressBar.style.width = '0%';
        if (timeEl) timeEl.textContent = '0:00';
    });

    // --- Play/Pause ---

    playBtn.addEventListener('click', function() {
        if (audio.paused) {
            audio.play();
        } else {
            audio.pause();
        }
    });

    // --- Progress seek ---

    if (progressContainer) {
        progressContainer.addEventListener('click', function(e) {
            if (!audio.duration) return;
            var rect = progressContainer.getBoundingClientRect();
            var pct = (e.clientX - rect.left) / rect.width;
            audio.currentTime = pct * audio.duration;
        });
    }

    // --- Volume ---

    if (volumeBtn) {
        var iconVolOn  = volumeBtn.querySelector('.mementor-tts-sticky-icon-volume-on');
        var iconVolOff = volumeBtn.querySelector('.mementor-tts-sticky-icon-volume-off');

        volumeBtn.addEventListener('click', function() {
            audio.muted = !audio.muted;
            if (audio.muted) {
                iconVolOn.style.display  = 'none';
                iconVolOff.style.display = '';
            } else {
                iconVolOn.style.display  = '';
                iconVolOff.style.display = 'none';
            }
        });
    }

    // --- Speed ---

    if (speedBtn) {
        var speeds = [1, 1.25, 1.5, 1.75, 2, 0.5, 0.75];
        var speedIndex = 0;

        speedBtn.addEventListener('click', function() {
            speedIndex = (speedIndex + 1) % speeds.length;
            audio.playbackRate = speeds[speedIndex];
            speedBtn.textContent = speeds[speedIndex] + 'x';
        });
    }

    // --- Close ---

    closeBtn.addEventListener('click', function() {
        audio.pause();
        stickyEl.style.display = 'none';
    });

})();
