<?php

/**
 * The template for displaying watchlist button
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/global/button-watchlist.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_user_id = get_current_user_id();
$movie_engine_post_id = get_the_ID();
$movie_engine_in_watchlist = false;
$movie_engine_watchlist_login_url = '';

if (is_user_logged_in()) {
    $movie_engine_in_watchlist = \MovieEngine\Frontend\Users\UserLibrary::in_watchlist($movie_engine_user_id, $movie_engine_post_id);
} else {
    $movie_engine_watchlist_login_url = wp_login_url(get_permalink());
}

$movie_engine_watchlist_class = $movie_engine_in_watchlist ? 'active' : '';
$movie_engine_watchlist_text = $movie_engine_in_watchlist ? esc_html__('Remove Watchlist', 'cine-movie-engine') : esc_html__('Add to Watchlist', 'cine-movie-engine');
$movie_engine_watchlist_icon = $movie_engine_in_watchlist ? 'fa-solid fa-check' : 'fa-solid fa-plus';
?>

<button class="movie-engine-watchlist-btn <?php echo esc_attr($movie_engine_watchlist_class); ?>"
    data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>"
    aria-label="<?php echo esc_attr($movie_engine_watchlist_text); ?>"
    <?php if (!empty($movie_engine_watchlist_login_url)) echo 'data-login-url="' . esc_url($movie_engine_watchlist_login_url) . '"'; ?>>
    <span class="<?php echo esc_attr($movie_engine_watchlist_icon); ?>" aria-hidden="true"></span>
    <span class="movie-engine-watchlist-text"><?php echo esc_html($movie_engine_watchlist_text); ?></span>
</button>