<?php

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

/**
 * Shortcode for Speaking Test.
 */
function qcld_stt_learning_mode_test_f($atts)
{

	$atts = shortcode_atts([
		'id' => 0,
	], $atts, 'qcld_stt_learning_mode_test');

	$post_id = intval($atts['id']);

	if (!$post_id) {
		return esc_html__('Please provide a valid test ID.', 'voice-widgets');
	}

	$levels = get_post_meta($post_id, 'vmwpmdp_test_levels', true);
	if ( ! is_array( $levels ) || empty( $levels ) ) {
		return esc_html__('No levels defined for this test.', 'voice-widgets');
	}

	ob_start();
?>
	<div id="vmwpmdp-stt-test-container-<?php echo $post_id; ?>" class="vmwpmdp-stt-test-container" data-post-id="<?php echo $post_id; ?>">
		<div class="vmwpmdp-stt-test-header">
			<h3><?php echo get_the_title($post_id); ?></h3>
		</div>

        <?php 
        $current_user = wp_get_current_user();
        $is_logged_in = is_user_logged_in();
        $student_name = $is_logged_in ? $current_user->display_name : '';
        $student_email = $is_logged_in ? $current_user->user_email : '';
        $readonly = $is_logged_in ? 'readonly' : '';

        if (is_user_logged_in()) {
            $current_user = wp_get_current_user();
            $first_name = $current_user->user_firstname;
            $last_name = $current_user->user_lastname;
            $full_name = trim( $first_name . ' ' . $last_name );
            
            $student_name = ! empty( $full_name ) ? $full_name : $current_user->display_name;
            $student_email = $current_user->user_email;
        }
            
        ?>
        <div class="vmwpmdp-student-id-fields">
            <div>
                <label><?php esc_html_e('Your Name', 'voice-widgets'); ?></label>
                <input type="text" class="vmwpmdp-student-input vmwpmdp-student-name" value="<?php echo esc_attr($student_name); ?>" <?php echo $readonly; ?> placeholder="<?php esc_html_e('Enter your name', 'voice-widgets'); ?>" style="width: 100%; padding: 8px; <?php echo $is_logged_in ? 'background: #f9f9f9;' : ''; ?>">
            </div>
            <div>
                <label><?php esc_html_e('Your Email', 'voice-widgets'); ?></label>
                <input type="email" class="vmwpmdp-student-input vmwpmdp-student-email" value="<?php echo esc_attr($student_email); ?>" <?php echo $readonly; ?> placeholder="<?php esc_html_e('Enter your email', 'voice-widgets'); ?>" style="width: 100%; padding: 8px; <?php echo $is_logged_in ? 'background: #f9f9f9;' : ''; ?>">
            </div>
        </div>

        <div class="vmwpmdp-test-levels-list">
            <?php foreach ( $levels as $index => $level ) : ?>
                <div class="vmwpmdp-test-level-item" data-level-index="<?php echo $index; ?>" data-level-label="<?php echo esc_attr( $level['label'] ); ?>">
                    <div class="vmwpmdp-completion-box">
                        <h4 style="margin: 0;"><?php echo esc_html( $level['label'] ); ?></h4>
                        <div class="vmwpmdp-completion-status" style="font-size: 0.8em; color: #999;"><span><?php esc_html_e('Not Recorded', 'voice-widgets'); ?></span></div>
                    </div>
                    <div class="vmwpmdp-speak-now-wrapper">
                        <p class="vmwpmdp-speak-now-text">"<?php echo esc_html( $level['text'] ); ?>"</p>
                        
                        <div class="vmwpmdp-stt-test-controls">
                            <button class="vmwpmdp-stt-test-record-btn" data-level-index="<?php echo $index; ?>">
                                <span class="vmwpmdp-btn-text"><?php esc_html_e('Start Speaking', 'qcld_wp_voice'); ?></span>
                            </button>
                            <div class="vmwpmdp-stt-test-status" style="display:none;"><?php esc_html_e('Recording...', 'qcld_wp_voice'); ?></div>

                            <div class="vmwpmdp-stt-test-result" style="display:none;">
                                <div class="vmwpmdp-result-badge"></div>
                                <div class="vmwpmdp-result-details"></div>
                                <button class="vmwpmdp-stt-test-retry-btn" style="display:none;"><?php esc_html_e('Try Again', 'qcld_wp_voice'); ?></button>
                            </div>
                        </div>
                    </div>
                </div>
            
            <?php endforeach; ?>
        </div>

        <div class="vmwpmdp-test-footer">
            <button class="button button-primary vmwpmdp-submit-full-test"><?php esc_html_e('Submit Full Test', 'voice-widgets'); ?></button>
            <div class="vmwpmdp-final-msg"></div>
        </div>
        
	</div>
	<?php
	return ob_get_clean();
}
add_shortcode('qcld_stt_learning_mode_test', 'qcld_stt_learning_mode_test_f');
