<?php

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

const barestho_plugin_root_dir = __DIR__ . "/..";
const barestho_env_file_path = barestho_plugin_root_dir . "/env.php";

function barestho_loadenv() {
    if (file_exists(barestho_env_file_path)) {
        return require barestho_env_file_path;
    } else {
        throw new Exception("Configuration file not found.");
    }
}

$barestho_config = barestho_loadenv();

// Définir les variables d'environnement pour l'utilisation dans le plugin
$barestho_port = $barestho_config['PORT'];
$barestho_protocol = $barestho_config['PROTOCOL'];
$barestho_domain_reservation = $barestho_config['DOMAIN_RESERVATION'];

// Fonction réutilisable pour générer l'URL du widget
function barestho_generate_widget_url($view, $restaurant_id, $theme_color, $logo_choice, $barestho_protocol, $barestho_domain_reservation, $barestho_port) {
    $logo_params = [
        '1' => '&barestho-logo=%23252332&barestho-alternate=false&text-color=%23FFFFFF',
        '2' => '&barestho-logo=%23DC0044&barestho-alternate=false&text-color=%23FFFFFF',
        '3' => '&barestho-logo=%23DC0044&barestho-alternate=true&text-color=%23252332',
    ];

    // Détecter la langue active — méthode robuste pour TranslatePress
    $lang = 'fr'; // fallback to French

    if ( function_exists( 'trp_custom_language_switcher' ) ) {
        $trp_langs = trp_custom_language_switcher();
        $current_locale = get_locale();
        foreach ( $trp_langs as $code => $trp_item ) {
            if ( isset( $trp_item['language_code'] ) && $trp_item['language_code'] === $current_locale ) {
                $lang = isset( $trp_item['short_language_name'] ) ? $trp_item['short_language_name'] : $code;
                break;
            }
        }
    } elseif ( function_exists( 'trp_get_current_language' ) ) {
        $trp_lang = trp_get_current_language();
        if ( strlen( $trp_lang ) === 2 ) {
            $lang = $trp_lang;
        } else {
            $lang = strtolower( substr( $trp_lang, 0, 2 ) );
        }
    } else {
        // Last-resort: detect 2-letter language from URL path (e.g. /en/)
        $request_path = trim( parse_url( home_url( add_query_arg( array(), $_SERVER['REQUEST_URI'] ) ), PHP_URL_PATH ), '/' );
        $first_segment = explode( '/', $request_path )[0] ?? '';
        if ( preg_match( '/^[a-z]{2}$/i', $first_segment ) ) {
            $lang = strtolower( $first_segment );
        }
    }

    $color_with_hash = strpos($theme_color, '#') === 0 ? $theme_color : '#' . $theme_color;

    return $barestho_protocol . '://' . esc_attr($restaurant_id) . '.' . $barestho_domain_reservation . ':' . $barestho_port
        . '/?lng=' . esc_attr($lang)
        . '&view=' . $view
        . '&primary-color=' . rawurlencode($color_with_hash)
        . '&secondary-color=' . rawurlencode($color_with_hash)
        . $logo_params[$logo_choice];
}

function barestho_getoptions() {
    $barestho_options = get_option('barestho_settings');
    if (!is_array($barestho_options)) {
        $barestho_options = [];
    }
    return [
        'restaurant_id' => isset($barestho_options['barestho_restaurant_id']) ? $barestho_options['barestho_restaurant_id'] : '',
        'theme_color' => !empty($barestho_options['barestho_theme_color']) ? $barestho_options['barestho_theme_color'] : '',
        'logo_choice' => isset($barestho_options['barestho_logo_choice']) ? $barestho_options['barestho_logo_choice'] : '1',
        'custom_widget' => !empty($barestho_options['barestho_custom_widget']),
        'reservation_button' => !empty($barestho_options['barestho_reservation_button']),
        'popup_widget' => !empty($barestho_options['barestho_popup_widget']),
        'excluded_urls' => !empty($barestho_options['barestho_excluded_urls']) ? $barestho_options['barestho_excluded_urls'] : '',
    ];
}

function barestho_generate_iframe($view, $barestho_protocol, $barestho_domain_reservation, $barestho_port, $barestho_options) {
    $widget_url = barestho_generate_widget_url(
        $view,
        esc_attr($barestho_options['restaurant_id']),
        esc_attr($barestho_options['theme_color']),
        esc_attr($barestho_options['logo_choice']),
        esc_attr($barestho_protocol),
        esc_attr($barestho_domain_reservation),
        esc_attr($barestho_port)
    );

    return '<iframe id="' . esc_attr($barestho_options['restaurant_id']) . '" class="barestho-widget" data-no-translation src="' . esc_url($widget_url) . '"></iframe>';
}

