<?php

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

require_once __DIR__ . '/enosi-utils.php';

/**
* Returns an array of strings to translate for the JS interface.
*/
function enosiGetTranslationStrings(): array {
    return [
        'buildChoose' => __('Choose a unity build', 'enosi-embedder-unity') . ' : ',
        'buildSelectionne' => __('Selected build', 'enosi-embedder-unity'),
        'warnExpectedRatio1' => '⚠️ ' . __('Expected format: number/number (4/3)', 'enosi-embedder-unity'),
        'warnExpectedRatio2' => __('If the format is invalid, the aspect ratio will be ignored.', 'enosi-embedder-unity'),
        'showToolbar'     => __('Show toolbar', 'enosi-embedder-unity'),
        'showOnMobile'    => __('Display on mobile', 'enosi-embedder-unity'),
        'showLogs'        => __('Display logs in the console', 'enosi-embedder-unity'),
        'panelDimensions' => __('Dimensions', 'enosi-embedder-unity'),
        'panelOptions'    => __('Options', 'enosi-embedder-unity'),
        'panelDeveloper'  => __('Developer', 'enosi-embedder-unity'),
        'dimensionsHelp'  => __('Fill both fields for a fixed size. Aspect ratio alone gives a responsive layout.', 'enosi-embedder-unity'),
        'noBuildsFound'   => __('No Unity build found.', 'enosi-embedder-unity'),
        'uploadBuild'     => __('Upload a Unity build', 'enosi-embedder-unity'),
        'urlAdmin'        => admin_url('/admin.php'),
    ];
}

/**
* Registers the Unity WebGL block and enqueues the necessary editor script.
*/
function enosiEnqueueUnityBlock(): void
{
    // Register the editor JavaScript file
    wp_register_script(
        'enosi-unity-block',
        plugins_url('../js/editor-unity-block.js', __FILE__),
        ['wp-blocks', 'wp-element', 'wp-editor', 'wp-i18n'],
        filemtime(plugin_dir_path(__FILE__) . '../js/editor-unity-block.js'),
        true
    );
    
    // Ajout des trad dans le script
    wp_localize_script('enosi-unity-block', 'enosiI18n', enosiGetTranslationStrings());
    
    // Register the block type
    register_block_type('wpunity/unity-webgl', [
        'editor_script' => 'enosi-unity-block',
        'editor_style' => 'enosi-unity-block-style',
        'style' => 'enosi-unity-block-style',
    ]);
}

// enqueue_block_editor_assets ne s'exécute que dans l'éditeur de blocs (page/post avec Gutenberg).
add_action('enqueue_block_editor_assets', 'enosiEnqueueUnityBlock');

/*
Cette fonction crée un tableau builds des dossiers présents dans uploads/unity_webgl.
Elle le passe à JS sous le nom global unityBuildsData.
JS peut ensuite lire unityBuildsData.builds pour afficher la liste.
*/
function enosiLocalizeUnityBuilds(): void
{
    $upload_dir = wp_upload_dir();
    $builds_dir = $upload_dir['basedir'] . '/unity_webgl';
    
    $builds = EnosiUtils::listBuilds($builds_dir);
    
    wp_localize_script('enosi-unity-block', 'unityBuildsData', ['builds' => $builds]);
}
add_action('enqueue_block_editor_assets', 'enosiLocalizeUnityBuilds');
