<?php
if ( ! defined( 'ABSPATH' ) ) exit;

class Sifency_Footer_Loader {

    public function __construct() {
        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_footer_assets' ], 10 );
        add_action( 'admin_bar_menu', [ $this, 'add_admin_bar_link' ], 200 );
        add_action( 'get_footer', [ $this, 'start_buffer' ], 1 );
        add_action( 'wp_footer', [ $this, 'end_buffer' ], 9999 );
    }

    public function enqueue_footer_assets() {
        $footer_id = Sifency_Frontend_Helper::find_matching_template( 'footer' );

        if ( ! $footer_id || ! class_exists( '\Elementor\Plugin' ) ) {
            return;
        }

        if ( class_exists( '\Elementor\Frontend' ) ) {
            \Elementor\Plugin::instance()->frontend->enqueue_styles();
            \Elementor\Plugin::instance()->frontend->enqueue_scripts();
        }

        $css_file = null;
        if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
            $css_file = new \Elementor\Core\Files\CSS\Post( $footer_id );
        } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
            $css_file = new \Elementor\Post_CSS_File( $footer_id );
        }

        if ( $css_file ) {
            $css_file->enqueue();
        }

        if ( class_exists( '\Elementor\Plugin' ) ) {
            ob_start();
            \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $footer_id, true );
            ob_end_clean();
        }
    }

    public function add_admin_bar_link( \WP_Admin_Bar $admin_bar ) {
        if ( ! current_user_can( 'edit_posts' ) || ! is_admin_bar_showing() ) {
            return;
        }

        $footer_id = Sifency_Frontend_Helper::find_matching_template( 'footer' );

        if ( $footer_id && class_exists( '\Elementor\Plugin' ) ) {
            $admin_bar->add_node( [
                'id'     => 'sifency-footer-edit',
                'parent' => 'elementor_edit_page',
                'title'  => __( 'Edit Footer', 'sifency-addons' ),
                'href'   => admin_url( 'post.php?post=' . $footer_id . '&action=elementor' ),
            ] );
        }
    }

    public function start_buffer() {
        ob_start();
    }

    public function end_buffer() {
        if ( ob_get_length() ) {
            $footer_content = ob_get_clean();

            $template_id = Sifency_Frontend_Helper::find_matching_template( 'footer' );

            if ( $template_id ) {
                ob_start();
                ?>
                <!-- Sifency Custom Footer Start -->
                <footer id="sifency-custom-footer" class="sifency-footer" itemscope itemtype="https://schema.org/WPFooter">
                    <?php 
                    echo \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id, true );  // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                    ?>
                </footer>
                <!-- Sifency Custom Footer End -->
                <?php
                $custom_html = ob_get_clean();

                $pattern = '/<footer\b[^>]*>.*?<\/footer>/is';

                if ( preg_match( $pattern, $footer_content ) ) {
                    $footer_content = preg_replace( $pattern, $custom_html, $footer_content, 1 );
                } else {
                    $footer_content = $custom_html . $footer_content;
                }
            }

            echo $footer_content ; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        }
    }
}

new Sifency_Footer_Loader();