<?php
/**
 * HVAC Layout Manager
 * 
 * Theme-agnostic layout management for HVAC plugin pages
 * Complies with WordPress plugin guidelines
 *
 * @package HVAC_Community_Events
 * @since 2.0.0
 */

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

/**
 * HVAC Layout Manager Class
 */
class HVAC_Layout_Manager {
    
    /**
     * Instance
     *
     * @var HVAC_Layout_Manager
     */
    private static $instance = null;
    
    /**
     * Get instance
     */
    public static function instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * Constructor
     */
    private function __construct() {
        $this->init_hooks();
    }
    
    /**
     * Initialize hooks
     */
    private function init_hooks() {
        // Use standard WordPress hooks that work with any theme
        add_filter('body_class', [$this, 'add_body_classes'], 10);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_layout_styles'], 20);
        add_action('wp_head', [$this, 'add_inline_styles'], 99);
        
        // Standard WordPress sidebar management
        add_filter('is_active_sidebar', [$this, 'maybe_disable_sidebar'], 10, 2);
        
        // Content width for HVAC pages
        add_action('template_redirect', [$this, 'set_content_width']);
        
        // Use standard post meta for layout settings
        add_action('wp', [$this, 'setup_page_layout']);
    }
    
    /**
     * Add body classes for HVAC pages
     */
    public function add_body_classes($classes) {
        if ($this->is_hvac_page()) {
            // Add generic classes that work with any theme
            $classes[] = 'hvac-plugin-page';
            $classes[] = 'hvac-no-sidebar';
            $classes[] = 'hvac-full-width';
            
            if ($this->is_find_trainer_page()) {
                $classes[] = 'hvac-find-trainer-page';
                $classes[] = 'hvac-boxed-layout';
            }
        }
        
        return $classes;
    }
    
    /**
     * Enqueue layout styles
     */
    public function enqueue_layout_styles() {
        if ($this->is_hvac_page()) {
            // Enqueue theme-agnostic layout styles
            wp_enqueue_style(
                'hvac-layout',
                HVAC_PLUGIN_URL . 'assets/css/hvac-layout.css',
                [],
                HVAC_VERSION
            );
        }
    }
    
    /**
     * Add inline styles for dynamic layout adjustments
     */
    public function add_inline_styles() {
        if (!$this->is_hvac_page()) {
            return;
        }
        
        $css = '';
        
        if ($this->is_find_trainer_page()) {
            // Boxed layout for Find a Trainer
            $css .= '
                /* HVAC Find a Trainer - Boxed Layout */
                .hvac-find-trainer-page .site-content,
                .hvac-find-trainer-page .content-area,
                .hvac-find-trainer-page #primary,
                .hvac-find-trainer-page .entry-content {
                    max-width: 1200px;
                    margin: 0 auto;
                    padding-left: 20px;
                    padding-right: 20px;
                    box-sizing: border-box;
                }
                
                /* Hide sidebar universally */
                .hvac-find-trainer-page .widget-area,
                .hvac-find-trainer-page .sidebar,
                .hvac-find-trainer-page #secondary,
                .hvac-find-trainer-page aside.widget-area {
                    display: none !important;
                }
                
                /* Full width content area */
                .hvac-find-trainer-page #primary {
                    width: 100% !important;
                    float: none !important;
                }
            ';
        } else {
            // Full-width layout for other HVAC pages
            $css .= '
                /* HVAC Pages - Full Width Layout */
                .hvac-plugin-page .site-content,
                .hvac-plugin-page .content-area {
                    max-width: 100%;
                    width: 100%;
                    padding-left: 40px;
                    padding-right: 40px;
                }
                
                .hvac-plugin-page .hvac-page-wrapper {
                    max-width: 1920px;
                    margin: 0 auto;
                }
                
                /* Hide sidebar universally */
                .hvac-plugin-page .widget-area,
                .hvac-plugin-page .sidebar,
                .hvac-plugin-page #secondary,
                .hvac-plugin-page aside.widget-area {
                    display: none !important;
                }
                
                /* Full width content area */
                .hvac-plugin-page #primary,
                .hvac-plugin-page .content-area {
                    width: 100% !important;
                    float: none !important;
                    margin: 0 !important;
                }
            ';
        }
        
        if (!empty($css)) {
            echo '<style type="text/css" id="hvac-layout-inline-css">' . $css . '</style>';
        }
    }
    
    /**
     * Maybe disable sidebar for HVAC pages
     */
    public function maybe_disable_sidebar($is_active, $index) {
        if ($this->is_hvac_page()) {
            return false;
        }
        return $is_active;
    }
    
    /**
     * Set content width for HVAC pages
     */
    public function set_content_width() {
        if ($this->is_hvac_page()) {
            global $content_width;
            
            if ($this->is_find_trainer_page()) {
                $content_width = 1200;
            } else {
                $content_width = 1920;
            }
        }
    }
    
    /**
     * Setup page layout using standard WordPress post meta
     */
    public function setup_page_layout() {
        if ($this->is_hvac_page()) {
            global $post;
            if ($post) {
                // Use generic post meta that many themes support
                update_post_meta($post->ID, '_sidebar_layout', 'no-sidebar');
                update_post_meta($post->ID, 'sidebar_layout', 'no-sidebar');
                update_post_meta($post->ID, '_disable_sidebar', '1');
                
                // Common theme-agnostic meta keys
                update_post_meta($post->ID, 'page_layout', 'full-width');
                update_post_meta($post->ID, '_page_layout', 'full-width');
                
                // Disable common features
                update_post_meta($post->ID, '_disable_breadcrumbs', '1');
                update_post_meta($post->ID, 'hide_breadcrumbs', '1');
            }
        }
    }
    
    /**
     * Check if current page is an HVAC page
     */
    private function is_hvac_page() {
        // Check by template
        if (is_page_template()) {
            $template = get_page_template_slug();
            if (strpos($template, 'page-trainer') !== false || 
                strpos($template, 'page-master') !== false ||
                strpos($template, 'page-certificate') !== false ||
                strpos($template, 'page-generate') !== false) {
                return true;
            }
        }
        
        // Check by URL
        $current_url = $_SERVER['REQUEST_URI'];
        $hvac_paths = ['trainer/', 'master-trainer/', 'certificate', 'generate-certificates', 'find-a-trainer'];
        
        foreach ($hvac_paths as $path) {
            if (strpos($current_url, $path) !== false) {
                return true;
            }
        }
        
        // Check by page slug
        if (is_page()) {
            global $post;
            if ($post) {
                $slug = $post->post_name;
                $hvac_slugs = ['trainer', 'dashboard', 'profile', 'certificate', 'venue', 'organizer', 'find-a-trainer'];
                foreach ($hvac_slugs as $hvac_slug) {
                    if (strpos($slug, $hvac_slug) !== false) {
                        return true;
                    }
                }
            }
        }
        
        return false;
    }
    
    /**
     * Check if current page is Find A Trainer page
     */
    private function is_find_trainer_page() {
        if (is_page()) {
            global $post;
            if ($post && $post->post_name === 'find-a-trainer') {
                return true;
            }
        }
        
        $current_url = $_SERVER['REQUEST_URI'];
        return strpos($current_url, 'find-a-trainer') !== false;
    }
}

// Initialize
HVAC_Layout_Manager::instance();