<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

/**
* Plugin Name: ENOSI Embedder For Unity
* Plugin URI:  https://enosistudio.com/
* Description: Displays a Unity WebGL game inside your page.
* Version: 1.1.0
* Author: MARTIN Baptiste / Voidstack
* Author URI: https://www.linkedin.com/in/baptiste-martin56/
* License: GPL-3.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Tested up to: 6.9
* Stable tag: 1.1.0
* Text Domain: enosi-embedder-unity
* Domain Path: /languages
*/

require_once plugin_dir_path(__FILE__) . 'php/enosi-utils.php';

// Allows to load the administration page script, only for administration (optimization)
if(is_admin()){
    require_once plugin_dir_path(__FILE__) . 'php/enosi-admin-page.php';
    require_once plugin_dir_path(__FILE__) . 'php/enosi-unity-block.php'; // ne s'exécute que dans l'éditeur de blocs (page/post avec Gutenberg)
}

// Add of main.css
add_action('wp_enqueue_scripts', fn() =>
wp_enqueue_style(
    'enosi-main-css',
    plugins_url('css/enosi-main.css', __FILE__),
    [],
    filemtime(plugin_dir_path(__FILE__) . 'css/enosi-main.css')
    )
);

// Enqueue the client script as a module
add_action('wp_enqueue_scripts', function () {
    $handle = 'enosi-embedder-unity';
    $script_url = plugins_url('js/client-unity-block.js', __FILE__);

    // Registers and enqueues a JS module (WordPress 6.5+)
    wp_enqueue_script_module(
        $handle,
        $script_url,
        [],
        filemtime(plugin_dir_path(__FILE__) . 'js/client-unity-block.js')
    );
});

/**
 * Generates a container for a Unity WebGL build and enqueues the module script.
 */
function unityWebglShortcode($atts): string {
    static $loader_cache = []; // Cache to avoid checking the same build multiple times

    $a = shortcode_atts([
        'build'=>'','showoptions'=>'true','showonmobile'=>'false','showlogs'=>'false',
        'fixedheight'=>0,'aspectratio'=>''
    ], array_change_key_case($atts, CASE_LOWER));

    $args=[
        'build'=>sanitize_title($a['build']),
        'showOptions'=>filter_var($a['showoptions'],FILTER_VALIDATE_BOOLEAN),
        'showOnMobile'=>filter_var($a['showonmobile'],FILTER_VALIDATE_BOOLEAN),
        'showLogs'=>filter_var($a['showlogs'],FILTER_VALIDATE_BOOLEAN),
        'fixedHeight'=>(int)$a['fixedheight'],
        'aspectRatio'=>sanitize_text_field($a['aspectratio']),
    ];
    
    $up = wp_upload_dir();
    $build_path = "{$up['basedir']}/unity_webgl/{$args['build']}";
    $build_url  = trailingslashit("{$up['baseurl']}/unity_webgl/{$args['build']}");
    $loader_file = "$build_path/{$args['build']}.loader.js";
    
    if(!isset($loader_cache[$args['build']])) {
        $loader_cache[$args['build']] = file_exists($loader_file);
    }
    if (!$loader_cache[$args['build']]) {
        return "<p style='color:red'>" . esc_html__("Unity build not found:", "enosi-embedder-unity") . " " . esc_html($loader_file) . "</p>";
    }
    if (wp_is_mobile() && !$args['showOnMobile']) {
        return "<p>🚫 " . esc_html__("Not available on mobile", "enosi-embedder-unity") . "</p>";
    }
    
    $style = '';
    $hasHeight = $args['fixedHeight'] > 0;
    $hasRatio  = !empty($args['aspectRatio']) && preg_match('/^([1-9]\d*)\/([1-9]\d*)$/', $args['aspectRatio'], $rm);

    if ($hasHeight && $hasRatio) {
        $width = round($args['fixedHeight'] * ((int)$rm[1] / (int)$rm[2]));
        $style = "height:{$args['fixedHeight']}px;width:{$width}px;";
    } elseif ($hasHeight) {
        $style = "height:{$args['fixedHeight']}px;";
    } elseif ($hasRatio) {
        $style = "aspect-ratio:{$args['aspectRatio']};";
    }
    
    $uuid = EnosiUtils::generateUuid();
    
    $handle = 'enosi-embedder-unity';

    // Enqueue the module script (already registered above)
    wp_enqueue_script_module($handle);

    $is_admin = current_user_can('administrator') ? 'true' : 'false';
    $adm_message = esc_attr(__('Unity error', 'enosi-embedder-unity'));

    ob_start(); ?>
    <div id="<?php echo esc_attr($uuid)?>-error" class="unity-error"></div>
    <div id="<?php echo esc_attr($uuid)?>-container" class="unity-container" style="<?php echo esc_attr($style)?>">
    <canvas id="<?php echo esc_attr($uuid)?>-canvas" class="unity-canvas"
    data-build-url="<?php echo esc_attr($build_url)?>"
    data-loader-name="<?php echo esc_attr(basename($loader_file,'.loader.js'))?>"
    data-show-options="<?php echo esc_attr($args['showOptions']?'true':'false')?>"
    data-show-logs="<?php echo esc_attr($args['showLogs']?'true':'false')?>"
    data-fixed-height="<?php echo esc_attr($args['fixedHeight']); ?>"
    data-aspect-ratio="<?php echo esc_attr($args['aspectRatio'])?>"
    data-current-user-is-admin="<?php echo esc_attr($is_admin?'true':'false')?>"
    data-adm-message="<?php echo esc_attr($adm_message)?>">
    </canvas>
    </div>
    <?php return ob_get_clean();
}
// Register the shortcode
add_shortcode('unity_webgl', 'unityWebglShortcode');
