<?php
/**
 * Frontend - Injects Seazonify controller script
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class Seazonify_Frontend {
    
    public function __construct() {
        if (!is_admin()) {
            add_action('wp_enqueue_scripts', array($this, 'enqueue_seazonify_scripts'));
            add_action('wp_enqueue_scripts', array($this, 'inject_custom_styles'), 20);
        }
        
        // Add custom attributes to our controller script - Global for Customizer compatibility
        add_filter('script_loader_tag', array($this, 'add_script_attributes'), 10, 2);
    }
    
    /**
     * Enqueue the controller script if effects are selected
     */
    public function enqueue_seazonify_scripts() {
        $settings = get_option('seazonify_settings', array());
        
        // Check if enabled (always enable in Customizer preview for better UX)
        if (empty($settings['enabled']) && !is_customize_preview()) {
            return;
        }
        
        // Check display conditions
        if (!$this->should_display_effects($settings)) {
            return;
        }

        // Only enqueue if we have something to show
        $visual_id = isset($settings['visual_effect']) ? $settings['visual_effect'] : '';
        $audio_id = isset($settings['audio_effect']) ? $settings['audio_effect'] : '';
        $combo_id = isset($settings['combo_effect']) ? $settings['combo_effect'] : '';

        if (empty($visual_id) && empty($audio_id) && empty($combo_id)) {
            return;
        }

        wp_enqueue_script(
            'seazonify-controller',
            Seazonify_Effects::get_controller_url(),
            array(),
            SEAZONIFY_VERSION,
            true
        );
    }

    /**
     * Add custom attributes to the script tag (visual, audio, combo)
     */
    public function add_script_attributes($tag, $handle) {
        if ('seazonify-controller' !== $handle) {
            return $tag;
        }

        $settings = get_option('seazonify_settings', array());
        $attributes = array();

        $visual_id = isset($settings['visual_effect']) ? $settings['visual_effect'] : '';
        $audio_id = isset($settings['audio_effect']) ? $settings['audio_effect'] : '';
        $combo_id = isset($settings['combo_effect']) ? $settings['combo_effect'] : '';

        if (!empty($visual_id)) {
            $visual_url = Seazonify_Effects::get_effect_url($visual_id, 'visual');
            if ($visual_url) $attributes['visual'] = $visual_url;
        }
        if (!empty($audio_id)) {
            $audio_url = Seazonify_Effects::get_effect_url($audio_id, 'audio');
            if ($audio_url) $attributes['audio'] = $audio_url;
        }
        if (!empty($combo_id)) {
            $combo_url = Seazonify_Effects::get_effect_url($combo_id, 'combo');
            if ($combo_url) $attributes['combo'] = $combo_url;
        }

        $attributes['seazonify-logo'] = SEAZONIFY_LOGO_URL;
        $attributes['seazonify-cdn'] = SEAZONIFY_CDN_BASE;
        $attributes['seazonify-base'] = SEAZONIFY_PLUGIN_URL;

        $attr_string = '';

        foreach ($attributes as $name => $value) {
            if (strpos($tag, " $name=") === false) {
                $attr_string .= ' ' . esc_attr($name) . '="' . esc_url($value) . '"';
            }
        }

        // Add defer and crossorigin if not already present
        if (strpos($tag, ' defer') === false) {
            $attr_string .= ' defer';
        }
        if (strpos($tag, ' crossorigin=') === false) {
            $attr_string .= ' crossorigin="anonymous"';
        }

        // Inject attributes right after the <script opening tag
        return str_replace('<script ', '<script' . $attr_string . ' ', $tag);
    }
    
    /**
     * Inject custom CSS styles for Seazonify
     */
    public function inject_custom_styles() {
        $settings = get_option('seazonify_settings', array());
        
        // Hide controller button if disabled
        if (empty($settings['show_controller'])) {
            wp_register_style('seazonify-frontend-extra', false, array(), SEAZONIFY_VERSION);
            wp_enqueue_style('seazonify-frontend-extra');
            wp_add_inline_style('seazonify-frontend-extra', '.szfy-sidebar-btn { display: none !important; }');
        }
    }
    
    /**
     * Check if effects should be displayed on current page
     * 
     * @param array $settings Plugin settings
     * @return bool True if should display, false otherwise
     */
    private function should_display_effects($settings) {
        $display_on = isset($settings['display_on']) ? $settings['display_on'] : 'all';
        
        // All pages
        if ($display_on === 'all') {
            return true;
        }
        
        // Homepage only
        if ($display_on === 'homepage') {
            return is_front_page() || is_home();
        }
        
        // Specific pages
        if ($display_on === 'pages') {
            if (is_page()) {
                $include_pages = isset($settings['include_pages']) ? $settings['include_pages'] : array();
                return in_array(get_the_ID(), $include_pages);
            }
            return false;
        }
        
        // Specific posts
        if ($display_on === 'posts') {
            if (is_single()) {
                $include_posts = isset($settings['include_posts']) ? $settings['include_posts'] : array();
                return in_array(get_the_ID(), $include_posts);
            }
            return false;
        }
        
        // Custom (include & exclude)
        if ($display_on === 'custom') {
            $include_pages = isset($settings['include_pages']) ? $settings['include_pages'] : array();
            $exclude_pages = isset($settings['exclude_pages']) ? $settings['exclude_pages'] : array();
            $include_posts = isset($settings['include_posts']) ? $settings['include_posts'] : array();
            $exclude_posts = isset($settings['exclude_posts']) ? $settings['exclude_posts'] : array();
            
            $current_id = get_the_ID();
            
            // Check exclusions first
            if (is_page() && in_array($current_id, $exclude_pages)) {
                return false;
            }
            if (is_single() && in_array($current_id, $exclude_posts)) {
                return false;
            }
            
            // Check inclusions
            if (is_page()) {
                return in_array($current_id, $include_pages);
            }
            if (is_single()) {
                return in_array($current_id, $include_posts);
            }
        }
        
        return false;
    }
}

