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

defined( 'ABSPATH' ) || exit;

class Matrix_Falling_Code_Admin {

    /**
     * Initialize admin hooks.
     */
    public function init() {
        add_action( 'admin_menu',            [ $this, 'add_settings_page' ] );
        add_action( 'admin_init',            [ $this, 'register_settings' ] );
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
    }

    /**
     * Add settings page under the Settings menu.
     */
    public function add_settings_page() {
        add_options_page(
            __( 'Matrix Code Settings', 'matrix-falling-code' ),
            __( 'Matrix Code',          'matrix-falling-code' ),
            'manage_options',
            'matrix-falling-code',
            [ $this, 'render_settings_page' ]
        );
    }

    /**
     * Register plugin settings.
     */
    public function register_settings() {
        register_setting( 'mfc_settings_group', 'mfc_enabled', [
            'type'              => 'boolean',
            'sanitize_callback' => 'rest_sanitize_boolean',
            'default'           => true,
        ] );
        register_setting( 'mfc_settings_group', 'mfc_color', [
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_hex_color',
            'default'           => '#0F0',
        ] );
        register_setting( 'mfc_settings_group', 'mfc_font_size', [
            'type'              => 'integer',
            'sanitize_callback' => 'absint',
            'default'           => 16,
        ] );
        register_setting( 'mfc_settings_group', 'mfc_interval', [
            'type'              => 'integer',
            'sanitize_callback' => 'absint',
            'default'           => 50,
        ] );
        register_setting( 'mfc_settings_group', 'mfc_opacity', [
            'type'              => 'number',
            'sanitize_callback' => [ $this, 'sanitize_opacity' ],
            'default'           => 0.05,
        ] );
	register_setting( 'mfc_settings_group', 'mfc_characters', [
    	  'type'              => 'string',
          'sanitize_callback' => 'sanitize_textarea_field',
         'default'           => 'アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズヅブプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロゴゾドボポヴッン',
      ] );
	register_setting( 'mfc_settings_group', 'mfc_pause_delay', [
    'type'              => 'integer',
    'sanitize_callback' => 'absint',
    'default'           => 2, // seconds
	] );
//Register the audio toggle
	register_setting( 'mfc_settings_group', 'mfc_audio_enabled', [
    'type'              => 'boolean',
    'sanitize_callback' => 'rest_sanitize_boolean',
    'default'           => true,
	] );


        add_settings_section(
            'mfc_main_section',
            __( 'Matrix Code Options', 'matrix-falling-code' ),
            [ $this, 'render_section' ],
            'matrix-falling-code'
        );

        add_settings_field(
            'mfc_enabled',
            __( 'Enable Effect', 'matrix-falling-code' ),
            [ $this, 'render_enabled_field' ],
            'matrix-falling-code',
            'mfc_main_section'
        );
        add_settings_field(
            'mfc_color',
            __( 'Code Color', 'matrix-falling-code' ),
            [ $this, 'render_color_field' ],
            'matrix-falling-code',
            'mfc_main_section'
        );
        add_settings_field(
            'mfc_font_size',
            __( 'Font Size (px)', 'matrix-falling-code' ),
            [ $this, 'render_font_size_field' ],
            'matrix-falling-code',
            'mfc_main_section'
        );
        add_settings_field(
            'mfc_interval',
            __( 'Animation Interval (ms)', 'matrix-falling-code' ),
            [ $this, 'render_interval_field' ],
            'matrix-falling-code',
            'mfc_main_section'
        );
        add_settings_field(
            'mfc_opacity',
            __( 'Trail Opacity (0–1)', 'matrix-falling-code' ),
            [ $this, 'render_opacity_field' ],
            'matrix-falling-code',
            'mfc_main_section'
        );
	add_settings_field(
    	'mfc_characters',
    	__( 'Characters Set', 'matrix-falling-code' ),
    	[ $this, 'render_characters_field' ],
    	'matrix-falling-code',
    	'mfc_main_section'
        );
	add_settings_field(
    'mfc_pause_delay',
    __( 'Pause Delay (sec)', 'matrix-falling-code' ),
    [ $this, 'render_pause_delay_field' ],
    'matrix-falling-code',
    'mfc_main_section'
    );
	//Add a settings field
	add_settings_field(
    'mfc_audio_enabled',
    __( 'Enable Audio', 'matrix-falling-code' ),
    [ $this, 'render_audio_enabled_field' ],
    'matrix-falling-code',
    'mfc_main_section'
	);
    }

