<?php

/**
 * Themesupport for the plguin.
 *
 * @link       https://themeatelier.net/
 * @since      2.2.4
 *
 * @package    Greet_Bubble
 * @subpackage Greet_Bubble
 * @author     ThemeAtelier <themeatelierbd@gmail.com>
 */

namespace ThemeAtelier\GreetBubble\Includes;

if (! defined('ABSPATH')) {
	exit; // Exit if accessed directly.
}

/**
 * ThemeSupport class to handle support different theme.
 *
 * @since 2.2.4
 */
class ThemeSupport
{

    public function __construct()
    {
        add_action('wp_footer', [$this, 'footer_custom']);
    }

    public function footer_custom() {
        // Only proceed if Themify Ultra theme is active
        if (get_template() !== 'themify-ultra') {
            return;
        }
        ?>
        <script>
            // Prevent Themify from initializing video player on Greet elements
            document.addEventListener('DOMContentLoaded', function() {
                const originalOn = Themify.on;
                Themify.on = function(eventName, callback, ...args) {
                    if (eventName === 'tf_video_init') {
                        const wrappedCallback = function(items) {
                            // Filter out greet videos from initialization
                            const filteredItems = items.filter(item => {
                                const parent = item.closest('.greet_wrapper');
                                return !parent;
                            });
                            return callback(filteredItems);
                        };
                        return originalOn.call(this, eventName, wrappedCallback, ...args);
                    }
                    return originalOn.call(this, eventName, callback, ...args);
                };
            });

            // Backup cleanup code in case needed
            function cleanupThemifyElements() {
                const greetWrapper = document.querySelector('.greet_wrapper');
                if (greetWrapper) {
                    // Remove the wrapper div
                    const tfWrapper = greetWrapper.querySelector('.tf_vd_lazy');
                    if (tfWrapper) {
                        const video = tfWrapper.querySelector('video');
                        if (video) {
                            // Remove preload="none" if it exists
                            if (video.getAttribute('preload') === 'none') {
                                video.removeAttribute('preload');
                            }
                            greetWrapper.insertBefore(video, tfWrapper);
                        }
                        tfWrapper.remove();
                    }
                    
                    // Remove the root div
                    const tfRoot = greetWrapper.querySelector('.tf_vd_root');
                    if (tfRoot) {
                        tfRoot.remove();
                    }

                    // Also check the main video element
                    const mainVideo = greetWrapper.querySelector('#greet_video');
                    if (mainVideo && mainVideo.getAttribute('preload') === 'none') {
                        mainVideo.removeAttribute('preload');
                    }
                }
            }

            // Watch for any attempts to modify our video
            const observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    if (mutation.target.id === 'greet_video' || 
                        mutation.target.classList.contains('greet_wrapper')) {
                        cleanupThemifyElements();
                    }
                });
            });

            document.addEventListener('DOMContentLoaded', function() {
                const greetWrapper = document.querySelector('.greet_wrapper');
                if (greetWrapper) {
                    observer.observe(greetWrapper, {
                        childList: true,
                        subtree: true,
                        attributes: true
                    });
                }
            });
        </script>
        <?php
    }
}