<?php
add_action('wp_footer', 'rm_load_custom_fonts_from');

function rm_load_custom_fonts_from() {
    if (!function_exists('get_field')) return;

    $fonts = get_field('fonts', 'option');
    if (!$fonts) return;

    $css = '';
    foreach ($fonts as $font) {
        $lang       = $font['lang'] ?? '';
        $font_name  = $font['font_name'] ?? '';
        $weight     = $font['weight'] ?? 'normal';
        $ftype      = $font['ftype'] ?? '';

        // تحقق من اللغة الحالية
        if (get_locale() != $lang) {
            continue;
        }

        if ($ftype === 'sheet') {
            $font_url = $font['font_url'] ?? '';
            if ($font_url) {
                echo "<link href='{$font_url}' rel='stylesheet' />";
            }
        }
        elseif ($ftype === 'url') {
            $woff2 = $font['woff2_file']['url'] ?? '';
            $woff  = $font['woff_file']['url'] ?? '';
            $ttf   = $font['ttf_file']['url'] ?? '';

            if ($woff2 || $woff || $ttf) {
                $css .= "@font-face {";
                $css .= "  font-family: '{$font_name}';";
                $css .= "  font-style: normal;";
                $css .= "  font-weight: {$weight};";
                $css .= "  font-display: swap;";
                $css .= "  src: ";

                $sources = [];
                if ($woff2) $sources[] = "url('{$woff2}') format('woff2')";
                if ($woff)  $sources[] = "url('{$woff}') format('woff')";
                if ($ttf)   $sources[] = "url('{$ttf}') format('truetype')";

                $css .= implode(",       ", $sources) . ";";
                $css .= "}";
            }
        }
    }

    if ($css) {
        echo "<style id=\"custom-fonts-loader\">{$css}</style>";
    }



    // تطبيق الفونتات الافتراضية حسب اللغة
    $defaults       = get_field('default_fonts', 'option');
    $font_sizes     = get_field('default_fonts_size', 'option');

    $theme_colors   = get_field('theme_colors', 'option');


    echo '<style id="custom-fonts-defaults">';

    if ($defaults) {
        foreach ($defaults as $def) {
            $lang = $def['lang'] ?? '';
            $font_name = $def['font_name'] ?? '';
            $weight = $def['weight'] ?? 'normal';
            $tag_type = $def['tags'] ?? 'normal';
            $custom_class = $def['custom_css_class'] ?? '';

            if (get_locale() != $lang) {
                continue;
            }

            if ($tag_type === 'normal') {
                echo "body, h1, h2, h3, h4, h5, h6 { font-family: '{$font_name}'; font-weight: {$weight}; }";
            } elseif ($tag_type === 'headlines') {
                echo "h1, h2, h3, h4, h5, h6, strong, b, th { font-family: '{$font_name}'; font-weight: {$weight}; }";
            } elseif ($tag_type === 'custom_css' && $custom_class) {
                echo ".{$custom_class} { font-family: '{$font_name}'; font-weight: {$weight}; }";
            }
        }
    }

    // حجم الخط الافتراضي حسب اللغة
    if ($font_sizes) {
        foreach ($font_sizes as $size) {
            $lang = $size['lang'] ?? '';
            $size_pixel = $size['size_pixel'] ?? '';

            if (!$lang || !$size_pixel) continue;

            if (get_locale() == $lang) {
                echo "body { font-size: {$size_pixel}px; }";
            }
        }
    }

    if ($theme_colors) {
        $css_vars = ":root {";
        $css_classes = "";

        foreach ($theme_colors as $color) {
            $id = sanitize_title($color['color_id'] ?? '');
            $code = trim($color['color_code'] ?? '');

            if (!$id || !$code) continue;

            // المتغير
            $css_vars .= "--{$id}: {$code};";

            // الكلاس
            $css_classes .= "
    .{$id}-bg { background-color: var(--{$id}) !important; }
    .{$id}-text { color: var(--{$id}) !important; }
    ";
        }

        $css_vars .= "}";
        
        echo $css_vars . $css_classes;
    }



    
    echo '</style>';


}

add_filter( 'elementor/fonts/additional_fonts', function ( $fonts ) {
    $fonts = get_field('fonts', 'option');
    if (!$fonts) return;
    
    foreach ($fonts as $font) {
        $font_name  = $font['font_name'] ?? '';
        $fonts[$font_name] = 'system';
    }
    return $fonts;
} );