    /**
     * Enqueue admin assets (color picker, custom JS).
     *
     * @param string $hook_suffix Current admin page hook.
     */
    public function enqueue_assets( $hook_suffix ) {
        if ( 'settings_page_matrix-falling-code' !== $hook_suffix ) {
            return;
        }
        // WP Color Picker assets
        wp_enqueue_style( 'wp-color-picker' );
        wp_enqueue_script( 'wp-color-picker' );
        // Our tiny admin script to initialize the picker
        wp_enqueue_script(
            'mfc-admin-js',
            MFC_PLUGIN_URL . 'assets/js/admin.js',
            [ 'wp-color-picker', 'jquery' ],
            MFC_VERSION,
            true
        );
    }

    /**
     * Section description.
     */
    public function render_section() {
        echo '<p>' . esc_html__( 'Customize the Matrix code rain effect below.', 'matrix-falling-code' ) . '</p>';
    }

    // --- Field Renderers ---

    public function render_enabled_field() {
        $value = get_option( 'mfc_enabled', true );
        printf(
            '<input type="checkbox" name="mfc_enabled" value="1" %s />',
            checked( $value, true, false )
        );
    }

    public function render_color_field() {
        $value = get_option( 'mfc_color', '#0F0' );
        printf(
            '<input type="text" class="mfc-color-field" name="mfc_color" value="%s" />',
            esc_attr( $value )
        );
    }

    public function render_font_size_field() {
        $value = get_option( 'mfc_font_size', 16 );
        printf(
            '<input type="number" min="8" max="72" name="mfc_font_size" value="%d" />',
            absint( $value )
        );
    }

    public function render_interval_field() {
        $value = get_option( 'mfc_interval', 50 );
        printf(
            '<input type="number" min="10" max="1000" name="mfc_interval" value="%d" />',
            absint( $value )
        );
    }

    public function render_opacity_field() {
        $value = get_option( 'mfc_opacity', 0.05 );
        printf(
            '<input type="number" step="0.01" min="0" max="1" name="mfc_opacity" value="%s" />',
            esc_attr( $value )
        );
    }
	/**
 	* Render the Characters Set textarea.
 	*/
     public function render_characters_field() {
    	$value = get_option( 'mfc_characters', '' );
    	printf(
        '<textarea name="mfc_characters" rows="4" cols="50">%s</textarea><p class="description">%s</p>',
        esc_textarea( $value ),
        esc_html__( 'List all characters to use, no separators.', 'matrix-falling-code' )
    );
   }
	public function render_pause_delay_field() {
    $value = get_option( 'mfc_pause_delay', 2 );
    printf(
        '<input type="number" name="mfc_pause_delay" min="1" max="60" value="%d" /> <p class="description">%s</p>',
        esc_attr( $value ),
        esc_html__( 'How many seconds without mouse movement before the rain resumes.', 'matrix-falling-code' )
    );
     }
/**
 * Render the “Enable Audio” checkbox.
 */
public function render_audio_enabled_field() {
    $enabled = get_option( 'mfc_audio_enabled', true );
    printf(
        '<input type="checkbox" name="mfc_audio_enabled" value="1" %s />',
        checked( $enabled, true, false )
    );
    echo ' <label for="mfc_audio_enabled">'
       . esc_html__( 'Play background Matrix audio.', 'matrix-falling-code' )
       . '</label>';
}
    /**
     * Render the settings page markup.
     */
    public function render_settings_page() {
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( esc_html__( 'You do not have permission to access this page.', 'matrix-falling-code' ) );
        }
        ?>
        <div class="wrap">
            <h1><?php esc_html_e( 'Matrix Falling Code Settings', 'matrix-falling-code' ); ?></h1>
            <form method="post" action="options.php">
                <?php
                settings_fields( 'mfc_settings_group' );
                do_settings_sections( 'matrix-falling-code' );
                submit_button();
                ?>
            </form>
        </div>
        <?php
    }

    /**
     * Sanitize opacity to a float between 0 and 1.
     *
     * @param mixed $value Raw input.
     * @return float
     */
    public function sanitize_opacity( $value ) {
        $float = floatval( $value );
        return ( $float >= 0 && $float <= 1 ) ? $float : 0.05;
    }
}
