<?php
/**
 * HVAC Trainer Navigation System
 *
 * @package HVAC_Community_Events
 * @since 2.0.0
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * HVAC_Trainer_Navigation class
 */
class HVAC_Trainer_Navigation {
    
    /**
     * Constructor - DISABLED: Replaced with HVAC_Menu_System
     */
    public function __construct() {
        // Old navigation system disabled to prevent conflicts with new WordPress menu system
        // All functionality moved to HVAC_Menu_System class
        
        // // Register shortcode
        // add_shortcode('hvac_trainer_navigation', array($this, 'render_navigation'));
        // 
        // // Add navigation to trainer pages
        // add_action('hvac_before_trainer_content', array($this, 'display_navigation'));
        // 
        // // Enqueue styles
        // add_action('wp_enqueue_scripts', array($this, 'enqueue_styles'));
    }
    
    /**
     * Enqueue navigation styles
     */
    public function enqueue_styles() {
        if ($this->is_trainer_page()) {
            wp_enqueue_style(
                'hvac-trainer-navigation',
                HVAC_PLUGIN_URL . 'assets/css/hvac-trainer-navigation.css',
                array(),
                HVAC_PLUGIN_VERSION
            );
        }
    }
    
    /**
     * Check if current page is a trainer page
     */
    private function is_trainer_page() {
        $current_url = home_url(add_query_arg(array(), $GLOBALS['wp']->request));
        return strpos($current_url, '/trainer/') !== false || strpos($current_url, '/master-trainer/') !== false;
    }
    
    /**
     * Get navigation menu items
     */
    private function get_menu_items() {
        $user = wp_get_current_user();
        $is_master = in_array('hvac_master_trainer', $user->roles);
        $is_admin = current_user_can('manage_options');
        
        $menu_items = array(
            'dashboard' => array(
                'label' => 'Dashboard',
                'url' => '/trainer/dashboard/',
                'icon' => 'dashicons-dashboard',
                'capability' => 'hvac_trainer'
            ),
            'events' => array(
                'label' => 'Events',
                'url' => '#',
                'icon' => 'dashicons-calendar-alt',
                'capability' => 'hvac_trainer',
                'submenu' => array(
                    'create' => array(
                        'label' => 'Create Event',
                        'url' => '/trainer/event/manage/',
                        'icon' => 'dashicons-plus-alt'
                    ),
                    'summary' => array(
                        'label' => 'Event Summary',
                        'url' => '/trainer/event-summary/',
                        'icon' => 'dashicons-list-view'
                    ),
                    'certificates' => array(
                        'label' => 'Generate Certificates',
                        'url' => '/trainer/generate-certificates/',
                        'icon' => 'dashicons-awards'
                    ),
                    'reports' => array(
                        'label' => 'Certificate Reports',
                        'url' => '/trainer/certificate-reports/',
                        'icon' => 'dashicons-analytics'
                    )
                )
            ),
            'venues' => array(
                'label' => 'Venues',
                'url' => '#',
                'icon' => 'dashicons-location',
                'capability' => 'hvac_trainer',
                'submenu' => array(
                    'list' => array(
                        'label' => 'All Venues',
                        'url' => '/trainer/venue/list/',
                        'icon' => 'dashicons-list-view'
                    ),
                    'add' => array(
                        'label' => 'Add New Venue',
                        'url' => '/trainer/venue/manage/',
                        'icon' => 'dashicons-plus-alt'
                    )
                )
            ),
            'organizers' => array(
                'label' => 'Organizers',
                'url' => '#',
                'icon' => 'dashicons-building',
                'capability' => 'hvac_trainer',
                'submenu' => array(
                    'list' => array(
                        'label' => 'All Organizers',
                        'url' => '/trainer/organizer/list/',
                        'icon' => 'dashicons-list-view'
                    ),
                    'add' => array(
                        'label' => 'Add New Organizer',
                        'url' => '/trainer/organizer/manage/',
                        'icon' => 'dashicons-plus-alt'
                    )
                )
            ),
            'profile' => array(
                'label' => 'Profile',
                'url' => '#',
                'icon' => 'dashicons-admin-users',
                'capability' => 'hvac_trainer',
                'submenu' => array(
                    'view' => array(
                        'label' => 'View Profile',
                        'url' => '/trainer/profile/',
                        'icon' => 'dashicons-visibility'
                    ),
                    'edit' => array(
                        'label' => 'Edit Profile',
                        'url' => '/trainer/profile/edit/',
                        'icon' => 'dashicons-edit'
                    )
                )
            )
        );
        
        // Add Master Dashboard as main menu item for master trainers and admins
        if ($is_master || $is_admin) {
            $menu_items['master-dashboard'] = array(
                'label' => 'Master Dashboard',
                'url' => '/master-trainer/master-dashboard/',
                'icon' => 'dashicons-star-filled',
                'capability' => 'hvac_master_trainer'
            );
        }
        
        // Add master trainer items if applicable (for master trainers or WordPress admins)
        if ($is_master || $is_admin) {
            $menu_items['master'] = array(
                'label' => 'Master Trainer',
                'url' => '#',
                'icon' => 'dashicons-star-filled',
                'capability' => 'hvac_master_trainer',
                'submenu' => array(
                    'dashboard' => array(
                        'label' => 'Master Dashboard',
                        'url' => '/master-trainer/master-dashboard/',
                        'icon' => 'dashicons-dashboard'
                    ),
                    'trainers' => array(
                        'label' => 'All Trainers',
                        'url' => '/master-trainer/trainers/',
                        'icon' => 'dashicons-groups'
                    ),
                    'reports' => array(
                        'label' => 'Global Reports',
                        'url' => '/master-trainer/reports/',
                        'icon' => 'dashicons-chart-area'
                    )
                )
            );
        }
        
        return apply_filters('hvac_trainer_menu_items', $menu_items);
    }
    
