<?php

/**
 * The template for displaying series cast section
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/single-series/section-cast.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_design_options = get_option('movie_engine_design_settings', []);
$movie_engine_is_episode = (get_post_type(get_the_ID()) === 'movie_engine_episode');
$movie_engine_prefix = $movie_engine_is_episode ? 'episode_' : 'series_';
$movie_engine_show_cast = isset($movie_engine_design_options[$movie_engine_prefix . 'show_cast']) ? $movie_engine_design_options[$movie_engine_prefix . 'show_cast'] : 1;
if (!$movie_engine_show_cast) {
    return;
}

$movie_engine_cast = get_post_meta(get_the_ID(), '_movie_engine_cast', true);
if (empty($movie_engine_cast) && $movie_engine_is_episode) {
    $movie_engine_series_id = get_post_meta(get_the_ID(), '_movie_engine_series_id', true);
    $movie_engine_cast = get_post_meta($movie_engine_series_id, '_movie_engine_cast', true);
}

if (!empty($movie_engine_cast) && is_array($movie_engine_cast)) :
    $movie_engine_cast_title = $movie_engine_design_options[$movie_engine_prefix . 'cast_title'] ?? esc_html__('Top Cast', 'cine-movie-engine');
    $movie_engine_cast_count = isset($movie_engine_design_options[$movie_engine_prefix . 'cast_count']) ? intval($movie_engine_design_options[$movie_engine_prefix . 'cast_count']) : 16;
    $movie_engine_cast_columns = isset($movie_engine_design_options[$movie_engine_prefix . 'cast_columns']) ? intval($movie_engine_design_options[$movie_engine_prefix . 'cast_columns']) : 8;
    $movie_engine_cast_columns_laptop = isset($movie_engine_design_options[$movie_engine_prefix . 'cast_columns_laptop']) ? intval($movie_engine_design_options[$movie_engine_prefix . 'cast_columns_laptop']) : 6;
    $movie_engine_cast_columns_tablet = isset($movie_engine_design_options[$movie_engine_prefix . 'cast_columns_tablet']) ? intval($movie_engine_design_options[$movie_engine_prefix . 'cast_columns_tablet']) : 6;
    $movie_engine_cast_columns_mobile = isset($movie_engine_design_options[$movie_engine_prefix . 'cast_columns_mobile']) ? intval($movie_engine_design_options[$movie_engine_prefix . 'cast_columns_mobile']) : 4;
    $movie_engine_cast_limited = array_slice($movie_engine_cast, 0, $movie_engine_cast_count);
    $movie_engine_cast_slider = isset($movie_engine_design_options[$movie_engine_prefix . 'cast_slider']) ? $movie_engine_design_options[$movie_engine_prefix . 'cast_slider'] : 1;
    $movie_engine_slider_class = $movie_engine_cast_slider ? 'movie-engine-slider-enabled' : '';
?>
    <section class="movie-engine-section movie-engine-cast-section <?php echo esc_attr($movie_engine_slider_class); ?>"
        style="--grid-columns: <?php echo esc_attr($movie_engine_cast_columns); ?>; --grid-columns-laptop: <?php echo esc_attr($movie_engine_cast_columns_laptop); ?>; --grid-columns-tablet: <?php echo esc_attr($movie_engine_cast_columns_tablet); ?>; --grid-columns-mobile: <?php echo esc_attr($movie_engine_cast_columns_mobile); ?>;">
        <div class="movie-engine-section-title">
            <h2>
                <?php printf(
                    /* translators: 1: cast section title, 2: number of cast members */
                    esc_html__('%1$s (%2$d)', 'cine-movie-engine'),
                    esc_html($movie_engine_cast_title),
                    count($movie_engine_cast_limited)
                ); ?>
            </h2>
        </div>
        <div class="movie-engine-items-grid movie-engine-cast-grid">
            <?php foreach ($movie_engine_cast_limited as $movie_engine_actor) :
                $movie_engine_person_id = $movie_engine_actor['id'] ?? 0;
                $movie_engine_name = $movie_engine_actor['name'];
                $movie_engine_photo = $movie_engine_actor['photo'];
                $movie_engine_character = $movie_engine_actor['character'];
                $movie_engine_person_url = '';

                if ($movie_engine_person_id && get_post_status($movie_engine_person_id)) {
                    $movie_engine_person_url = get_permalink($movie_engine_person_id);
                    $movie_engine_name = get_the_title($movie_engine_person_id);
                    if (has_post_thumbnail($movie_engine_person_id)) {
                        $movie_engine_photo = get_the_post_thumbnail_url($movie_engine_person_id, 'medium');
                    }
                }
            ?>
                <div class="movie-engine-item-card movie-engine-cast-item">
                    <?php if ($movie_engine_person_url) : ?><a href="<?php echo esc_url($movie_engine_person_url); ?>" class="me-cast-link-wrap"><?php endif; ?>
                        <div class="movie-engine-item-thumb movie-engine-cast-thumb">
                            <?php if (!empty($movie_engine_photo)) : ?>
                                <img src="<?php echo esc_url($movie_engine_photo); ?>" alt="<?php echo esc_attr($movie_engine_name); ?>">
                            <?php else : ?>
                                <div class="movie-engine-item-placeholder">
                                    <span class="fa-solid fa-users"></span>
                                </div>
                            <?php endif; ?>
                        </div>
                        <?php if ($movie_engine_person_url) : ?>
                        </a><?php endif; ?>

                    <div class="movie-engine-item-info movie-engine-cast-info">
                        <h2 class="movie-engine-item-title movie-engine-actor-name">
                            <?php if ($movie_engine_person_url) : ?><a href="<?php echo esc_url($movie_engine_person_url); ?>"><?php endif; ?>
                                <?php echo esc_html($movie_engine_name); ?>
                                <?php if ($movie_engine_person_url) : ?></a><?php endif; ?>
                        </h2>
                        <div class="movie-engine-item-meta movie-engine-actor-role">
                            <span><?php echo esc_html($movie_engine_character); ?></span>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </section>
<?php endif; ?>