function barestho_toggle() {
    global $barestho_port, $barestho_protocol, $barestho_domain_reservation;

    $barestho_options = barestho_getoptions();

    // Vérifier les URLs exclues
    $excluded_urls_raw = isset($barestho_options['excluded_urls']) ? $barestho_options['excluded_urls'] : '';
    $excluded_urls = array_filter(array_map('trim', explode("\n", $excluded_urls_raw)));

    $current_url = home_url(add_query_arg([], $_SERVER['REQUEST_URI']));
    foreach ($excluded_urls as $excluded_url) {
        if (strpos($current_url, $excluded_url) !== false) {
            return;
        }
    }

    // Vérifier et échapper les options avant de les utiliser
    if (empty($barestho_options['restaurant_id']) || !$barestho_options['reservation_button']) {
        return;
    }

    $barestho_allowed_tags = array(
        'iframe' => array(
            'id' => array(),
            'class' => array(),
            'src' => array(),
            'frameborder' => array(),
            'style' => array(),
            'width' => array(),
            'height' => array(),
            'allow' => array(),
            'allowfullscreen' => array(),
            'data-no-translation' => array(),
        ),
        'div' => array(
            'style' => array(),
            'class' => array(),
        ),
    );
    
    echo wp_kses(
        barestho_generate_iframe(
            'toggle',
            esc_attr($barestho_protocol),
            esc_attr($barestho_domain_reservation),
            esc_attr($barestho_port),
            $barestho_options
        ), 
        $barestho_allowed_tags
    );
}

function barestho_inpage() {
    global $barestho_port, $barestho_protocol, $barestho_domain_reservation;

    $barestho_options = barestho_getoptions();
    if (empty($barestho_options['restaurant_id']) || !$barestho_options['custom_widget']) {
        return;
    }

    return barestho_generate_iframe('in-page', $barestho_protocol, $barestho_domain_reservation, $barestho_port, $barestho_options);
}
add_shortcode('barestho_inpage', 'barestho_inpage');

function barestho_preview() {
    global $barestho_port, $barestho_protocol, $barestho_domain_reservation;

    $barestho_options = barestho_getoptions();
    if (empty($barestho_options['restaurant_id'])) {
        return;
    }

    $widget_url = barestho_generate_widget_url('in-page', $barestho_options['restaurant_id'], $barestho_options['theme_color'], $barestho_options['logo_choice'], $barestho_protocol, $barestho_domain_reservation, $barestho_port);

    return '
        <div style="position: relative; width: 360px; height: 550px; border-radius: 20px; background-color: white;">
            <iframe id="' . esc_attr($barestho_options['restaurant_id']) . '" class="barestho-widget" data-no-translation src="' . esc_url($widget_url) . '" frameborder="0" style="width: 100%; height: 100%; pointer-events: none; border-radius: 20px;"></iframe>
            <div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(255, 255, 255, 0); z-index: 10;"></div>
        </div>';
}

function barestho_popup() {
    global $barestho_port, $barestho_protocol, $barestho_domain_reservation;

    $barestho_options = barestho_getoptions();

    if (empty($barestho_options['restaurant_id']) || !$barestho_options['popup_widget']) {
        return;
    }

    $barestho_escaped_protocol = esc_attr($barestho_protocol);
    $barestho_escaped_domain = esc_attr($barestho_domain_reservation);
    $barestho_escaped_port = esc_attr($barestho_port);

    $barestho_allowed_tags = array(
        'iframe' => array(
            'id' => array(),
            'class' => array(),
            'src' => array(),
            'frameborder' => array(),
            'style' => array(),
            'width' => array(),
            'height' => array(),
            'allow' => array(),
            'allowfullscreen' => array(),
            'data-no-translation' => array(),
        ),
        'div' => array(
            'style' => array(),
            'class' => array(),
        ),
    );
    
    echo wp_kses(
        barestho_generate_iframe(
            'popup',
            esc_attr($barestho_escaped_protocol),
            esc_attr($barestho_escaped_domain),
            esc_attr($barestho_escaped_port),
            $barestho_options
        ), 
        $barestho_allowed_tags
    );
}
add_action('wp_footer', 'barestho_popup');

?>