<?php

/**
 * The template for displaying pay-per-view buttons
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/global/button-ppv.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;
}

$movie_engine_post_id = $movie_engine_post_id ?? get_the_ID();
$movie_engine_user_id = get_current_user_id();
$movie_engine_is_premium = get_post_meta($movie_engine_post_id, '_movie_engine_is_premium', true);
$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) ?: 48;

$movie_engine_payment_settings = get_option('movie_engine_payments_settings', []);
$movie_engine_currency = $movie_engine_payment_settings['movie_engine_currency'] ?? ($movie_engine_payment_settings['currency'] ?? 'USD');
$movie_engine_currency_symbol = $movie_engine_currency === 'USD' ? '$' : $movie_engine_currency;

$movie_engine_has_purchased = \MovieEngine\Payments\PPV::has_purchased($movie_engine_user_id, $movie_engine_post_id);
$movie_engine_has_rental = \MovieEngine\Payments\PPV::has_active_rental($movie_engine_user_id, $movie_engine_post_id);

$movie_engine_is_admin = current_user_can('administrator');
$movie_engine_has_membership = $movie_engine_is_admin ? false : \MovieEngine\Payments\Membership::has_active_membership($movie_engine_user_id);

if (!$movie_engine_is_admin && ($movie_engine_has_membership || $movie_engine_has_purchased)) {
    return;
}
$movie_engine_subscription_only = $movie_engine_is_premium && !$movie_engine_sale_price && !$movie_engine_rent_price;

if (!$movie_engine_is_premium && !$movie_engine_sale_price && !$movie_engine_rent_price) {
    return;
}

$movie_engine_compact = $args['compact'] ?? false;
$movie_engine_context = $args['context'] ?? 'hero';

$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'] ?? '');
$movie_engine_subscribe_text = $movie_engine_monetization_settings['movie_engine_cta_button_text'] ?? ($movie_engine_monetization_settings['cta_button_text'] ?? esc_html__('Subscribe', 'cine-movie-engine'));
if (empty($movie_engine_subscribe_url)) {
    $movie_engine_design_settings = get_option('movie_engine_design_settings', []);
    $movie_engine_pricing_page_id = $movie_engine_design_settings['movie_engine_pricing_page'] ?? ($movie_engine_design_settings['pricing_page'] ?? 0);
    $movie_engine_subscribe_url = $movie_engine_pricing_page_id ? get_permalink($movie_engine_pricing_page_id) : home_url('/pricing');
}

$movie_engine_has_both_options = ($movie_engine_sale_price && $movie_engine_sale_price > 0) && ($movie_engine_rent_price && $movie_engine_rent_price > 0);
$movie_engine_has_any_ppv = ($movie_engine_sale_price && $movie_engine_sale_price > 0) || ($movie_engine_rent_price && $movie_engine_rent_price > 0);

if ($movie_engine_compact) : ?>
    <?php if (!$movie_engine_has_rental) : ?>
        <div class="movie-engine-hero-ppv-actions <?php echo 'me-context-' . esc_attr($movie_engine_context); ?>">

            <?php if ($movie_engine_context === 'hero') : ?>
                <button type="button" class="movie-engine-btn movie-engine-btn-get-access movie-engine-btn-ppv-trigger"
                    data-item-id="<?php echo esc_attr($movie_engine_post_id); ?>"
                    data-sale-price="<?php echo esc_attr($movie_engine_sale_price); ?>"
                    data-rent-price="<?php echo esc_attr($movie_engine_rent_price); ?>"
                    data-rent-duration="<?php echo esc_attr($movie_engine_rent_duration); ?>"
                    data-currency="<?php echo esc_attr($movie_engine_currency_symbol); ?>"
                    data-title="<?php echo esc_attr(get_the_title($movie_engine_post_id)); ?>"
                    data-is-premium="<?php echo esc_attr($movie_engine_is_premium ? '1' : '0'); ?>"
                    data-subscribe-url="<?php echo esc_url($movie_engine_subscribe_url); ?>"
                    data-subscribe-text="<?php echo esc_attr($movie_engine_subscribe_text); ?>"
                    aria-label="<?php esc_attr_e('Get Access', 'cine-movie-engine'); ?>: <?php echo esc_attr(get_the_title($movie_engine_post_id)); ?>">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                        <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" />
                    </svg>
                    <?php esc_html_e('GET ACCESS', 'cine-movie-engine'); ?>
                </button>
            <?php else : ?>
                <?php if ($movie_engine_is_premium) : ?>
                    <a href="<?php echo esc_url($movie_engine_subscribe_url); ?>" class="movie-engine-btn movie-engine-btn-subscribe" aria-label="<?php echo esc_attr($movie_engine_subscribe_text); ?>">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                            <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" />
                        </svg>
                        <?php echo esc_html($movie_engine_subscribe_text); ?>
                    </a>
                <?php endif; ?>

                <?php if ($movie_engine_has_both_options) : ?>
                    <button class="movie-engine-btn movie-engine-btn-ppv-trigger"
                        data-item-id="<?php echo esc_attr($movie_engine_post_id); ?>"
                        data-sale-price="<?php echo esc_attr($movie_engine_sale_price); ?>"
                        data-rent-price="<?php echo esc_attr($movie_engine_rent_price); ?>"
                        data-rent-duration="<?php echo esc_attr($movie_engine_rent_duration); ?>"
                        data-currency="<?php echo esc_attr($movie_engine_currency_symbol); ?>"
                        data-title="<?php echo esc_attr(get_the_title($movie_engine_post_id)); ?>"
                        data-is-premium="<?php echo esc_attr($movie_engine_is_premium ? '1' : '0'); ?>"
                        data-subscribe-url="<?php echo esc_url($movie_engine_subscribe_url); ?>"
                        data-subscribe-text="<?php echo esc_attr($movie_engine_subscribe_text); ?>"
                        aria-label="<?php esc_attr_e('Buy or Rent', 'cine-movie-engine'); ?>: <?php echo esc_attr(get_the_title($movie_engine_post_id)); ?>">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                            <path d="M21 2l-2 2m-7.61 7.61a5.5 5.5 0 1 1-7.778 7.778 5.5 5.5 0 0 1 7.777-7.777zm0 0L15.5 7.5m0 0l3 3L22 7l-3-3m-3.5 3.5L19 4" />
                        </svg>
                        <?php esc_html_e('Buy or Rent', 'cine-movie-engine'); ?>
                    </button>
                <?php elseif ($movie_engine_sale_price && $movie_engine_sale_price > 0) : ?>
                    <button class="movie-engine-btn movie-engine-btn-ppv me-btn-buy"
                        data-item-id="<?php echo esc_attr($movie_engine_post_id); ?>"
                        data-type="sale"
                        data-price="<?php echo esc_attr($movie_engine_sale_price); ?>"
                        aria-label="<?php esc_attr_e('Buy', 'cine-movie-engine'); ?>: <?php echo esc_attr(get_the_title($movie_engine_post_id)); ?>">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                            <circle cx="9" cy="21" r="1" />
                            <circle cx="20" cy="21" r="1" />
                            <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6" />
                        </svg>
                        <?php esc_html_e('Buy', 'cine-movie-engine'); ?>
                        <span class="me-price"><?php echo esc_html($movie_engine_currency_symbol) . esc_html(number_format($movie_engine_sale_price, 2)); ?></span>
                    </button>
                <?php elseif ($movie_engine_rent_price && $movie_engine_rent_price > 0) : ?>
                    <button class="movie-engine-btn movie-engine-btn-ppv me-btn-rent"
                        data-item-id="<?php echo esc_attr($movie_engine_post_id); ?>"
                        data-type="rent"
                        data-price="<?php echo esc_attr($movie_engine_rent_price); ?>"
                        data-duration="<?php echo esc_attr($movie_engine_rent_duration); ?>"
                        aria-label="<?php esc_attr_e('Rent', 'cine-movie-engine'); ?>: <?php echo esc_attr(get_the_title($movie_engine_post_id)); ?>">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                            <circle cx="12" cy="12" r="10" />
                            <polyline points="12 6 12 12 16 14" />
                        </svg>
                        <?php esc_html_e('Rent', 'cine-movie-engine'); ?>
                        <span class="me-price"><?php echo esc_html($movie_engine_currency_symbol) . esc_html(number_format($movie_engine_rent_price, 2)); ?></span>
                    </button>
                <?php endif; ?>
            <?php endif; ?>

        </div>
    <?php else : ?>
        <?php
        $movie_engine_ppv_expiry = \MovieEngine\Payments\PPV::get_rental_expiry($movie_engine_user_id, $movie_engine_post_id);
        $movie_engine_ppv_time_remaining = \MovieEngine\Payments\PPV::get_rental_time_remaining($movie_engine_user_id, $movie_engine_post_id);
        $movie_engine_ppv_hours_remaining = floor($movie_engine_ppv_time_remaining / 3600);
        $movie_engine_ppv_minutes_remaining = floor(($movie_engine_ppv_time_remaining % 3600) / 60);
        ?>
        <div class="movie-engine-rental-badge" aria-label="<?php esc_attr_e('Rental Time Remaining', 'cine-movie-engine'); ?>">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                <circle cx="12" cy="12" r="10" />
                <polyline points="12 6 12 12 16 14" />
            </svg>
            <span class="movie-engine-rental-countdown" data-expiry="<?php echo esc_attr($movie_engine_ppv_expiry); ?>">
                <?php
                printf(
                    /* translators: 1: hours remaining, 2: minutes remaining */
                    esc_html__('%1$dh %2$dm', 'cine-movie-engine'),
                    absint($movie_engine_ppv_hours_remaining),
                    absint($movie_engine_ppv_minutes_remaining)
                );
                ?>
            </span>
            <?php esc_html_e('remaining', 'cine-movie-engine'); ?>
        </div>
    <?php endif; ?>
