<?php

/**
 * The template for displaying video player in single movie
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/single-movie/player.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
 * @var int $movie_engine_post_id
 * @var array $sources
 */

if (! defined('ABSPATH')) {
    exit;
}

if (!isset($movie_engine_post_id)) {
    $movie_engine_post_id = isset($args['movie_engine_post_id']) ? $args['movie_engine_post_id'] : get_the_ID();
}
$movie_engine_player_sources_list = \MovieEngine\Frontend\Player::get_player_data($movie_engine_post_id);
$movie_engine_first_source_obj = ! empty($movie_engine_player_sources_list[0]) ? $movie_engine_player_sources_list[0] : null;
?>
<?php
$movie_engine_partial_paywall_duration = apply_filters('movie_engine_partial_paywall_duration', false, $movie_engine_post_id);
$movie_engine_is_partial = ($movie_engine_partial_paywall_duration !== false) ? 'true' : 'false';
$movie_engine_preview_duration = ($movie_engine_partial_paywall_duration !== false) ? intval($movie_engine_partial_paywall_duration) : 0;
?>
<div class="movie-engine-player-wrapper" data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>" data-poster="<?php echo esc_url(get_the_post_thumbnail_url($movie_engine_post_id, 'full')); ?>" data-partial-paywall="<?php echo esc_attr($movie_engine_is_partial); ?>" data-preview-duration="<?php echo esc_attr($movie_engine_preview_duration); ?>" data-gramm="false" data-gramm_editor="false" data-enable-grammarly="false">
    <?php do_action('movie_engine_before_player'); ?>
    <?php do_action('movie_engine_ad_player_top'); ?>
    <?php
    $movie_engine_player_settings_val = get_option('movie_engine_player_settings', []);
    $movie_engine_show_source_tabs_val = isset($movie_engine_player_settings_val['movie_engine_show_source_tabs']) ? $movie_engine_player_settings_val['movie_engine_show_source_tabs'] : (isset($movie_engine_player_settings_val['show_source_tabs']) ? $movie_engine_player_settings_val['show_source_tabs'] : 1);
    $movie_engine_default_source_setting_val = isset($movie_engine_player_settings_val['movie_engine_default_source']) ? intval($movie_engine_player_settings_val['movie_engine_default_source']) : (isset($movie_engine_player_settings_val['default_source']) ? intval($movie_engine_player_settings_val['default_source']) : 1);
    $movie_engine_active_source_index = ($movie_engine_default_source_setting_val - 1);

    if (!isset($movie_engine_player_sources_list[$movie_engine_active_source_index])) {
        $movie_engine_active_source_index = 0;
    }

    if (count($movie_engine_player_sources_list) > 0 && $movie_engine_show_source_tabs_val) :
        $movie_engine_flag_map_list = [
            'en' => 'gb',
            'es' => 'es',
            'fr' => 'fr',
            'de' => 'de',
            'it' => 'it',
            'pt' => 'pt',
            'ru' => 'ru',
            'tr' => 'tr',
            'zh' => 'cn',
            'jp' => 'jp',
            'hi' => 'in',
            'multi' => 'un'
        ];
    ?>
        <div class="movie-engine-server-switcher">
            <div class="movie-engine-server-buttons">
                <?php foreach ($movie_engine_player_sources_list as $movie_engine_source_idx => $movie_engine_source_item) :
                    $movie_engine_lang_code = $movie_engine_source_item['lang'] ?? 'en';
                    $movie_engine_flag_code = $movie_engine_flag_map_list[$movie_engine_lang_code] ?? 'un';
                ?>
                    <button
                        type="button"
                        class="movie-engine-server-btn <?php echo esc_attr($movie_engine_source_idx === $movie_engine_active_source_index ? 'active' : ''); ?>"
                        data-server-index="<?php echo esc_attr($movie_engine_source_idx); ?>"
                        data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>">

                        <img src="https://flagcdn.com/w20/<?php echo esc_attr($movie_engine_flag_code); ?>.png" srcset="https://flagcdn.com/w40/<?php echo esc_attr($movie_engine_flag_code); ?>.png 2x" width="20" alt="<?php echo esc_attr($movie_engine_lang_code); ?>" class="movie-engine-server-flag">

                        <span class="movie-engine-server-name"><?php echo esc_html($movie_engine_source_item['name']); ?></span>

                        <?php if (!empty($movie_engine_source_item['quality'])): ?>
                            <span class="movie-engine-server-quality"><?php echo esc_html($movie_engine_source_item['quality']); ?></span>
                        <?php endif; ?>
                    </button>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>

    <?php $vtt_url = get_post_meta($movie_engine_post_id, '_movie_engine_vtt_url', true); ?>
    <div class="movie-engine-player-container" id="movie-engine-player-container-<?php echo esc_attr($movie_engine_post_id); ?>" data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>" data-vtt="<?php echo esc_url($vtt_url); ?>" data-subtitles="<?php echo esc_attr(wp_json_encode(\MovieEngine\Frontend\Player::get_subtitle_data($movie_engine_post_id))); ?>">
        <div class="movie-engine-player-loader">
            <span class="fa-solid fa-spinner fa-spin"></span> <?php esc_html_e('Loading player...', 'cine-movie-engine'); ?>
        </div>
    </div>

    <?php do_action('movie_engine_ad_player_bottom'); ?>
    <?php do_action('movie_engine_after_player'); ?>

    <?php if ($movie_engine_is_partial === 'true') :
        $movie_engine_post_type = get_post_type($movie_engine_post_id);
        $movie_engine_permalink = get_permalink($movie_engine_post_id);
        $movie_engine_ppv_id = $movie_engine_post_id;

        $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);

        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) {
                // For episodes, close button goes back to series page
                $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_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_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);
    ?>
        <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>