<?php
/**
 * Language Support Enhancement for AI Blog Theme
 *
 * @package Ai Blog
 */

/**
 * Enhanced language loading for better French support
 */
function ai_blog_enhanced_language_support() {
    // Load theme textdomain with enhanced path checking
    $domain = 'ai-blog';
    $locale = get_locale();
    
    // Check for language files in multiple locations
    $mofile_global = WP_LANG_DIR . '/themes/' . $domain . '-' . $locale . '.mo';
    $mofile_local = get_template_directory() . '/languages/' . $locale . '.mo';
    $mofile_theme = get_template_directory() . '/languages/' . $domain . '-' . $locale . '.mo';
    
    // Load global language file first
    if (file_exists($mofile_global)) {
        load_textdomain($domain, $mofile_global);
    }
    // Then load theme-specific language file
    elseif (file_exists($mofile_theme)) {
        load_textdomain($domain, $mofile_theme);
    }
    // Fallback to simple locale file
    elseif (file_exists($mofile_local)) {
        load_textdomain($domain, $mofile_local);
    }
    
    // Load theme textdomain
    load_theme_textdomain($domain, get_template_directory() . '/languages');
}

// Hook with higher priority to ensure proper loading
add_action('after_setup_theme', 'ai_blog_enhanced_language_support', 5);

/**
 * Add French-specific date format
 */
function ai_blog_french_date_format($the_date, $format, $post) {
    if (get_locale() === 'fr_FR') {
        // French date format: jour mois année
        return date_i18n('j F Y', get_the_time('U', $post));
    }
    return $the_date;
}
add_filter('get_the_date', 'ai_blog_french_date_format', 10, 3);

/**
 * French typography improvements
 */
function ai_blog_french_typography_css() {
    if (get_locale() === 'fr_FR') {
        ?>
        <style type="text/css">
            /* French typography improvements */
            body {
                text-rendering: optimizeLegibility;
                -webkit-font-feature-settings: "liga", "kern";
                font-feature-settings: "liga", "kern";
            }
            
            /* French quotes styling */
            blockquote:before {
                content: "« ";
            }
            
            blockquote:after {
                content: " »";
            }
            
            /* Improve French text spacing */
            .entry-content p {
                line-height: 1.7;
            }
            
            /* French-specific button text */
            .wp-block-button__link,
            .button,
            .btn {
                text-transform: none; /* French doesn't typically use all caps */
            }
        </style>
        <?php
    }
}
add_action('wp_head', 'ai_blog_french_typography_css');

/**
 * French-specific customizer strings
 */
function ai_blog_french_customizer_strings() {
    if (get_locale() === 'fr_FR') {
        // Add French-specific customizer settings if needed
        add_filter('theme_mod_ai_blog_greadmore', function($value) {
            return empty($value) ? __('Lire Plus', 'ai-blog') : $value;
        });
    }
}
add_action('customize_register', 'ai_blog_french_customizer_strings');



/**
 * Register strings for translation (for block patterns)
 */
function ai_blog_register_pattern_strings() {
    if (function_exists('pll_register_string')) {
        // Register block pattern strings for Polylang
        pll_register_string('Hero Section CTA', 'Get Started', 'ai-blog');
        pll_register_string('Hero Section CTA', 'Learn More', 'ai-blog');
        pll_register_string('Newsletter', 'Subscribe Now', 'ai-blog');
        pll_register_string('Newsletter', 'No spam. Unsubscribe anytime.', 'ai-blog');
    }
}
add_action('init', 'ai_blog_register_pattern_strings');

/**
 * Translation helper function for new strings
 */
function ai_blog_get_translation($string, $context = '') {
    $translations = array(
        'fr_FR' => array(
            'Hero Section with CTA' => 'Section Héros avec Appel à l\'Action',
            'Featured Posts Grid' => 'Grille d\'Articles Vedettes',
            'About Section with Image' => 'Section À Propos avec Image',
            'Newsletter Signup Section' => 'Section d\'Inscription Newsletter',
            'Call to Action Banner' => 'Bannière d\'Appel à l\'Action',
            'AI Blog Patterns' => 'Modèles Ai Blog',
            'Clear AI Blog Cache' => 'Vider le Cache Ai Blog',
            'Cache cleared successfully' => 'Cache vidé avec succès',
            'Menu' => 'Menu',
            'Close' => 'Fermer',
        )
    );
    
    $locale = get_locale();
    
    if (isset($translations[$locale][$string])) {
        return $translations[$locale][$string];
    }
    
    return $string;
}

/**
 * Add RTL support detection (though French is LTR)
 */
function ai_blog_language_direction_class($classes) {
    $locale = get_locale();
    
    // Add language-specific classes
    $classes[] = 'lang-' . substr($locale, 0, 2);
    $classes[] = 'locale-' . str_replace('_', '-', strtolower($locale));
    
    return $classes;
}
add_filter('body_class', 'ai_blog_language_direction_class');

/**
 * French number formatting
 */
function ai_blog_french_number_format($number) {
    if (get_locale() === 'fr_FR') {
        return number_format_i18n($number);
    }
    return $number;
}

/**
 * Add language switcher support (if WPML or Polylang is active)
 */
function ai_blog_language_switcher() {
    // WPML support
    if (function_exists('icl_get_languages')) {
        $languages = icl_get_languages('skip_missing=0&orderby=code');
        if (!empty($languages)) {
            echo '<div class="language-switcher">';
            foreach ($languages as $l) {
                if (!$l['active']) {
                    echo '<a href="' . $l['url'] . '" title="' . $l['native_name'] . '">';
                }
                echo $l['language_code'];
                if (!$l['active']) {
                    echo '</a>';
                }
                echo ' | ';
            }
            echo '</div>';
        }
    }
    
    // Polylang support
    if (function_exists('pll_the_languages')) {
        echo '<div class="language-switcher">';
        pll_the_languages(array('dropdown' => 0));
        echo '</div>';
    }
}