<?php else: ?>
    <div class="movie-engine-ppv-section">
        <?php if ($movie_engine_has_purchased) : ?>
            <div class="movie-engine-ppv-owned">
                <span class="fa-solid fa-check" aria-hidden="true"></span>
                <span><?php esc_html_e('You own this movie', 'cine-movie-engine'); ?></span>
            </div>
        <?php elseif ($movie_engine_has_rental) : ?>
            <?php
            $movie_engine_ppv_expiry = \MovieEngine\Payments\PPV::get_rental_expiry($movie_engine_user_id, $movie_engine_post_id);
            $movie_engine_ppv_time_remaining = \MovieEngine\Payments\PPV::get_rental_time_remaining($movie_engine_user_id, $movie_engine_post_id);
            $movie_engine_ppv_hours_remaining = floor($movie_engine_ppv_time_remaining / 3600);
            $movie_engine_ppv_minutes_remaining = floor(($movie_engine_ppv_time_remaining % 3600) / 60);
            ?>
            <div class="movie-engine-ppv-rental-active">
                <span class="fa-solid fa-clock" aria-hidden="true"></span>
                <span>
                    <?php
                    printf(
                        /* translators: %s: rental expiry countdown html */
                        esc_html__('Rental expires in %s', 'cine-movie-engine'),
                        '<strong class="movie-engine-rental-countdown" data-expiry="' . esc_attr($movie_engine_ppv_expiry) . '">' .
                            sprintf(
                                /* translators: 1: hours remaining, 2: minutes remaining */
                                esc_html__('%1$dh %2$dm', 'cine-movie-engine'),
                                absint($movie_engine_ppv_hours_remaining),
                                absint($movie_engine_ppv_minutes_remaining)
                            ) .
                            '</strong>'
                    );
                    ?>
                </span>
            </div>
        <?php else : ?>
            <div class="movie-engine-ppv-options">
                <h3 class="movie-engine-ppv-title"><?php esc_html_e('Watch This Movie', 'cine-movie-engine'); ?></h3>
                <div class="movie-engine-ppv-buttons">
                    <?php if ($movie_engine_sale_price && $movie_engine_sale_price > 0) : ?>
                        <button class="movie-engine-btn me-btn-ppv me-btn-buy"
                            data-item-id="<?php echo esc_attr($movie_engine_post_id); ?>"
                            data-type="sale"
                            data-price="<?php echo esc_attr($movie_engine_sale_price); ?>"
                            aria-label="<?php esc_attr_e('Buy', 'cine-movie-engine'); ?>: <?php echo esc_html($movie_engine_currency_symbol) . esc_html(number_format($movie_engine_sale_price, 2)); ?>">
                            <span class="fa-solid fa-cart-shopping" aria-hidden="true"></span>
                            <span class="movie-engine-ppv-label">
                                <?php esc_html_e('Buy', 'cine-movie-engine'); ?>
                                <span class="movie-engine-ppv-price"><?php echo esc_html($movie_engine_currency_symbol) . esc_html(number_format($movie_engine_sale_price, 2)); ?></span>
                            </span>
                            <span class="movie-engine-ppv-desc"><?php esc_html_e('Own it forever', 'cine-movie-engine'); ?></span>
                        </button>
                    <?php endif; ?>

                    <?php if ($movie_engine_rent_price && $movie_engine_rent_price > 0) : ?>
                        <button class="movie-engine-btn me-btn-ppv me-btn-rent"
                            data-item-id="<?php echo esc_attr($movie_engine_post_id); ?>"
                            data-type="rent"
                            data-price="<?php echo esc_attr($movie_engine_rent_price); ?>"
                            data-duration="<?php echo esc_attr($movie_engine_rent_duration); ?>"
                            aria-label="<?php esc_attr_e('Rent', 'cine-movie-engine'); ?>: <?php echo esc_html($movie_engine_currency_symbol) . esc_html(number_format($movie_engine_rent_price, 2)); ?>">
                            <span class="fa-solid fa-clock" aria-hidden="true"></span>
                            <span class="movie-engine-ppv-label">
                                <?php esc_html_e('Rent', 'cine-movie-engine'); ?>
                                <span class="movie-engine-ppv-price"><?php echo esc_html($movie_engine_currency_symbol) . esc_html(number_format($movie_engine_rent_price, 2)); ?></span>
                            </span>
                            <span class="movie-engine-ppv-desc">
                                <?php
                                printf(
                                    /* translators: %d: number of hours */
                                    esc_html__('%d hours access', 'cine-movie-engine'),
                                    absint($movie_engine_rent_duration)
                                );
                                ?>
                            </span>
                        </button>
                    <?php endif; ?>
                </div>

                <?php if (!$movie_engine_user_id) : ?>
                    <p class="movie-engine-ppv-login-notice">
                        <?php
                        printf(
                            /* translators: %s: login link */
                            esc_html__('Please %s to purchase or rent this movie.', 'cine-movie-engine'),
                            '<a href="' . esc_url(home_url('/dashboard')) . '">' . esc_html__('login', 'cine-movie-engine') . '</a>'
                        );
                        ?>
                    </p>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