    /**
     * Check if user can access item (admins can access everything)
     */
    private function user_can_access_item($capability) {
        // WordPress admins can access everything
        if (current_user_can('manage_options')) {
            return true;
        }
        
        // Otherwise check the specific capability
        return current_user_can($capability);
    }
    
    /**
     * Get current page identifier
     */
    private function get_current_page() {
        $current_url = home_url(add_query_arg(array(), $GLOBALS['wp']->request));
        
        // Map URLs to page identifiers
        $page_map = array(
            '/trainer/dashboard/' => 'dashboard',
            '/trainer/event/manage/' => 'events.create',
            '/trainer/event-summary/' => 'events.summary',
            '/trainer/generate-certificates/' => 'events.certificates',
            '/trainer/certificate-reports/' => 'events.reports',
            '/trainer/venue/list/' => 'venues.list',
            '/trainer/venue/manage/' => 'venues.add',
            '/trainer/organizer/list/' => 'organizers.list',
            '/trainer/organizer/manage/' => 'organizers.add',
            '/trainer/profile/' => 'profile.view',
            '/trainer/profile/edit/' => 'profile.edit',
            '/master-trainer/master-dashboard/' => 'master.dashboard',
            '/master-trainer/trainers/' => 'master.trainers',
            '/master-trainer/reports/' => 'master.reports'
        );
        
        foreach ($page_map as $url => $page_id) {
            if (strpos($current_url, $url) !== false) {
                return $page_id;
            }
        }
        
        return '';
    }
    
    /**
     * Check if menu item is active
     */
    private function is_menu_active($item_id, $current_page) {
        // Check exact match
        if ($item_id === $current_page) {
            return true;
        }
        
        // Check parent match
        if (strpos($current_page, $item_id . '.') === 0) {
            return true;
        }
        
        return false;
    }
    
    /**
     * Render navigation shortcode
     */
    public function render_navigation($atts = array()) {
        if (!is_user_logged_in()) {
            return '';
        }
        
        // Show navigation for trainers, master trainers, or WordPress admins
        if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
            return '';
        }
        
        $atts = shortcode_atts(array(
            'type' => 'horizontal', // horizontal or vertical
            'show_icons' => 'yes',
            'show_submenu' => 'hover' // hover, click, or always
        ), $atts);
        
        ob_start();
        ?>
        <nav class="hvac-trainer-nav hvac-trainer-nav-<?php echo esc_attr($atts['type']); ?>" 
             data-submenu="<?php echo esc_attr($atts['show_submenu']); ?>">
            <?php $this->render_menu_items($atts); ?>
        </nav>
        <?php
        return ob_get_clean();
    }
    
    /**
     * Render menu items
     */
    private function render_menu_items($atts) {
        $menu_items = $this->get_menu_items();
        $current_page = $this->get_current_page();
        
        echo '<ul class="hvac-nav-menu">';
        
        foreach ($menu_items as $item_id => $item) {
            // Check capability (allow admins to access everything)
            if (isset($item['capability']) && !$this->user_can_access_item($item['capability'])) {
                continue;
            }
            
            $is_active = $this->is_menu_active($item_id, $current_page);
            $has_submenu = !empty($item['submenu']);
            
            $classes = array('hvac-nav-item');
            if ($is_active) {
                $classes[] = 'hvac-nav-active';
            }
            if ($has_submenu) {
                $classes[] = 'hvac-nav-has-submenu';
            }
            
            echo '<li class="' . esc_attr(implode(' ', $classes)) . '">';
            
            // Main menu link
            echo '<a href="' . esc_url($item['url']) . '" class="hvac-nav-link">';
            
            if ($atts['show_icons'] === 'yes' && !empty($item['icon'])) {
                echo '<span class="dashicons ' . esc_attr($item['icon']) . '"></span>';
            }
            
            echo '<span class="hvac-nav-label">' . esc_html($item['label']) . '</span>';
            
            if ($has_submenu) {
                echo '<span class="dashicons dashicons-arrow-down-alt2 hvac-nav-arrow"></span>';
            }
            
            echo '</a>';
            
            // Submenu
            if ($has_submenu) {
                echo '<ul class="hvac-nav-submenu">';
                
                foreach ($item['submenu'] as $sub_id => $subitem) {
                    $sub_is_active = ($item_id . '.' . $sub_id === $current_page);
                    
                    echo '<li class="hvac-nav-subitem' . ($sub_is_active ? ' hvac-nav-active' : '') . '">';
                    echo '<a href="' . esc_url($subitem['url']) . '" class="hvac-nav-sublink">';
                    
                    if ($atts['show_icons'] === 'yes' && !empty($subitem['icon'])) {
                        echo '<span class="dashicons ' . esc_attr($subitem['icon']) . '"></span>';
                    }
                    
                    echo '<span class="hvac-nav-label">' . esc_html($subitem['label']) . '</span>';
                    echo '</a>';
                    echo '</li>';
                }
                
                echo '</ul>';
            }
            
            echo '</li>';
        }
        
        echo '</ul>';
    }
    
    /**
     * Display navigation on trainer pages - DISABLED
     */
    public function display_navigation() {
        // Disabled to prevent conflicts with new HVAC_Menu_System
        // if ($this->is_trainer_page()) {
        //     echo do_shortcode('[hvac_trainer_navigation]');
        // }
    }
    
    /**
     * Render mobile navigation toggle
     */
    public function render_mobile_toggle() {
        ?>
        <button class="hvac-nav-mobile-toggle" aria-label="Toggle navigation">
            <span class="hvac-nav-toggle-icon"></span>
        </button>
        <?php
    }
}