<?php

namespace MovieEngine\Admin\Player;

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

class PlayerSettings {

    public static function render($active_tab_slug = 'player') {
        settings_fields('movie_engine_player');
        $options = get_option('movie_engine_player_settings', []);
        $current_sub_tab = 'general';
        if (strpos($active_tab_slug, 'player_') === 0) {
            $current_sub_tab = str_replace('player_', '', $active_tab_slug);
        }
        if ($current_sub_tab === 'player') {
            $current_sub_tab = 'general';
        }
?>
        <input type="hidden" name="movie_engine_player_settings[active_sub_tab]" id="movie-engine-active-sub-tab" value="<?php echo esc_attr($current_sub_tab); ?>">
        <div class="movie-engine-settings-section">
            <div class="movie-engine-section-header">
                <h2><?php esc_html_e('Player Settings', 'cine-movie-engine'); ?></h2>
                <p class="description"><?php esc_html_e('Configure video player appearance, behavior, and performance.', 'cine-movie-engine'); ?></p>
            </div>

            <div class="movie-engine-sub-tabs">
                <a href="#general" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'general' ? 'active' : ''); ?>"><?php esc_html_e('General', 'cine-movie-engine'); ?></a>
                <a href="#sources" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'sources' ? 'active' : ''); ?>"><?php esc_html_e('Sources & Quality', 'cine-movie-engine'); ?></a>
                <a href="#subtitles" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'subtitles' ? 'active' : ''); ?>"><?php esc_html_e('Subtitles', 'cine-movie-engine'); ?></a>
                <a href="#audio" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'audio' ? 'active' : ''); ?>"><?php esc_html_e('Audio', 'cine-movie-engine'); ?></a>
                <a href="#series" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'series' ? 'active' : ''); ?>"><?php esc_html_e('Series', 'cine-movie-engine'); ?></a>
                <a href="#ui" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'ui' ? 'active' : ''); ?>"><?php esc_html_e('UI & Theme', 'cine-movie-engine'); ?></a>
                <a href="#performance" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'performance' ? 'active' : ''); ?>"><?php esc_html_e('Performance', 'cine-movie-engine'); ?></a>
                <a href="#security" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'security' ? 'active' : ''); ?>"><?php esc_html_e('Security', 'cine-movie-engine'); ?></a>
                <a href="#trailer" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'trailer' ? 'active' : ''); ?>"><?php esc_html_e('Background Trailer', 'cine-movie-engine'); ?></a>
            </div>

            <div class="movie-engine-sub-tab-content">

                <div id="general" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'general' ? 'active' : ''); ?>">
                    <?php self::render_tab('GeneralTab', $options); ?>
                </div>

                <div id="sources" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'sources' ? 'active' : ''); ?>">
                    <?php self::render_tab('SourcesTab', $options); ?>
                </div>

                <div id="subtitles" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'subtitles' ? 'active' : ''); ?>">
                    <?php self::render_tab('SubtitlesTab', $options); ?>
                </div>

                <div id="audio" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'audio' ? 'active' : ''); ?>">
                    <?php self::render_tab('AudioTab', $options); ?>
                </div>

                <div id="series" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'series' ? 'active' : ''); ?>">
                    <?php self::render_tab('SeriesTab', $options); ?>
                </div>
                <div id="ui" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'ui' ? 'active' : ''); ?>">
                    <?php self::render_tab('UITab', $options); ?>
                </div>

                <div id="performance" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'performance' ? 'active' : ''); ?>">
                    <?php self::render_tab('PerformanceTab', $options); ?>
                </div>
                <div id="security" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'security' ? 'active' : ''); ?>">
                    <?php self::render_tab('SecurityTab', $options); ?>
                </div>

                <div id="trailer" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'trailer' ? 'active' : ''); ?>">
                    <?php self::render_tab('TrailerTab', $options); ?>
                </div>

            </div>

            <div class="movie-engine-form-actions">
                <?php submit_button(esc_html__('Save Changes', 'cine-movie-engine'), 'primary me-btn-primary', 'submit', false); ?>
                <button type="button" id="movie-engine-player-reset-btn" class="movie-engine-reset-section-btn" data-tab="player_<?php echo esc_attr($current_sub_tab); ?>">
                    <span class="fa-solid fa-rotate-left"></span>
                    <?php esc_html_e('Reset Section', 'cine-movie-engine'); ?>
                </button>
            </div>
        </div>

<?php
    }

    /**
     * Render individual tab
     */
    private static function render_tab($tab_name, $options) {
        $tab_file = MOVIE_ENGINE_PATH . 'includes/Admin/Player/Tabs/' . $tab_name . '.php';

        if (file_exists($tab_file)) {
            require_once $tab_file;
            $tab_class = '\\MovieEngine\\Admin\\Player\\Tabs\\' . $tab_name;
            if (class_exists($tab_class) && method_exists($tab_class, 'render')) {
                $tab_class::render($options);
            }
        }
    }
}