<?php endif; ?>

<?php if ($movie_engine_compact && !defined('MOVIE_ENGINE_PPV_MODAL_RENDERED')) :
    define('MOVIE_ENGINE_PPV_MODAL_RENDERED', true);
?>
    <div id="movie-engine-ppv-modal" class="movie-engine-modal me-hidden" role="dialog" aria-modal="true" aria-labelledby="ppv-modal-title">
        <div class="movie-engine-ppv-modal-content">
            <button type="button" class="movie-engine-close-modal" aria-label="<?php esc_attr_e('Close Modal', 'cine-movie-engine'); ?>">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                    <line x1="18" y1="6" x2="6" y2="18" />
                    <line x1="6" y1="6" x2="18" y2="18" />
                </svg>
            </button>

            <div class="movie-engine-ppv-modal-header">
                <h3 class="movie-engine-ppv-modal-title"><?php esc_html_e('Choose Your Option', 'cine-movie-engine'); ?></h3>
                <p class="movie-engine-ppv-modal-subtitle" id="ppv-modal-movie-title"></p>
            </div>

            <div class="movie-engine-ppv-modal-options">
                <div class="movie-engine-ppv-option me-ppv-subscribe-option me-hidden" data-type="subscribe">
                    <div class="movie-engine-ppv-option-icon">
                        <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" />
                        </svg>
                    </div>
                    <div class="movie-engine-ppv-option-info">
                        <h4 id="ppv-subscribe-title"><?php echo esc_html($movie_engine_subscribe_text); ?></h4>
                        <p class="movie-engine-ppv-option-price"><?php esc_html_e('Unlimited Access', 'cine-movie-engine'); ?></p>
                        <span class="movie-engine-ppv-option-desc"><?php esc_html_e('Best for regular viewers', 'cine-movie-engine'); ?></span>
                    </div>
                    <div class="movie-engine-ppv-option-check">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3">
                            <polyline points="20 6 9 17 4 12" />
                        </svg>
                    </div>
                </div>

                <div class="movie-engine-ppv-option me-ppv-buy-option me-hidden" data-type="sale">
                    <div class="movie-engine-ppv-option-icon">
                        <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <circle cx="9" cy="21" r="1" />
                            <circle cx="20" cy="21" r="1" />
                            <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6" />
                        </svg>
                    </div>
                    <div class="movie-engine-ppv-option-info">
                        <h4><?php esc_html_e('Buy', 'cine-movie-engine'); ?></h4>
                        <p class="movie-engine-ppv-option-price" id="ppv-buy-price"></p>
                        <span class="movie-engine-ppv-option-desc"><?php esc_html_e('Own it forever', 'cine-movie-engine'); ?></span>
                    </div>
                    <div class="movie-engine-ppv-option-check">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3">
                            <polyline points="20 6 9 17 4 12" />
                        </svg>
                    </div>
                </div>

                <div class="movie-engine-ppv-option me-ppv-rent-option me-hidden" data-type="rent">
                    <div class="movie-engine-ppv-option-icon">
                        <svg width="32" height="32" 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="movie-engine-ppv-option-info">
                        <h4><?php esc_html_e('Rent', 'cine-movie-engine'); ?></h4>
                        <p class="movie-engine-ppv-option-price" id="ppv-rent-price"></p>
                        <span class="movie-engine-ppv-option-desc" id="ppv-rent-duration"></span>
                    </div>
                    <div class="movie-engine-ppv-option-check">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3">
                            <polyline points="20 6 9 17 4 12" />
                        </svg>
                    </div>
                </div>
            </div>

            <div class="movie-engine-ppv-modal-footer">
                <button type="button" class="movie-engine-btn movie-engine-btn-primary movie-engine-ppv-confirm" disabled>
                    <?php esc_html_e('Continue to Payment', 'cine-movie-engine'); ?>
                </button>
                <?php if (!$movie_engine_user_id) : ?>
                    <p class="movie-engine-ppv-login-notice">
                        <?php
                        printf(
                            /* translators: %s: login link */
                            esc_html__('Please %s to continue', 'cine-movie-engine'),
                            '<a href="' . esc_url(home_url('/dashboard')) . '">' . esc_html__('login', 'cine-movie-engine') . '</a>'
                        );
                        ?>
                    </p>
                <?php endif; ?>
            </div>
        </div>
    </div>
<?php endif; ?>