<?php

namespace MovieEngine\Admin\Player\Tabs;

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

class GeneralTab {

    public static function render($options) {
?>
        <input type="hidden" name="movie_engine_player_settings[active_sub_tab]" value="player_general">
        <div class="movie-engine-card">
            <div class="movie-engine-card-header">
                <h3><span class="fa-solid fa-film3"></span> <?php esc_html_e('Player Library', 'cine-movie-engine'); ?></h3>
            </div>
            <div class="movie-engine-card-body">
                <div class="movie-engine-form-group">
                    <label for="movie_engine_player_library"><?php esc_html_e('Select Player Library', 'cine-movie-engine'); ?></label>
                    <?php
                    $is_pro = apply_filters('movie_engine_is_pro', false);
                    $player_lib = $options['movie_engine_player_library'] ?? ($options['player_library'] ?? 'plyr');

                    // Force default if not pro
                    if (!$is_pro && in_array($player_lib, ['shaka', 'clappr', 'artplayer'])) {
                        $player_lib = 'plyr';
                    }
                    ?>
                    <select id="movie_engine_player_library" name="movie_engine_player_settings[player_library]" class="movie-engine-select">
                        <option value="plyr" <?php selected($player_lib, 'plyr'); ?>>
                            <?php esc_html_e('Plyr.js (Simple & Fast)', 'cine-movie-engine'); ?>
                        </option>
                        <option value="shaka" <?php selected($player_lib, 'shaka'); ?> <?php echo !$is_pro ? 'disabled' : ''; ?>>
                            <?php esc_html_e('Shaka Player (Google - DRM/Streaming)', 'cine-movie-engine'); ?> <?php echo !$is_pro ? esc_html__('(Pro Only)', 'cine-movie-engine') : ''; ?>
                        </option>
                        <option value="clappr" <?php selected($player_lib, 'clappr'); ?> <?php echo !$is_pro ? 'disabled' : ''; ?>>
                            <?php esc_html_e('Clappr Player ⭐ (All-in-One)', 'cine-movie-engine'); ?> <?php echo !$is_pro ? esc_html__('(Pro Only)', 'cine-movie-engine') : ''; ?>
                        </option>
                        <option value="artplayer" <?php selected($player_lib, 'artplayer'); ?> <?php echo !$is_pro ? 'disabled' : ''; ?>>
                            <?php esc_html_e('ArtPlayer (Modern UI)', 'cine-movie-engine'); ?> <?php echo !$is_pro ? esc_html__('(Pro Only)', 'cine-movie-engine') : ''; ?>
                        </option>
                    </select>
                    <p class="movie-engine-help-text"><?php esc_html_e('Clappr is recommended for maximum compatibility. Shaka for DRM content.', 'cine-movie-engine'); ?></p>

                    <?php if (!$is_pro) : ?>
                        <div class="me-pro-notice-box">
                            <div class="me-pro-notice-message">
                                <span class="dashicons dashicons-lock"></span>
                                <strong><?php esc_html_e('Pro Feature:', 'cine-movie-engine'); ?></strong>
                                <p><?php esc_html_e('Shaka and Clappr players allow advanced streaming (HLS/DASH) and DRM protection.', '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 Unlock', 'cine-movie-engine'); ?></a>
                        </div>
                    <?php endif; ?>
                </div>

            </div>
        </div>

        <div class="movie-engine-card">
            <div class="movie-engine-card-header">
                <h3><span class="fa-solid fa-play"></span> <?php esc_html_e('Playback Behavior', '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[autoplay]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[autoplay]" value="1" <?php checked($options['movie_engine_autoplay'] ?? ($options['autoplay'] ?? 0), 1); ?>>
                        <span><?php esc_html_e('Enable Autoplay', '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[resume_playback]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[resume_playback]" value="1" <?php checked($options['movie_engine_resume_playback'] ?? ($options['resume_playback'] ?? 1), 1); ?>>
                        <span><?php esc_html_e('Enable Resume Playback (Continue Watching)', 'cine-movie-engine'); ?></span>
                    </label>
                </div>
                <div class="movie-engine-form-group">
                    <label for="movie_engine_default_volume"><?php esc_html_e('Default Volume Level', 'cine-movie-engine'); ?></label>
                    <?php $def_vol = $options['movie_engine_default_volume'] ?? ($options['default_volume'] ?? 80); ?>
                    <div class="movie-engine-range-wrapper">
                        <input type="range" id="movie_engine_default_volume" name="movie_engine_player_settings[default_volume]" min="0" max="100" value="<?php echo esc_attr($def_vol); ?>" class="movie-engine-range" oninput="document.getElementById('volume_output').value = this.value + '%'">
                        <output id="volume_output"><?php echo esc_html($def_vol); ?>%</output>
                    </div>
                </div>
                <div class="movie-engine-form-group">
                    <label class="movie-engine-checkbox-label">
                        <input type="hidden" name="movie_engine_player_settings[mute_on_start]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[mute_on_start]" value="1" <?php checked($options['movie_engine_mute_on_start'] ?? ($options['mute_on_start'] ?? 0), 1); ?>>
                        <span><?php esc_html_e('Mute on Start', 'cine-movie-engine'); ?></span>
                    </label>
                </div>
                <div class="movie-engine-form-group">
                    <label for="movie_engine_controls_timeout"><?php esc_html_e('Show Controls Timeout (seconds)', 'cine-movie-engine'); ?></label>
                    <input type="number" id="movie_engine_controls_timeout" name="movie_engine_player_settings[controls_timeout]" value="<?php echo esc_attr($options['movie_engine_controls_timeout'] ?? ($options['controls_timeout'] ?? 3)); ?>" class="movie-engine-input" min="0" placeholder="0 = never hide">
                </div>
            </div>
        </div>

        <div class="movie-engine-card">
            <div class="movie-engine-card-header">
                <h3><span class="fa-solid fa-gears"></span> <?php esc_html_e('Features', '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[keyboard_shortcuts]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[keyboard_shortcuts]" value="1" <?php checked($options['movie_engine_keyboard_shortcuts'] ?? ($options['keyboard_shortcuts'] ?? 1), 1); ?>>
                        <span><?php esc_html_e('Enable Keyboard Shortcuts', '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[theater_mode]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[theater_mode]" value="1" <?php checked($options['movie_engine_theater_mode'] ?? ($options['theater_mode'] ?? 1), 1); ?>>
                        <span><?php esc_html_e('Enable Theater Mode', '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[pip_mode]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[pip_mode]" value="1" <?php checked($options['movie_engine_pip_mode'] ?? ($options['pip_mode'] ?? 1), 1); ?>>
                        <span><?php esc_html_e('Enable Picture-in-Picture Mode (PiP)', '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[mini_player]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[mini_player]" value="1" <?php checked($options['movie_engine_mini_player'] ?? ($options['mini_player'] ?? 0), 1); ?>>
                        <span><?php esc_html_e('Enable Mini Player (Floating)', '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[download_button]" value="0">
                        <input type="checkbox" name="movie_engine_player_settings[download_button]" value="1" <?php checked($options['movie_engine_download_button'] ?? ($options['download_button'] ?? 0), 1); ?>>
                        <span><?php esc_html_e('Enable Download Button', '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-list"></span> <?php esc_html_e('Playlist Player Settings', 'cine-movie-engine'); ?></h3>
            </div>
            <div class="movie-engine-card-body">
                <div class="movie-engine-form-group">
                    <label for="movie_engine_playlist_slug"><?php esc_html_e('Playlist Player Slug', 'cine-movie-engine'); ?></label>
                    <input type="text" id="movie_engine_playlist_slug" name="movie_engine_player_settings[playlist_slug]" value="<?php echo esc_attr($options['movie_engine_playlist_slug'] ?? ($options['playlist_slug'] ?? 'movie-engine-playlist')); ?>" class="movie-engine-input" placeholder="movie-engine-playlist">
                    <p class="movie-engine-help-text"><?php esc_html_e('The URL slug for the dedicated playlist player page (e.g., domain.com/me-playlist/). Change this if you have a conflict with an existing page.', 'cine-movie-engine'); ?></p>
                </div>
                <div class="movie-engine-alert me-alert-info">
                    <p><span class="fa-solid fa-circle-info"></span> <?php esc_html_e('After changing the slug, please go to Settings > Permalinks and click "Save Changes" to apply.', 'cine-movie-engine'); ?></p>
                </div>
            </div>
        </div>
<?php
    }
}
