<?php

/**
 * The template for displaying episode information section
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/single-episode/section-info.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_ep_num = get_post_meta($movie_engine_post_id, '_movie_engine_episode_number', true);
$movie_engine_release_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_design_options = get_option('movie_engine_design_settings', []);
$movie_engine_show_title = isset($movie_engine_design_options['episode_show_title']) ? $movie_engine_design_options['episode_show_title'] : 1;
if (!$movie_engine_show_title) return;

?>

<div class="movie-engine-episode-info-section">
    <div class="me-info-content-wrapper">
        <div class="me-info-breadcrumbs">
            <span class="me-series-title"><?php echo esc_html(get_the_title($movie_engine_series_id)); ?></span>
            <span class="sep">/</span>
            <span class="me-season-ep">
                <?php
                printf(
                    /* translators: 1: season number, 2: episode number */
                    esc_html__('Season %1$d - Episode E%2$d', 'cine-movie-engine'),
                    absint($movie_engine_season_num),
                    absint($movie_engine_ep_num)
                );
                ?>
            </span>
        </div>

        <h1 class="me-episode-title"><?php echo esc_html(get_the_title()); ?></h1>
        <div class="me-info-meta-row me-pb-3">
            <div class="me-meta-left">
                <span class="me-ep-label">E<?php echo esc_html($movie_engine_ep_num); ?></span>

                <?php if ($movie_engine_release_date) : ?>
                    <span class="me-sep-dot"></span>
                    <span class="me-meta-item"><?php echo esc_html(date_i18n('M Y', strtotime($movie_engine_release_date))); ?></span>
                <?php endif; ?>

                <?php if ($movie_engine_runtime) : ?>
                    <span class="me-sep-dot"></span>
                    <span class="me-meta-item">
                        <span class="fa-solid fa-clock"></span>
                        <?php echo esc_html($movie_engine_runtime); ?> <?php esc_html_e('min', 'cine-movie-engine'); ?>
                    </span>
                <?php endif; ?>

                <?php
                $movie_engine_views = get_post_meta($movie_engine_post_id, '_movie_engine_views', true);
                $movie_engine_show_views = isset($movie_engine_design_options['episode_show_views']) ? $movie_engine_design_options['episode_show_views'] : 1;
                if ($movie_engine_show_views) :
                    $movie_engine_views_count = $movie_engine_views ? intval($movie_engine_views) : 0;
                    $movie_engine_formatted_views = \MovieEngine\Utils\Helpers::format_number($movie_engine_views_count);
                ?>
                    <span class="me-sep-dot"></span>
                    <span class="me-meta-item movie-engine-views-stat">
                        <span class="fa-solid fa-eye"></span>
                        <span class="me-count"><?php echo esc_html($movie_engine_formatted_views); ?></span>
                        <?php echo ' ';
                        echo esc_html(_n('view', 'views', $movie_engine_views_count, 'cine-movie-engine')); ?>
                    </span>
                <?php endif; ?>
            </div>
        </div>
        <div class="me-episode-description">
            <div class="me-synopsis-text">
                <?php echo esc_html(wp_trim_words(get_the_content(), 40)); ?>
            </div>
            <button class="me-read-more-btn me-open-details-modal"><?php esc_html_e('Read More', 'cine-movie-engine'); ?></button>
        </div>

        <div class="me-info-meta-row">
            <div class="me-meta-right">
                <?php
                $movie_engine_show_like = $movie_engine_design_options['episode_show_hero_like'] ?? 1;
                $movie_engine_show_share = $movie_engine_design_options['episode_show_hero_share'] ?? 1;
                $movie_engine_show_playlist = $movie_engine_design_options['episode_show_hero_playlist'] ?? 1;
                $movie_engine_show_watchlist = $movie_engine_design_options['episode_show_hero_watchlist'] ?? 1;

                if ($movie_engine_show_watchlist) :
                    $movie_engine_is_logged_in_for_watchlist = is_user_logged_in();
                    $movie_engine_is_in_watchlist = false;
                    if ($movie_engine_is_logged_in_for_watchlist) {
                        $movie_engine_is_in_watchlist = \MovieEngine\Frontend\Users\UserLibrary::in_watchlist(get_current_user_id(), $movie_engine_post_id);
                    }
                    $movie_engine_watchlist_text = $movie_engine_is_in_watchlist ? esc_html__('REMOVE WATCHLIST', 'cine-movie-engine') : esc_html__('ADD TO WATCHLIST', 'cine-movie-engine');
                    $movie_engine_watchlist_icon = $movie_engine_is_in_watchlist ? 'fa-solid fa-check' : 'fa-solid fa-plus';
                ?>
                    <button class="movie-engine-btn movie-engine-btn-secondary movie-engine-watchlist-btn <?php echo esc_attr($movie_engine_is_in_watchlist ? 'active' : ''); ?>"
                        data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>"
                        data-tooltip="<?php echo esc_attr($movie_engine_watchlist_text); ?>"
                        <?php if (!$movie_engine_is_logged_in_for_watchlist) : ?>data-login-url="<?php echo esc_url(home_url('/dashboard/')); ?>" <?php endif; ?>>
                        <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>
                    </button>
                <?php endif; ?>

                <?php
                if ($movie_engine_show_like || $movie_engine_show_share) {
                    \MovieEngine\Utils\TemplateLoader::get_template_part('global/actions-social', '', [
                        'post_id'    => $movie_engine_post_id,
                        '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>
    </div>
</div>