<?php

/**
 * The template for displaying full-screen player page
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/pages/player-page.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;
}

// Get post data
$movie_engine_post_id = get_queried_object_id();
$movie_engine_post_type = get_post_type($movie_engine_post_id);
$movie_engine_title = get_the_title($movie_engine_post_id);
$movie_engine_permalink = get_permalink($movie_engine_post_id);
$movie_engine_poster = get_the_post_thumbnail_url($movie_engine_post_id, 'large');
$movie_engine_backdrop = get_post_meta($movie_engine_post_id, '_movie_engine_backdrop', true);

$movie_engine_partial_paywall_duration = apply_filters('movie_engine_partial_paywall_duration', false, $movie_engine_post_id);

if (\MovieEngine\Payments\Membership::is_restricted($movie_engine_post_id) && !$movie_engine_partial_paywall_duration) {
    wp_safe_redirect($movie_engine_permalink);
    exit;
}


$movie_engine_player_sources = \MovieEngine\Frontend\Player::get_player_data($movie_engine_post_id);
$movie_engine_trailer        = get_post_meta($movie_engine_post_id, '_movie_engine_trailer', true);

if (empty($movie_engine_player_sources) && empty($movie_engine_trailer)) {
    wp_safe_redirect($movie_engine_permalink);
    exit;
}

$movie_engine_show_partial_paywall = !empty($movie_engine_partial_paywall_duration);

$movie_engine_monetization_settings = get_option('movie_engine_monetization_settings', []);
$movie_engine_subscribe_url = $movie_engine_monetization_settings['movie_engine_cta_button_url'] ?? ($movie_engine_monetization_settings['cta_button_url'] ?? home_url('/pricing'));
$movie_engine_subscribe_text = $movie_engine_monetization_settings['movie_engine_cta_button_text'] ?? ($movie_engine_monetization_settings['cta_button_text'] ?? esc_html__('Subscribe Now', 'cine-movie-engine'));

$movie_engine_is_premium = get_post_meta($movie_engine_post_id, '_movie_engine_is_premium', true);
$movie_engine_is_premium = ($movie_engine_is_premium === '1' || $movie_engine_is_premium === 'yes' || $movie_engine_is_premium === 'true' || $movie_engine_is_premium === true || $movie_engine_is_premium === 1);

$movie_engine_sale_price = get_post_meta($movie_engine_post_id, '_movie_engine_sale_price', true);
$movie_engine_rent_price = get_post_meta($movie_engine_post_id, '_movie_engine_rent_price', true);
$movie_engine_rent_duration = get_post_meta($movie_engine_post_id, '_movie_engine_rent_duration', true);

$movie_engine_ppv_id = $movie_engine_post_id;

if ($movie_engine_post_type === 'movie_engine_episode') {
    $movie_engine_series_id = get_post_meta($movie_engine_post_id, '_movie_engine_series_id', true);
    if ($movie_engine_series_id) {
        $movie_engine_permalink = get_permalink($movie_engine_series_id);
        $movie_engine_ppv_id = $movie_engine_series_id;

        if (empty($movie_engine_is_premium)) {
            $series_premium = get_post_meta($movie_engine_series_id, '_movie_engine_is_premium', true);
            $movie_engine_is_premium = ($series_premium === '1' || $series_premium === 'yes' || $series_premium === 'true' || $series_premium === true || $series_premium === 1);
        }
        if (empty($movie_engine_sale_price)) {
            $movie_engine_sale_price = get_post_meta($movie_engine_series_id, '_movie_engine_sale_price', true);
        }
        if (empty($movie_engine_rent_price)) {
            $movie_engine_rent_price = get_post_meta($movie_engine_series_id, '_movie_engine_rent_price', true);
        }
        if (empty($movie_engine_rent_duration)) {
            $movie_engine_rent_duration = get_post_meta($movie_engine_series_id, '_movie_engine_rent_duration', true);
        }
    }
}
$movie_engine_rent_duration = $movie_engine_rent_duration ?: 48;

$movie_engine_payments_settings = get_option('movie_engine_payments_settings', []);
$movie_engine_currency = $movie_engine_payments_settings['movie_engine_currency'] ?? ($movie_engine_payments_settings['currency'] ?? 'USD');
$movie_engine_currency_symbols = ['USD' => '$', 'EUR' => '€', 'GBP' => '£', 'INR' => '₹', 'BDT' => '৳'];
$movie_engine_currency_symbol = $movie_engine_currency_symbols[$movie_engine_currency] ?? '$';
$movie_engine_has_buy_option = ($movie_engine_is_premium && $movie_engine_sale_price && floatval($movie_engine_sale_price) > 0);
$movie_engine_has_rent_option = ($movie_engine_is_premium && $movie_engine_rent_price && floatval($movie_engine_rent_price) > 0);

add_filter('show_admin_bar', '__return_false');

?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>

<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo esc_html($movie_engine_title); ?> - <?php esc_html_e('Player', 'cine-movie-engine'); ?></title>
    <?php wp_head(); ?>
</head>

<body class="movie-engine-player-page" data-partial-paywall="<?php echo $movie_engine_show_partial_paywall ? 'true' : 'false'; ?>" data-preview-duration="<?php echo esc_attr($movie_engine_partial_paywall_duration ?: 0); ?>">

    <div class="movie-engine-player-fullpage">
        <a href="<?php echo esc_url($movie_engine_permalink); ?>" class="movie-engine-player-page-close" aria-label="<?php esc_attr_e('Close Player', 'cine-movie-engine'); ?>">
            <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <line x1="18" y1="6" x2="6" y2="18"></line>
                <line x1="6" y1="6" x2="18" y2="18"></line>
            </svg>
        </a>
        <div class="movie-engine-player-fullpage-content">
            <?php echo \MovieEngine\Frontend\Player::render($movie_engine_post_id); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
            ?>
        </div>
        <?php if ($movie_engine_show_partial_paywall) : ?>
            <div class="movie-engine-partial-paywall" id="movie-engine-partial-paywall">
                <div class="me-paywall-content">
                    <div class="me-paywall-timer"><?php esc_html_e('Preview Ended', 'cine-movie-engine'); ?></div>
                    <a href="<?php echo esc_url($movie_engine_permalink); ?>" class="me-paywall-close" aria-label="<?php esc_attr_e('Close', 'cine-movie-engine'); ?>">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <line x1="18" y1="6" x2="6" y2="18"></line>
                            <line x1="6" y1="6" x2="18" y2="18"></line>
                        </svg>
                    </a>
                    <div class="me-paywall-badge">
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
                            <path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
                        </svg>
                        <?php esc_html_e('Premium Content', 'cine-movie-engine'); ?>
                    </div>
                    <h2 class="me-paywall-title"><?php esc_html_e('Continue Watching', 'cine-movie-engine'); ?></h2>
                    <p class="me-paywall-text"><?php esc_html_e('Your free preview has ended. Choose how you\'d like to continue.', 'cine-movie-engine'); ?></p>
                    <div class="me-paywall-options">
                        <a href="<?php echo esc_url($movie_engine_subscribe_url); ?>" class="me-paywall-option me-option-subscribe">
                            <div class="me-option-icon">
                                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                    <polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2" />
                                </svg>
                            </div>
                            <div class="me-option-info">
                                <span class="me-option-label"><?php esc_html_e('Subscribe', 'cine-movie-engine'); ?></span>
                                <span class="me-option-desc"><?php esc_html_e('Unlimited access to all content', 'cine-movie-engine'); ?></span>
                            </div>
                            <span class="me-option-price"><?php esc_html_e('Best Value', 'cine-movie-engine'); ?></span>
                        </a>
                        <?php if ($movie_engine_has_buy_option) : ?>
                            <button type="button" class="me-paywall-option me-option-buy movie-engine-btn-ppv"
                                data-item-id="<?php echo esc_attr($movie_engine_ppv_id); ?>"
                                data-type="sale"
                                data-price="<?php echo esc_attr($movie_engine_sale_price); ?>">
                                <div class="me-option-icon">
                                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <path d="M20.59 13.41l-7.17 7.17a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z" />
                                        <line x1="7" y1="7" x2="7.01" y2="7" />
                                    </svg>
                                </div>
                                <div class="me-option-info">
                                    <span class="me-option-label"><?php esc_html_e('Buy Now', 'cine-movie-engine'); ?></span>
                                    <span class="me-option-desc"><?php esc_html_e('Own this title forever', 'cine-movie-engine'); ?></span>
                                </div>
                                <span class="me-option-price"><?php echo esc_html($movie_engine_currency_symbol . number_format(floatval($movie_engine_sale_price), 2)); ?></span>
                            </button>
                        <?php endif; ?>

                        <?php if ($movie_engine_has_rent_option) : ?>
                            <button type="button" class="me-paywall-option me-option-rent movie-engine-btn-ppv"
                                data-item-id="<?php echo esc_attr($movie_engine_ppv_id); ?>"
                                data-type="rent"
                                data-price="<?php echo esc_attr($movie_engine_rent_price); ?>">
                                <div class="me-option-icon">
                                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <circle cx="12" cy="12" r="10" />
                                        <polyline points="12 6 12 12 16 14" />
                                    </svg>
                                </div>
                                <div class="me-option-info">
                                    <span class="me-option-label"><?php esc_html_e('Rent', 'cine-movie-engine'); ?></span>
                                    <span class="me-option-desc">
                                        <?php
                                        printf(
                                            /* translators: %d: number of hours */
                                            esc_html__('Watch for %d hours', 'cine-movie-engine'),
                                            absint($movie_engine_rent_duration)
                                        );
                                        ?>
                                    </span>
                                </div>
                                <span class="me-option-price"><?php echo esc_html($movie_engine_currency_symbol . number_format(floatval($movie_engine_rent_price), 2)); ?></span>
                            </button>
                        <?php endif; ?>
                    </div>

                    <a href="<?php echo esc_url($movie_engine_permalink); ?>" class="me-paywall-back">
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M19 12H5M12 19l-7-7 7-7" />
                        </svg>
                        <?php esc_html_e('Back to details', 'cine-movie-engine'); ?>
                    </a>
                </div>
            </div>
        <?php endif; ?>
    </div>

    <?php wp_footer(); ?>
</body>

</html>