<?php

/**
 * The template for displaying fullwidth movie hero section
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/single-movie/hero-fullwidth.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_meta = get_post_meta($movie_engine_post_id);
$movie_engine_backdrop = $movie_engine_meta['_movie_engine_backdrop_url'][0] ?? get_the_post_thumbnail_url($movie_engine_post_id, 'full');

$movie_engine_design = get_option('movie_engine_design_settings', []);
$movie_engine_show_title = isset($movie_engine_design['movie_show_title']) ? $movie_engine_design['movie_show_title'] : 1;

$movie_engine_year = $movie_engine_meta['_movie_engine_year'][0] ?? '';
$movie_engine_runtime = $movie_engine_meta['_movie_engine_runtime'][0] ?? '';
$movie_engine_quality = $movie_engine_meta['_movie_engine_quality'][0] ?? 'HD';

$movie_engine_trailer = get_post_meta($movie_engine_post_id, '_movie_engine_trailer', true);
$movie_engine_trailer_url = '';
$movie_engine_trailer_platform = '';
$movie_engine_trailer_id = '';

if ($movie_engine_trailer) {
    $movie_engine_trailer_url = esc_url($movie_engine_trailer);

    if (preg_match('/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/', $movie_engine_trailer, $movie_engine_matches)) {
        $movie_engine_trailer_platform = 'youtube';
        $movie_engine_trailer_id = $movie_engine_matches[1];
    } elseif (preg_match('/(?:vimeo\.com\/|player\.vimeo\.com\/video\/)(\d+)/', $movie_engine_trailer, $movie_engine_matches)) {
        $movie_engine_trailer_platform = 'vimeo';
        $movie_engine_trailer_id = $movie_engine_matches[1];
    } elseif (preg_match('/(?:dailymotion\.com\/video\/|dai\.ly\/)([a-zA-Z0-9]+)/', $movie_engine_trailer, $movie_engine_matches)) {
        $movie_engine_trailer_platform = 'dailymotion';
        $movie_engine_trailer_id = $movie_engine_matches[1];
    } else {
        $movie_engine_trailer_platform = 'direct';
        $movie_engine_trailer_id = $movie_engine_trailer_url;
    }
}

$movie_engine_player_settings = get_option('movie_engine_player_settings', []);
$movie_engine_trailer_enabled = isset($movie_engine_player_settings['movie_engine_trailer_enable']) ? (bool)$movie_engine_player_settings['movie_engine_trailer_enable'] : true;
$movie_engine_trailer_mobile = isset($movie_engine_player_settings['movie_engine_trailer_mobile']) ? (bool)$movie_engine_player_settings['movie_engine_trailer_mobile'] : false;
$movie_engine_trailer_loop = isset($movie_engine_player_settings['movie_engine_trailer_loop']) ? (bool)$movie_engine_player_settings['movie_engine_trailer_loop'] : true;
$movie_engine_trailer_pause_scroll = isset($movie_engine_player_settings['movie_engine_trailer_pause_on_scroll']) ? (bool)$movie_engine_player_settings['movie_engine_trailer_pause_on_scroll'] : true;
$movie_engine_trailer_opacity = isset($movie_engine_player_settings['movie_engine_trailer_overlay_opacity']) ? intval($movie_engine_player_settings['movie_engine_trailer_overlay_opacity']) : 60;
$movie_engine_trailer_blur = isset($movie_engine_player_settings['movie_engine_trailer_blur']) ? (bool)$movie_engine_player_settings['movie_engine_trailer_blur'] : false;
$movie_engine_trailer_fade = isset($movie_engine_player_settings['movie_engine_trailer_fade_in']) ? intval($movie_engine_player_settings['movie_engine_trailer_fade_in']) : 800;
?>

<div class="movie-engine-movie-hero" style="background-image: url('<?php echo esc_url($movie_engine_backdrop); ?>');">
    <?php if ($movie_engine_trailer_url && $movie_engine_trailer_platform && $movie_engine_trailer_enabled) : ?>
        <div class="movie-engine-hero-video <?php echo $movie_engine_trailer_blur ? 'me-trailer-blurred' : ''; ?>"
            data-trailer-url="<?php echo esc_attr($movie_engine_trailer_url); ?>"
            data-trailer-platform="<?php echo esc_attr($movie_engine_trailer_platform); ?>"
            data-trailer-id="<?php echo esc_attr($movie_engine_trailer_id); ?>"
            data-backdrop="<?php echo esc_url($movie_engine_backdrop); ?>"
            data-mobile="<?php echo $movie_engine_trailer_mobile ? '1' : '0'; ?>"
            data-loop="<?php echo $movie_engine_trailer_loop ? '1' : '0'; ?>"
            data-pause-scroll="<?php echo $movie_engine_trailer_pause_scroll ? '1' : '0'; ?>"
            data-opacity="<?php echo esc_attr($movie_engine_trailer_opacity); ?>"
            data-fade-in="<?php echo esc_attr($movie_engine_trailer_fade); ?>">
            <div class="movie-engine-hero-video-poster" style="background-image: url('<?php echo esc_url($movie_engine_backdrop); ?>');"></div>
            <div class="movie-engine-hero-video-controls">
                <button type="button" class="movie-engine-video-control-btn movie-engine-hero-fullscreen-btn" aria-label="Toggle Fullscreen">
                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="me-icon-svg">
                        <path d="M8 3H5a2 2 0 0 0-2 2v3"></path>
                        <path d="M16 3h3a2 2 0 0 1 2 2v3"></path>
                        <path d="M16 21h3a2 2 0 0 0 2-2v-3"></path>
                        <path d="M8 21H5a2 2 0 0 1-2-2v-3"></path>
                    </svg>
                </button>
                <button type="button" class="movie-engine-video-control-btn movie-engine-hero-volume-btn" aria-label="Toggle Volume">
                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="me-icon-svg">
                        <path d="M11 5L6 9H2v6h4l5 4V5z"></path>
                        <path d="M19.07 4.93a10 10 0 0 1 0 14.14" class="me-v-wave-2"></path>
                        <path d="M15.54 8.46a5 5 0 0 1 0 7.07" class="me-v-wave-1"></path>
                    </svg>
                </button>
            </div>
        </div>
    <?php endif; ?>

    <div class="movie-engine-container-fluid me-hero-container me-z-100 me-px-6 me-py-6">
        <div class="movie-engine-hero-info-wrapper">
            <div class="movie-engine-info-wrapper">

                <div class="movie-engine-genre-list">
                    <?php echo \MovieEngine\Utils\Helpers::kses_post(get_the_term_list($movie_engine_post_id, 'movie_engine_genre', '', ' • ', '') ?: ''); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML 
                    ?>
                </div>

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

                <div class="movie-engine-meta-row">
                    <?php
                    $movie_engine_imdb_rating_val = get_post_meta($movie_engine_post_id, '_movie_engine_rating', true);
                    if ($movie_engine_imdb_rating_val && $movie_engine_imdb_rating_val > 0) : ?>
                        <span class="movie-engine-meta-badge me-imdb-badge" title="<?php esc_attr_e('IMDb Rating', 'cine-movie-engine'); ?>">
                            <svg width="32" height="16" viewBox="0 0 64 32" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <rect width="64" height="32" rx="4" fill="#F5C518" />
                                <path d="M8 8h3v16H8V8zm6 0h4.5l2 10.5L22.5 8H27v16h-3V12.5L21.5 24h-2L17 12.5V24h-3V8zm14 0h6c2.5 0 4 1.5 4 4v8c0 2.5-1.5 4-4 4h-6V8zm3 3v10h2.5c1 0 1.5-.5 1.5-1.5v-7c0-1-.5-1.5-1.5-1.5H31zm11-3h6c2 0 3 1 3 3v2c0 1.5-.5 2.5-1.5 3 1 .5 1.5 1.5 1.5 3v2c0 2-1 3-3 3h-6V8zm3 3v4h2c.5 0 1-.5 1-1v-2c0-.5-.5-1-1-1h-2zm0 7v4h2c.5 0 1-.5 1-1v-2c0-.5-.5-1-1-1h-2z" fill="#000" />
                            </svg>
                            <?php echo esc_html(number_format((float)$movie_engine_imdb_rating_val, 1)); ?>
                        </span>
                    <?php endif; ?>

                    <?php
                    $movie_engine_user_rating_val = get_post_meta($movie_engine_post_id, '_movie_engine_user_rating', true);
                    if ($movie_engine_user_rating_val && $movie_engine_user_rating_val > 0) : ?>
                        <span class="movie-engine-meta-badge me-user-rating-badge" title="<?php esc_attr_e('User Rating', 'cine-movie-engine'); ?>">
                            <span class="me-rating-star">★</span>
                            <span class="me-rating-value"><?php echo esc_html(number_format((float)$movie_engine_user_rating_val, 1)); ?></span>
                            <span class="me-rating-label"><?php esc_html_e('USER', 'cine-movie-engine'); ?></span>
                        </span>
                    <?php endif; ?>

                    <?php if ($movie_engine_year) : ?>
                        <span class="movie-engine-meta-item"><?php echo esc_html($movie_engine_year); ?></span>
                    <?php endif; ?>

                    <?php if ($movie_engine_runtime) : ?>
                        <span class="movie-engine-meta-item">
                            <span class="fa-solid fa-clock"></span>
                            <?php
                            $movie_engine_hours = floor($movie_engine_runtime / 60);
                            $movie_engine_minutes = $movie_engine_runtime % 60;
                            echo ($movie_engine_hours > 0) ? esc_html($movie_engine_hours) . "h : " : "";
                            echo esc_html($movie_engine_minutes) . " Min";
                            ?>
                        </span>
                    <?php endif; ?>

                    <?php if ($movie_engine_quality) : ?>
                        <span class="movie-engine-meta-badge me-quality-badge"><?php echo esc_html($movie_engine_quality); ?></span>
                    <?php endif; ?>

                    <?php
                    $movie_engine_views_count = get_post_meta($movie_engine_post_id, '_movie_engine_views', true);
                    $movie_engine_views_count = $movie_engine_views_count ? intval($movie_engine_views_count) : 0;
                    if ($movie_engine_views_count > 0) :
                        if ($movie_engine_views_count >= 1000000) {
                            $movie_engine_formatted_views = number_format($movie_engine_views_count / 1000000, 1) . 'm';
                        } elseif ($movie_engine_views_count >= 1000) {
                            $movie_engine_formatted_views = number_format($movie_engine_views_count / 1000, 1) . 'k';
                        } else {
                            $movie_engine_formatted_views = $movie_engine_views_count;
                        }
                    ?>
                        <span class="movie-engine-meta-item movie-engine-views-stat">
                            <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
                                <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z" />
                            </svg>
                            <span class="me-count"><?php echo esc_html($movie_engine_formatted_views); ?></span> <?php esc_html_e('views', 'cine-movie-engine'); ?>
                        </span>
                    <?php endif; ?>
                </div>

                <div class="movie-engine-short-synopsis">
                    <div class="me-synopsis-text me-three-lines">
                        <?php the_content(); ?>
                    </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_is_premium_val = get_post_meta($movie_engine_post_id, '_movie_engine_is_premium', true);
                        $movie_engine_is_premium_val = ($movie_engine_is_premium_val === '1' || $movie_engine_is_premium_val === 'yes' || $movie_engine_is_premium_val === 'true' || $movie_engine_is_premium_val === true || $movie_engine_is_premium_val === 1);
                        $movie_engine_user_id = get_current_user_id();
                        $movie_engine_has_access = false;
                        $movie_engine_requires_login = false;

                        $movie_engine_monetization_settings = get_option('movie_engine_monetization_settings', []);
                        $movie_engine_paywall_enabled = !empty($movie_engine_monetization_settings['movie_engine_enable_paywall']) || !empty($movie_engine_monetization_settings['enable_paywall']);
                        $movie_engine_restriction_mode = $movie_engine_monetization_settings['movie_engine_restriction_mode'] ?? ($movie_engine_monetization_settings['restriction_mode'] ?? 'premium');
                        $movie_engine_preview_duration = \MovieEngine\Payments\Membership::get_preview_duration();
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                        $movie_engine_preview_as_user = isset($_GET['preview_as_user']) && sanitize_text_field(wp_unslash($_GET['preview_as_user'])) == '1';

                        if (current_user_can('administrator') && !$movie_engine_preview_as_user) {
                            $movie_engine_has_access = true;
                        } elseif (!$movie_engine_paywall_enabled) {
                            $movie_engine_has_access = true;
                        } elseif (\MovieEngine\Payments\Membership::is_premium($movie_engine_user_id, $movie_engine_preview_as_user)) {
                            $movie_engine_has_access = true;
                        } elseif (!$movie_engine_preview_as_user && \MovieEngine\Payments\Membership::is_in_trial_period($movie_engine_user_id)) {
                            $movie_engine_has_access = true;
                        } elseif ($movie_engine_restriction_mode === 'partial' && $movie_engine_preview_duration > 0) {
                            $movie_engine_has_access = true;
                        } elseif ($movie_engine_restriction_mode === 'all') {
                            if ($movie_engine_user_id) {
                                if ($movie_engine_is_premium_val) {
                                    if (\MovieEngine\Payments\PPV::has_access($movie_engine_user_id, $movie_engine_post_id)) {
                                        $movie_engine_has_access = true;
                                    } else {
                                        $movie_engine_has_access = false;
                                    }
                                } else {
                                    $movie_engine_has_access = true;
                                }
                            } else {
                                $movie_engine_requires_login = true;
                                $movie_engine_has_access = false;
                            }
                        } elseif ($movie_engine_restriction_mode === 'premium') {
                            if (!$movie_engine_is_premium_val) {
                                $movie_engine_has_access = true;
                            } elseif ($movie_engine_user_id) {
                                if (\MovieEngine\Payments\PPV::has_access($movie_engine_user_id, $movie_engine_post_id)) {
                                    $movie_engine_has_access = true;
                                } else {
                                    $movie_engine_has_access = false;
                                }
                            } else {
                                $movie_engine_requires_login = true;
                                $movie_engine_has_access = false;
                            }
                        } else {
                            $movie_engine_has_access = true;
                        }

                        $movie_engine_login_url = home_url('/dashboard/');
                        $movie_engine_player_url = trailingslashit(get_permalink($movie_engine_post_id)) . 'player/';
                        if (!get_option('permalink_structure')) {
                            $movie_engine_player_url = add_query_arg('player', 1, get_permalink($movie_engine_post_id));
                        }

                        if ($movie_engine_has_access) : ?>
                            <a href="<?php echo esc_url($movie_engine_player_url); ?>" 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 elseif ($movie_engine_requires_login && !$movie_engine_is_premium_val) : ?>
                            <button type="button" class="movie-engine-btn movie-engine-btn-play movie-engine-login-required-btn"
                                data-login-url="<?php echo esc_url($movie_engine_login_url); ?>">
                                <?php esc_html_e('WATCH NOW', 'cine-movie-engine'); ?>
                                <span class="fa-solid fa-play"></span>
                            </button>
                        <?php endif; ?>

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

                        <?php
                        $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_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';
                        ?>
                        <button type="button" class="<?php echo esc_attr($movie_engine_watchlist_btn_class); ?>"
                            data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>"
                            <?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>
                    </div>

                    <div class="me-icon-actions">
                        <?php
                        \MovieEngine\Utils\TemplateLoader::get_template_part('global/actions-social');
                        \MovieEngine\Utils\TemplateLoader::get_template_part('global/button-playlist');
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>