<?php

/**
 * The template for displaying episode hero section
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/single-episode/hero.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.0.0
 * @since 1.0.0
 */
if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

$movie_engine_post_id = get_the_ID();
$movie_engine_series_id = get_post_meta($movie_engine_post_id, '_movie_engine_series_id', true);
$movie_engine_season_num = get_post_meta($movie_engine_post_id, '_movie_engine_season_number', true);
$movie_engine_episode_num = get_post_meta($movie_engine_post_id, '_movie_engine_episode_number', true);
$movie_engine_air_date = get_post_meta($movie_engine_post_id, '_movie_engine_air_date', true);
$movie_engine_runtime = get_post_meta($movie_engine_post_id, '_movie_engine_runtime', true);

$movie_engine_backdrop = get_post_meta($movie_engine_series_id, '_movie_engine_backdrop_url', true);
if (!$movie_engine_backdrop) {
    $movie_engine_backdrop = get_the_post_thumbnail_url($movie_engine_series_id, 'full');
}
$movie_engine_design = get_option('movie_engine_design_settings', []);
$movie_engine_show_title = isset($movie_engine_design['episode_show_title']) ? $movie_engine_design['episode_show_title'] : 1;
?>

<?php
$movie_engine_hero_opacity = ($movie_engine_design['episode_hero_overlay_opacity'] ?? 70) / 100;
?>
<div class="movie-engine-movie-hero me-episode-hero" style="background-image: url('<?php echo esc_url($movie_engine_backdrop); ?>');">
    <div class="movie-engine-hero-overlay" style="background: rgba(0,0,0,<?php echo esc_attr($movie_engine_hero_opacity); ?>);"></div>
    <div class="movie-engine-hero-container">
        <div class="movie-engine-hero-info-wrapper">
            <div class="movie-engine-info-wrapper">
                <div class="movie-engine-genre-list me-episode-breadcrumbs">
                    <a href="<?php echo esc_url(get_permalink($movie_engine_series_id)); ?>"><?php echo esc_html(get_the_title($movie_engine_series_id)); ?></a>
                    <span class="sep">/</span>
                    <span>
                        <?php
                        printf(
                            /* translators: %d: season number */
                            esc_html__('S%d', 'cine-movie-engine'),
                            absint($movie_engine_season_num)
                        );
                        ?>
                    </span>
                    <span class="sep">/</span>
                    <span>
                        <?php
                        printf(
                            /* translators: %d: episode number */
                            esc_html__('E%d', 'cine-movie-engine'),
                            absint($movie_engine_episode_num)
                        );
                        ?>
                    </span>
                </div>

                <?php if ($movie_engine_show_title) : ?>
                    <h1 class="movie-engine-movie-title me-text-image"><?php echo esc_html(get_the_title()); ?></h1>

                    <div class="movie-engine-meta-row">
                        <?php
                        $movie_engine_user_rating = get_post_meta($movie_engine_post_id, '_movie_engine_user_rating', true);
                        if ($movie_engine_user_rating && $movie_engine_user_rating > 0) : ?>
                            <span class="movie-engine-meta-badge me-rating-badge">
                                <span class="fa-solid fa-star"></span>
                                <?php echo esc_html(number_format((float)$movie_engine_user_rating, 1)); ?>
                            </span>
                        <?php endif; ?>

                        <?php if ($movie_engine_air_date) : ?>
                            <span class="movie-engine-meta-item"><?php echo esc_html(date_i18n(get_option('date_format'), strtotime($movie_engine_air_date))); ?></span>
                        <?php endif; ?>

                        <?php if ($movie_engine_runtime) : ?>
                            <span class="movie-engine-meta-item">
                                <span class="fa-solid fa-clock"></span>
                                <?php
                                printf(
                                    /* translators: %d: runtime in minutes */
                                    esc_html__('%d Min', 'cine-movie-engine'),
                                    absint($movie_engine_runtime)
                                );
                                ?>
                            </span>
                        <?php endif; ?>

                        <span class="movie-engine-meta-badge me-quality-badge"><?php esc_html_e('Episode', 'cine-movie-engine'); ?></span>
                    </div>

                    <div class="movie-engine-short-synopsis">
                        <div class="me-synopsis-text">
                            <?php echo esc_html(wp_trim_words(get_the_content(), 30)); ?>
                        </div>
                        <button class="movie-engine-read-more-btn me-open-details-modal">
                            <?php esc_html_e('Read More', 'cine-movie-engine'); ?>
                        </button>
                    </div>

                    <div class="movie-engine-hero-actions">
                        <div class="me-primary-actions">
                            <?php
                            $movie_engine_show_partial = apply_filters('movie_engine_partial_paywall_duration', false, $movie_engine_post_id);
                            if (! \MovieEngine\Payments\Membership::is_restricted($movie_engine_post_id) || $movie_engine_show_partial !== false) : ?>
                                <a href="#movie-engine-player" class="movie-engine-btn movie-engine-btn-play">
                                    <?php esc_html_e('WATCH NOW', 'cine-movie-engine'); ?>
                                    <span class="fa-solid fa-play"></span>
                                </a>
                            <?php endif; ?>

                            <?php \MovieEngine\Utils\TemplateLoader::get_template_part('global/button-ppv', '', ['compact' => true]); ?>

                            <?php
                            $movie_engine_show_watchlist = $movie_engine_design['episode_show_hero_watchlist'] ?? 1;
                            if ($movie_engine_show_watchlist) :
                                $movie_engine_is_in_watchlist = false;
                                if (is_user_logged_in()) {
                                    $movie_engine_is_in_watchlist = \MovieEngine\Frontend\Users\UserLibrary::in_watchlist(get_current_user_id(), $movie_engine_post_id);
                                }
                                $movie_engine_watchlist_btn_class = 'movie-engine-btn movie-engine-btn-secondary movie-engine-watchlist-btn' . ($movie_engine_is_in_watchlist ? ' active' : '');
                                $movie_engine_watchlist_text = $movie_engine_is_in_watchlist ? esc_html__('Remove Watchlist', 'cine-movie-engine') : esc_html__('WATCH LATER', 'cine-movie-engine');
                                $movie_engine_watchlist_icon = $movie_engine_is_in_watchlist ? 'fa-solid fa-check' : 'fa-solid fa-plus';
                            ?>
                                <a href="#" class="movie-engine-btn movie-engine-btn-secondary <?php echo esc_attr($movie_engine_watchlist_btn_class); ?>" data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>">
                                    <span class="movie-engine-watchlist-text"><?php echo esc_html($movie_engine_watchlist_text); ?></span>
                                    <span class="<?php echo esc_attr($movie_engine_watchlist_icon); ?>"></span>
                                </a>
                            <?php endif; ?>
                        </div>

                        <div class="me-icon-actions">
                            <?php
                            $movie_engine_show_like = $movie_engine_design['episode_show_hero_like'] ?? 1;
                            $movie_engine_show_share = $movie_engine_design['episode_show_hero_share'] ?? 1;
                            $movie_engine_show_playlist = $movie_engine_design['episode_show_hero_playlist'] ?? 1;

                            if ($movie_engine_show_like || $movie_engine_show_share) {
                                \MovieEngine\Utils\TemplateLoader::get_template_part('global/actions-social', '', [
                                    'show_like' => $movie_engine_show_like,
                                    'show_share' => $movie_engine_show_share
                                ]);
                            }

                            if ($movie_engine_show_playlist) {
                                \MovieEngine\Utils\TemplateLoader::get_template_part('global/button-playlist');
                            }
                            ?>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>