<?php

/**
 * The template for displaying playlist player page (YouTube-style)
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/pages/page-playlist.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_playlist_id = get_query_var('playlist');
$movie_engine_current_video_index = (int) get_query_var('v', 0);

if (!$movie_engine_playlist_id || !is_user_logged_in()) {
    wp_safe_redirect(home_url('/dashboard/?movie_engine_view=playlists'));
    exit;
}

$movie_engine_user_id = get_current_user_id();
$movie_engine_playlists = \MovieEngine\Frontend\Users\UserLibrary::get_playlists($movie_engine_user_id);

if (!isset($movie_engine_playlists[$movie_engine_playlist_id])) {
    wp_safe_redirect(home_url('/dashboard/?movie_engine_view=playlists'));
    exit;
}

$movie_engine_playlist = $movie_engine_playlists[$movie_engine_playlist_id];
$movie_engine_playlist_items = isset($movie_engine_playlist['items']) ? (array) $movie_engine_playlist['items'] : [];

if (empty($movie_engine_playlist_items)) {
    wp_safe_redirect(home_url('/dashboard/?movie_engine_view=playlists'));
    exit;
}
if ($movie_engine_current_video_index >= count($movie_engine_playlist_items) || $movie_engine_current_video_index < 0) {
    $movie_engine_current_video_index = 0;
}

$movie_engine_current_video_id = $movie_engine_playlist_items[$movie_engine_current_video_index];
$movie_engine_current_post = get_post($movie_engine_current_video_id);

if (!$movie_engine_current_post) {
    wp_safe_redirect(home_url('/dashboard/?movie_engine_view=playlists'));
    exit;
}

get_header();

$movie_engine_pl_options = get_option('movie_engine_player_settings', []);
$movie_engine_pl_slug = isset($movie_engine_pl_options['playlist_slug']) && !empty($movie_engine_pl_options['playlist_slug']) ? trim($movie_engine_pl_options['playlist_slug'], '/') : 'movie-engine-playlist';
?>

<div class="movie-engine-playlist-page">
    <div class="movie-engine-playlist-container">
        <div class="movie-engine-playlist-main">
            <div class="movie-engine-player-wrapper" data-gramm="false" data-gramm_editor="false" data-enable-grammarly="false">
                <?php echo \MovieEngine\Frontend\Player::render($movie_engine_current_video_id); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
                ?>
            </div>
            <div class="movie-engine-video-info">
                <div class="movie-engine-video-info-top">
                    <div class="movie-engine-video-info-left">
                        <h1 class="movie-engine-video-title">
                            <a href="<?php echo esc_url(get_permalink($movie_engine_current_video_id)); ?>">
                                <?php echo esc_html($movie_engine_current_post->post_title); ?>
                            </a>
                        </h1>
                        <div class="movie-engine-video-meta">
                            <?php
                            $movie_engine_post_type = get_post_type($movie_engine_current_video_id);
                            $movie_engine_type_label = $movie_engine_post_type === 'movie_engine_series' ? esc_html__('Series', 'cine-movie-engine') : esc_html__('Movie', 'cine-movie-engine');
                            $movie_engine_year = get_post_meta($movie_engine_current_video_id, '_movie_engine_year', true);
                            ?>
                            <span class="movie-engine-type"><?php echo esc_html($movie_engine_type_label); ?></span>
                            <?php if ($movie_engine_year): ?>
                                <span class="movie-engine-year"><?php echo esc_html($movie_engine_year); ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="movie-engine-video-info-right">
                        <div class="movie-engine-social-actions-wrapper">
                            <?php
                            global $post;
                            $post = $movie_engine_current_post;
                            setup_postdata($post);
                            \MovieEngine\Utils\TemplateLoader::get_template_part('global/actions-social');
                            wp_reset_postdata();
                            ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="movie-engine-playlist-sidebar">
            <div class="movie-engine-playlist-header">
                <div class="movie-engine-playlist-title">
                    <h2><?php echo esc_html($movie_engine_playlist['name']); ?></h2>
                    <p class="movie-engine-playlist-info">
                        <?php
                        printf(
                            /* translators: 1: current item index, 2: total items in playlist */
                            esc_html__('%1$d / %2$d', 'cine-movie-engine'),
                            (int) $movie_engine_current_video_index + 1,
                            (int) count($movie_engine_playlist_items)
                        );
                        ?>
                    </p>
                </div>
                <div class="movie-engine-playlist-header-actions">
                    <button class="movie-engine-share-playlist-btn me-share-btn"
                        data-url="<?php echo esc_url(home_url('/' . $movie_engine_pl_slug . '/?playlist=' . $movie_engine_playlist_id)); ?>"
                        data-title="<?php echo esc_attr($movie_engine_playlist['name']); ?>"
                        title="<?php esc_html_e('Share Playlist', 'cine-movie-engine'); ?>">
                        <span class="fa-solid fa-share-nodes"></span>
                    </button>
                    <?php
                    $movie_engine_dashboard_pages = get_pages([
                        // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
                        'meta_key' => '_wp_page_template',
                        // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
                        'meta_value' => 'page-dashboard.php',
                        'number' => 1
                    ]);
                    $movie_engine_dashboard_url = !empty($movie_engine_dashboard_pages) ? get_permalink($movie_engine_dashboard_pages[0]->ID) : home_url('/dashboard/');
                    $movie_engine_back_url = add_query_arg('movie_engine_view', 'playlists', $movie_engine_dashboard_url);
                    ?>
                    <a href="<?php echo esc_url($movie_engine_back_url); ?>" class="movie-engine-back-link" title="<?php esc_html_e('Back to Playlists', 'cine-movie-engine'); ?>">
                        <span class="fa-solid fa-arrow-left"></span>
                        <span class="movie-engine-back-text"><?php esc_html_e('Back', 'cine-movie-engine'); ?></span>
                    </a>
                </div>
            </div>

            <div class="movie-engine-playlist-items">
                <?php foreach ($movie_engine_playlist_items as $movie_engine_index => $movie_engine_item_id):
                    $movie_engine_item = get_post($movie_engine_item_id);
                    if (!$movie_engine_item) continue;

                    $movie_engine_is_current = ($movie_engine_index === $movie_engine_current_video_index);

                    $movie_engine_item_url = add_query_arg([
                        'playlist' => $movie_engine_playlist_id,
                        'v' => $movie_engine_index
                    ], home_url('/' . $movie_engine_pl_slug . '/'));

                    $movie_engine_thumbnail = get_the_post_thumbnail_url($movie_engine_item_id, 'medium');
                    $movie_engine_item_type = get_post_type($movie_engine_item_id);
                    $movie_engine_item_type_label = $movie_engine_item_type === 'movie_engine_series' ? esc_html__('Series', 'cine-movie-engine') : esc_html__('Movie', 'cine-movie-engine');
                ?>
                    <a href="<?php echo esc_url($movie_engine_item_url); ?>"
                        class="movie-engine-playlist-item <?php echo $movie_engine_is_current ? 'active' : ''; ?>">
                        <div class="movie-engine-item-number"><?php echo esc_html($movie_engine_index + 1); ?></div>
                        <div class="movie-engine-item-thumbnail">
                            <?php if ($movie_engine_thumbnail): ?>
                                <img src="<?php echo esc_url($movie_engine_thumbnail); ?>" alt="<?php echo esc_attr($movie_engine_item->post_title); ?>">
                            <?php else: ?>
                                <div class="movie-engine-no-thumb">
                                    <span class="fa-solid fa-film"></span>
                                </div>
                            <?php endif; ?>
                            <?php if ($movie_engine_is_current): ?>
                                <div class="movie-engine-playing-badge">
                                    <span class="fa-solid fa-play"></span>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="movie-engine-item-details">
                            <h3><?php echo esc_html($movie_engine_item->post_title); ?></h3>
                            <span class="movie-engine-item-type"><?php echo esc_html($movie_engine_item_type_label); ?></span>
                        </div>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>

<?php get_footer(); ?>