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

class Sifency_Below_Header_Render {

    private static $rendered = false;

    public function __construct() {
        if ( is_admin() ) return;

        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_subheader_assets' ], 10 );
        add_action( 'admin_bar_menu', [ $this, 'add_admin_bar_link' ], 200 );
        add_action( 'template_redirect', [ $this, 'init_hooks' ] );
    }

    public function enqueue_subheader_assets() {
        if ( self::$rendered ) return;

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

        if ( ! $template_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( $template_id );
        } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
            $css_file = new \Elementor\Post_CSS_File( $template_id );
        }

        if ( $css_file ) {
            $css_file->enqueue();
        }
        if ( class_exists( '\Elementor\Plugin' ) ) {
            ob_start();
            \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_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;
        }

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

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

    public function init_hooks() {
        if ( self::$rendered ) return;

        $template_id = Sifency_Frontend_Helper::find_matching_template( 'subheader' );
        if ( ! $template_id ) return;
        add_action( 'get_header', [ $this, 'start_buffer' ], 15 );
        add_action( 'wp_footer', [ $this, 'end_buffer' ], 9999 );
    }

    public function start_buffer() {
        ob_start();
    }

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

            if ( strpos( $content, 'sifency-elementor-below-header' ) !== false ) {
                 echo  $content;
                return;
            }

            if ( self::$rendered ) {
                 echo  $content;
                return;
            }

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

            if ( $template_id ) {
                self::$rendered = true;

                ob_start();
                ?>
                <!-- Sifency Elementor Below Header Start -->
                <div class="sifency-elementor-below-header">
                    <?php 
                    echo  \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id, true ); 
                    ?>
                </div>
                <!-- Sifency Elementor Below Header End -->
                <?php
                $subheader_html = ob_get_clean();
                $pattern_custom = '/(<header[^>]*id="sifency-custom-header"[^>]*>.*?)(<\/header>)/is';
                $pattern_general = '/(<\/header>)/i';

                if ( preg_match( $pattern_custom, $content ) ) {
                    $content = preg_replace( $pattern_custom, '$1$2' . $subheader_html, $content, 1 );
                } 
                elseif ( preg_match( $pattern_general, $content ) ) {
                    $content = preg_replace( $pattern_general, '$1' . $subheader_html, $content, 1 );
                } 
                else {
                    $content = $subheader_html . $content;
                }
            }

            echo $content;
        }
    }
}

// Initialize
new Sifency_Below_Header_Render();