<?php
/**
 * Front-end functionality for The Matrix Falling Code plugin.
 *
 * @package MatrixFallingCode
 */

defined( 'ABSPATH' ) || exit;

class Matrix_Falling_Code_Public {

    /**
     * Hook into front-end: enqueue assets, inject settings, audio, canvas, and mute button.
     */
    public function init() {
        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] );
        add_action( 'wp_head',            [ $this, 'print_inline_settings' ], 5 );
        add_action( 'wp_footer',          [ $this, 'print_audio' ],         4 );
        add_action( 'wp_footer',          [ $this, 'print_canvas' ],        5 );
        add_action( 'wp_footer',          [ $this, 'print_mute_button' ],   6 );
    }

    /**
     * Enqueue CSS & JS if effect is enabled.
     */
    public function enqueue_assets() {
        if ( ! get_option( 'mfc_enabled', true ) ) {
            return;
        }

        wp_enqueue_style(
            'matrix-falling-code-css',
            MFC_PLUGIN_URL . 'assets/css/matrix.css',
            [],
            MFC_VERSION
        );
        wp_enqueue_script(
            'matrix-falling-code-js',
            MFC_PLUGIN_URL . 'assets/js/matrix.js',
            [],
            MFC_VERSION,
            true
        );
    }

    /**
     * Print a small inline script to expose user settings to matrix.js.
     */
    public function print_inline_settings() {
        if ( ! get_option( 'mfc_enabled', true ) ) {
            return;
        }

        $settings = [
            'color'        => get_option( 'mfc_color', '#0F0' ),
            'fontSize'     => absint( get_option( 'mfc_font_size', 16 ) ),
            'interval'     => absint( get_option( 'mfc_interval', 50 ) ),
            'opacity'      => floatval( get_option( 'mfc_opacity', 0.05 ) ),
            'characters'   => get_option(
                'mfc_characters',
                'アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズヅブプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロゴゾドボポヴッン'
            ),
            'pauseDelay'   => absint( get_option( 'mfc_pause_delay', 2 ) ) * 1000,
            'audioEnabled' => (bool) get_option( 'mfc_audio_enabled', true ),
        ];

        // Properly echo the <script> tag
        echo '<script>window.mfcSettings = ' . wp_json_encode( $settings ) . ';</script>';
    }

    /**
     * Output the <audio> element (only if audio is enabled).
     */
    public function print_audio() {
        if (
            ! get_option( 'mfc_enabled', true )
            || ! get_option( 'mfc_audio_enabled', true )
        ) {
            return;
        }

        $mp3 = MFC_PLUGIN_URL . 'assets/audio/matrix.mp3';
        $ogg = MFC_PLUGIN_URL . 'assets/audio/matrix.ogg';

        echo '<audio id="matrix-audio" loop preload="auto">';
        echo '<source src="' . esc_url( $mp3 ) . '" type="audio/mpeg">';
        echo '<source src="' . esc_url( $ogg ) . '" type="audio/ogg">';
        echo '</audio>';
    }

    /**
     * Output the mute/unmute button (only if audio is enabled).
     */
    public function print_mute_button() {
        if (
            ! get_option( 'mfc_enabled', true )
            || ! get_option( 'mfc_audio_enabled', true )
        ) {
            return;
        }

        echo '<button id="matrix-mute-btn" aria-label="'
           . esc_attr__( 'Mute/Unmute Matrix Audio', 'matrix-falling-code' )
           . '">🔊</button>';
    }

    /**
     * Output the full-screen canvas element.
     */
    public function print_canvas() {
        if ( ! get_option( 'mfc_enabled', true ) ) {
            return;
        }

        echo '<canvas id="matrix-background"></canvas>';
    }
}