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

class Sifency_Header_Loader {

    public function __construct() {
        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_header_assets' ], 10 );
        add_action( 'template_redirect', [ $this, 'sifency_addons_replace_header' ] );
        add_action( 'admin_bar_menu', [ $this, 'add_admin_bar_link' ], 200 );
    }

    public function enqueue_header_assets() {
        $header_id = Sifency_Frontend_Helper::find_matching_template( 'header' );

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

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

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

        $header_id = Sifency_Frontend_Helper::find_matching_template( 'header' );

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

    public function sifency_addons_replace_header() {
        $header_id = Sifency_Frontend_Helper::find_matching_template( 'header' );
        if ( ! $header_id ) return;

        add_action( 'get_header', [ $this, 'start_buffer' ] );
        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();
            
            $header_id = Sifency_Frontend_Helper::find_matching_template( 'header' );
            
            if ( $header_id ) {
                ob_start();
                ?>
                <header id="sifency-custom-header" class="sifency-header" itemscope itemtype="https://schema.org/WPHeader">
                    <?php 
                    echo \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $header_id, true );  // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                    ?>
                </header>
                <?php
                $custom_header_html = ob_get_clean();

                $pattern = '/<header[^>]*>.*?<\/header>/is';
                if ( preg_match( $pattern, $content ) ) {
                    $content = preg_replace( $pattern, $custom_header_html, $content, 1 );
                } else {
                    $content = preg_replace( '/<body([^>]*)>/i', '<body$1>' . $custom_header_html, $content, 1 );
                }
            }
            echo $content; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        }
    }
}

new Sifency_Header_Loader();