<?php

/**
 * The template for displaying single episode pages
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/single-episode.php.
 *
 * HOWEVER, on occasion MovieEngine will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @note Please do not delete our CSS classes when you customize, as it may break functionality.
 * @package Movie_Engine
 * @subpackage Movie_Engine/templates
 * @version 1.3.0
 * @since 1.0.0
 */

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

$movie_engine_post_id = get_queried_object_id();

get_header();

/**
 * Hooks for before the episode wrapper
 */
do_action('movie_engine_before_single_episode_wrapper');

$movie_engine_design_options = get_option('movie_engine_design_settings', []);
$movie_engine_episode_container_type = $movie_engine_design_options['episode_container_type'] ?? 'container';
$movie_engine_episode_container_class = 'movie-engine-' . esc_attr($movie_engine_episode_container_type);

if ($movie_engine_episode_container_type === 'container-fluid') {
    $movie_engine_episode_container_class .= ' me-px-6';
}

$movie_engine_episode_layout = $movie_engine_design_options['episode_single_layout'] ?? 'sidebar';
$movie_engine_show_sidebar = isset($movie_engine_design_options['episode_show_sidebar']) ? (bool)$movie_engine_design_options['episode_show_sidebar'] : true;
?>

<div class="movie-engine-single-movie-wrapper movie-engine-plugin me-single-episode movie-engine-layout-<?php echo esc_attr($movie_engine_episode_layout); ?>" data-post-id="<?php the_ID(); ?>">

    <?php if ($movie_engine_episode_layout === 'sidebar') : ?>
        <div id="movie-engine-player" class="movie-engine-top-player-area movie-engine-player-section movie-engine-player-sidebar <?php echo esc_attr($movie_engine_episode_container_class); ?>">
            <div class="movie-engine-main-player-col">
                <div class="movie-engine-player-ratio-container">
                    <div class="movie-engine-player-inner">
                        <?php
                        echo \MovieEngine\Frontend\Player::render($movie_engine_post_id ?: get_the_ID()); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

                        \MovieEngine\Utils\TemplateLoader::get_template_part('single-episode/player-data');
                        ?>
                    </div>
                </div>
            </div>

            <?php if ($movie_engine_show_sidebar) : ?>
                <div class="movie-engine-playlist-sidebar-col">
                    <div class="movie-engine-playlist-scroll-container">
                        <?php \MovieEngine\Utils\TemplateLoader::get_template_part('single-episode/sidebar'); ?>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    <?php else : ?>
        <div id="movie-engine-player" class="movie-engine-player-section movie-engine-player-fullwidth">
            <div class="movie-engine-container-fluid me-px-6">
                <?php
                echo \MovieEngine\Frontend\Player::render($movie_engine_post_id ?: get_the_ID()); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

                \MovieEngine\Utils\TemplateLoader::get_template_part('single-episode/player-data');
                ?>
            </div>
        </div>
    <?php endif; ?>

    <div class="<?php echo esc_attr($movie_engine_episode_container_class); ?> me-main-content">
        <div class="me-py-40">
            <?php if ($movie_engine_episode_layout === 'sidebar') : ?>
                <div class="movie-engine-content-below-player">
                    <div class="movie-engine-content-left-col">
                        <?php \MovieEngine\Utils\TemplateLoader::get_template_part('single-episode/section-info'); ?>
                        <?php \MovieEngine\Utils\TemplateLoader::get_template_part('single-episode/navigation'); ?>

                        <?php
                        /**
                         * Hooks for ad before content
                         */
                        do_action('movie_engine_ad_before_content');

                        /**
                         * Hooks for single episode sections (Trending, Related, Episodes, Reviews)
                         * 
                         * @hooked MovieEngine\Frontend\TemplateHooks::render_episodes_section - 10
                         * @hooked MovieEngine\Frontend\TemplateHooks::render_episode_trending_section - 20
                         * @hooked MovieEngine\Frontend\TemplateHooks::render_episode_related_section - 30
                         * @hooked MovieEngine\Frontend\TemplateHooks::render_reviews_section - 100
                         */
                        do_action('movie_engine_single_episode_sections');
                        ?>
                    </div>
                    <div class="movie-engine-content-right-col">
                        <div class="me-sidebar-extra-content">
                            <?php \MovieEngine\Utils\TemplateLoader::get_template_part('single-episode/sidebar-trending'); ?>
                        </div>
                    </div>
                </div>
            <?php else : ?>
                <?php \MovieEngine\Utils\TemplateLoader::get_template_part('single-episode/section-info'); ?>
                <?php \MovieEngine\Utils\TemplateLoader::get_template_part('single-episode/navigation'); ?>

                <?php
                /**
                 * Hooks for ad before content
                 */
                do_action('movie_engine_ad_before_content');

                /**
                 * Hooks for single episode sections (Trending, Related, Episodes, Reviews)
                 * 
                 * @hooked MovieEngine\Frontend\TemplateHooks::render_episodes_section - 10
                 * @hooked MovieEngine\Frontend\TemplateHooks::render_episode_trending_section - 20
                 * @hooked MovieEngine\Frontend\TemplateHooks::render_episode_related_section - 30
                 * @hooked MovieEngine\Frontend\TemplateHooks::render_reviews_section - 100
                 */
                do_action('movie_engine_single_episode_sections');

                ?>
            <?php endif; ?>
        </div>
    </div>
    <?php
    /**
     * Hooks for after the episode wrapper
     */
    do_action('movie_engine_after_single_episode_wrapper');

    ?>
</div>

<?php
get_footer();
