<?php

namespace MovieEngine\Admin\Player\Tabs;

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

class PerformanceTab {

    public static function render($options) {
?>
        <input type="hidden" name="movie_engine_player_settings[active_sub_tab]" value="player_performance">
        <div class="movie-engine-card">
            <div class="movie-engine-card-header">
                <h3><span class="fa-solid fa-gauge"></span> <?php esc_html_e('Performance', 'cine-movie-engine'); ?></h3>
            </div>
            <div class="movie-engine-card-body">
                <?php if (! has_action('movie_engine_render_pro_performance_protocols')) : ?>
                    <div class="me-pro-notice-box">
                        <span class="dashicons dashicons-lock"></span>
                        <div class="me-pro-notice-message">
                            <strong><?php esc_html_e('HLS & MPEG-DASH', 'cine-movie-engine'); ?></strong>
                            <p><?php esc_html_e('Support is a PRO feature. Upgrade to enable advanced streaming protocols.', 'cine-movie-engine'); ?></p>
                        </div>
                        <a href="<?php echo esc_url(\MovieEngine\Admin\Upgrade::get_pro_url('pricing')); ?>" target="_blank" class="button button-primary me-pro-notice-btn">
                            <?php esc_html_e('Upgrade to Pro', 'cine-movie-engine'); ?>
                        </a>
                    </div>
                <?php endif; ?>

                <?php
                if (has_action('movie_engine_render_pro_performance_protocols')) {
                    do_action('movie_engine_render_pro_performance_protocols', $options);
                } else {
                ?>
                    <div class="movie-engine-form-group me-locked-feature">
                        <label class="movie-engine-checkbox-label">
                            <input type="checkbox" disabled checked>
                            <span><?php esc_html_e('Enable HLS.js Playback', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>
                    <div class="movie-engine-form-group me-locked-feature">
                        <label class="movie-engine-checkbox-label">
                            <input type="checkbox" disabled checked>
                            <span><?php esc_html_e('Enable MPEG-DASH Support', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>
                <?php
                }
                ?>
                <div class="movie-engine-form-group">
                    <label class="movie-engine-checkbox-label">
                        <input type="hidden" name="movie_engine_player_settings[hardware_accel]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[hardware_accel]" value="1" <?php checked($options['movie_engine_hardware_accel'] ?? ($options['hardware_accel'] ?? 1), 1); ?>>
                        <span><?php esc_html_e('Hardware Acceleration Toggle', 'cine-movie-engine'); ?></span>
                    </label>
                </div>
                <div class="movie-engine-form-group">
                    <label for="max_buffer"><?php esc_html_e('Max Buffer Length (seconds)', 'cine-movie-engine'); ?></label>
                    <input type="number" id="max_buffer" name="movie_engine_player_settings[max_buffer]" value="<?php echo esc_attr($options['movie_engine_max_buffer'] ?? ($options['max_buffer'] ?? 30)); ?>" class="movie-engine-input" min="10">
                </div>
                <div class="movie-engine-form-group">
                    <label for="preload_strategy"><?php esc_html_e('Preload Strategy', 'cine-movie-engine'); ?></label>
                    <select id="preload_strategy" name="movie_engine_player_settings[preload_strategy]" class="movie-engine-select">
                        <?php
                        $preload_strategy = $options['movie_engine_preload_strategy'] ?? ($options['preload_strategy'] ?? 'metadata');
                        ?>
                        <option value="metadata" <?php selected($preload_strategy, 'metadata'); ?>><?php esc_html_e('Metadata', 'cine-movie-engine'); ?></option>
                        <option value="auto" <?php selected($preload_strategy, 'auto'); ?>><?php esc_html_e('Auto', 'cine-movie-engine'); ?></option>
                        <option value="none" <?php selected($preload_strategy, 'none'); ?>><?php esc_html_e('None', 'cine-movie-engine'); ?></option>
                    </select>
                </div>
                <div class="movie-engine-form-group">
                    <label for="buffering_strategy"><?php esc_html_e('Buffering Strategy', 'cine-movie-engine'); ?></label>
                    <select id="buffering_strategy" name="movie_engine_player_settings[buffering_strategy]" class="movie-engine-select">
                        <?php
                        $buffering_strategy = $options['movie_engine_buffering_strategy'] ?? ($options['buffering_strategy'] ?? 'conservative');
                        ?>
                        <option value="conservative" <?php selected($buffering_strategy, 'conservative'); ?>><?php esc_html_e('Conservative', 'cine-movie-engine'); ?></option>
                        <option value="aggressive" <?php selected($buffering_strategy, 'aggressive'); ?>><?php esc_html_e('Aggressive', 'cine-movie-engine'); ?></option>
                        <option value="adaptive" <?php selected($buffering_strategy, 'adaptive'); ?>><?php esc_html_e('Adaptive (Low Speed Optimized)', 'cine-movie-engine'); ?></option>
                    </select>
                </div>
                <div class="movie-engine-form-group">
                    <label class="movie-engine-checkbox-label">
                        <input type="hidden" name="movie_engine_player_settings[low_latency]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[low_latency]" value="1" <?php checked($options['movie_engine_low_latency'] ?? ($options['low_latency'] ?? 0), 1); ?>>
                        <span><?php esc_html_e('Enable Low-Latency Mode', 'cine-movie-engine'); ?></span>
                    </label>
                </div>
            </div>
        </div>

        <div class="movie-engine-card">
            <div class="movie-engine-card-header">
                <h3><span class="fa-solid fa-mobile-screen"></span> <?php esc_html_e('Compatibility', 'cine-movie-engine'); ?></h3>
            </div>
            <div class="movie-engine-card-body">
                <div class="movie-engine-form-group">
                    <label class="movie-engine-checkbox-label">
                        <input type="hidden" name="movie_engine_player_settings[enable_chromecast]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[enable_chromecast]" value="1" <?php checked($options['movie_engine_enable_chromecast'] ?? ($options['enable_chromecast'] ?? 1), 1); ?>>
                        <span><?php esc_html_e('Enable Chromecast', 'cine-movie-engine'); ?></span>
                    </label>
                </div>
                <div class="movie-engine-form-group">
                    <label class="movie-engine-checkbox-label">
                        <input type="hidden" name="movie_engine_player_settings[enable_airplay]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[enable_airplay]" value="1" <?php checked($options['movie_engine_enable_airplay'] ?? ($options['enable_airplay'] ?? 1), 1); ?>>
                        <span><?php esc_html_e('Enable AirPlay', 'cine-movie-engine'); ?></span>
                    </label>
                </div>
                <div class="movie-engine-form-group">
                    <label class="movie-engine-checkbox-label">
                        <input type="hidden" name="movie_engine_player_settings[mobile_double_tap]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[mobile_double_tap]" value="1" <?php checked($options['movie_engine_mobile_double_tap'] ?? ($options['mobile_double_tap'] ?? 1), 1); ?>>
                        <span><?php esc_html_e('Mobile Double Tap to Seek', 'cine-movie-engine'); ?></span>
                    </label>
                </div>
                <div class="movie-engine-form-group">
                    <label class="movie-engine-checkbox-label">
                        <input type="hidden" name="movie_engine_player_settings[tablet_ui]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[tablet_ui]" value="1" <?php checked($options['movie_engine_tablet_ui'] ?? ($options['tablet_ui'] ?? 1), 1); ?>>
                        <span><?php esc_html_e('Tablet UI Mode', 'cine-movie-engine'); ?></span>
                    </label>
                </div>
            </div>
        </div>
<?php
    }
}
