<?php

/**
 * The template for displaying advanced filter sidebar
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/archive/filter-sidebar.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
}

use MovieEngine\Utils\Helpers;

$movie_engine_locked_post_type = $args['locked_post_type'] ?? '';
$movie_engine_collapse_all = !empty($args['collapse_all']);
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$movie_engine_sidebar_post_type = !empty($movie_engine_locked_post_type) ? $movie_engine_locked_post_type : (isset($_GET['post_type']) ? sanitize_text_field(wp_unslash($_GET['post_type'])) : '');

if (!is_array($movie_engine_sidebar_post_type) && strpos($movie_engine_sidebar_post_type, ',') !== false) {
    $movie_engine_sidebar_post_type = array_map('trim', explode(',', $movie_engine_sidebar_post_type));
}

// Fetch Data
$movie_engine_genres = Helpers::get_terms_by_post_type('movie_engine_genre', $movie_engine_sidebar_post_type);
$movie_engine_tags = Helpers::get_terms_by_post_type('movie_engine_tag', $movie_engine_sidebar_post_type);
$movie_engine_years = Helpers::get_terms_by_post_type('movie_engine_year', $movie_engine_sidebar_post_type);
$movie_engine_languages = Helpers::get_terms_by_post_type('movie_engine_language', $movie_engine_sidebar_post_type);

// Quality
$movie_engine_qualities = [];
if (!$movie_engine_sidebar_post_type || $movie_engine_sidebar_post_type === 'movie_engine_movie' || (is_array($movie_engine_sidebar_post_type) && in_array('movie_engine_movie', $movie_engine_sidebar_post_type))) {
    $movie_engine_movie_qualities = Helpers::get_terms_by_post_type('movie_engine_quality', 'movie_engine_movie');
    $movie_engine_qualities = array_merge($movie_engine_qualities, $movie_engine_movie_qualities);
}
if (!$movie_engine_sidebar_post_type || $movie_engine_sidebar_post_type === 'movie_engine_series' || (is_array($movie_engine_sidebar_post_type) && in_array('movie_engine_series', $movie_engine_sidebar_post_type))) {
    $movie_engine_tv_qualities = Helpers::get_terms_by_post_type('movie_engine_tv_quality', 'movie_engine_series');
    $movie_engine_qualities = array_merge($movie_engine_qualities, $movie_engine_tv_qualities);
}

$movie_engine_temp_qualities = [];
foreach ($movie_engine_qualities as $movie_engine_q) $movie_engine_temp_qualities[$movie_engine_q->slug] = $movie_engine_q;
$movie_engine_qualities = array_values($movie_engine_temp_qualities);

usort($movie_engine_years, function ($a, $b) {
    return (int)$b->name <=> (int)$a->name;
});

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$movie_engine_active_genres = isset($_GET['me_genre']) ? array_map('sanitize_text_field', (array) wp_unslash($_GET['me_genre'])) : [];
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$movie_engine_active_years = isset($_GET['me_year']) ? array_map('sanitize_text_field', (array) wp_unslash($_GET['me_year'])) : [];
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$movie_engine_active_qualities = isset($_GET['me_quality']) ? array_map('sanitize_text_field', (array) wp_unslash($_GET['me_quality'])) : [];
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$movie_engine_active_languages = isset($_GET['me_language']) ? array_map('sanitize_text_field', (array) wp_unslash($_GET['me_language'])) : [];
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$movie_engine_active_access = isset($_GET['me_access']) ? array_map('sanitize_text_field', (array) wp_unslash($_GET['me_access'])) : [];
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$movie_engine_active_duration = isset($_GET['me_duration']) ? sanitize_text_field(wp_unslash($_GET['me_duration'])) : '';
?>

<aside class="movie-engine-filter-sidebar">
    <div class="me-filter-sidebar-header">
        <h3>
            <?php esc_html_e('FILTERS', 'cine-movie-engine'); ?>
            <span class="me-filter-active-count me-hidden">0</span>
        </h3>
        <button type="button" class="me-clear-all-filters me-hidden"><?php esc_html_e('Clear All', 'cine-movie-engine'); ?></button>
    </div>

    <div class="me-filter-sidebar-scroll">
        <div class="me-filter-section<?php echo $movie_engine_collapse_all ? ' is-collapsed' : ''; ?>" data-filter="me_genre">
            <h4 class="me-filter-section-title">
                <?php esc_html_e('Genres', 'cine-movie-engine'); ?>
                <span class="fa-solid fa-angle-right"></span>
            </h4>
            <div class="me-filter-options">
                <?php foreach ($movie_engine_genres as $movie_engine_genre) : ?>
                    <label class="me-filter-checkbox">
                        <input type="checkbox" name="me_genre[]" value="<?php echo esc_attr($movie_engine_genre->slug); ?>" <?php checked(in_array($movie_engine_genre->slug, $movie_engine_active_genres)); ?>>
                        <span class="me-checkbox-label"><?php echo esc_html($movie_engine_genre->name); ?></span>
                    </label>
                <?php endforeach; ?>
            </div>
        </div>

        <div class="me-filter-section is-collapsed" data-filter="me_year">
            <h4 class="me-filter-section-title">
                <?php esc_html_e('By Year', 'cine-movie-engine'); ?>
                <span class="fa-solid fa-angle-right"></span>
            </h4>
            <div class="me-filter-options">
                <?php foreach ($movie_engine_years as $movie_engine_year) : ?>
                    <label class="me-filter-checkbox">
                        <input type="checkbox" name="me_year[]" value="<?php echo esc_attr($movie_engine_year->slug); ?>" <?php checked(in_array($movie_engine_year->slug, $movie_engine_active_years)); ?>>
                        <span class="me-checkbox-label"><?php echo esc_html($movie_engine_year->name); ?></span>
                    </label>
                <?php endforeach; ?>
            </div>
        </div>

        <?php if (!empty($movie_engine_qualities)) : ?>
            <div class="me-filter-section is-collapsed" data-filter="me_quality">
                <h4 class="me-filter-section-title">
                    <?php esc_html_e('By Quality', 'cine-movie-engine'); ?>
                    <span class="fa-solid fa-angle-right"></span>
                </h4>
                <div class="me-filter-options">
                    <?php foreach ($movie_engine_qualities as $movie_engine_quality) : ?>
                        <label class="me-filter-checkbox">
                            <input type="checkbox" name="me_quality[]" value="<?php echo esc_attr($movie_engine_quality->slug); ?>" <?php checked(in_array($movie_engine_quality->slug, $movie_engine_active_qualities)); ?>>
                            <span class="me-checkbox-label"><?php echo esc_html($movie_engine_quality->name); ?></span>
                        </label>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>

        <?php if (!empty($movie_engine_languages)) : ?>
            <div class="me-filter-section is-collapsed" data-filter="me_language">
                <h4 class="me-filter-section-title">
                    <?php esc_html_e('By Language', 'cine-movie-engine'); ?>
                    <span class="fa-solid fa-angle-right"></span>
                </h4>
                <div class="me-filter-options">
                    <?php foreach ($movie_engine_languages as $movie_engine_lang) : ?>
                        <label class="me-filter-checkbox">
                            <input type="checkbox" name="me_language[]" value="<?php echo esc_attr($movie_engine_lang->slug); ?>" <?php checked(in_array($movie_engine_lang->slug, $movie_engine_active_languages)); ?>>
                            <span class="me-checkbox-label"><?php echo esc_html($movie_engine_lang->name); ?></span>
                        </label>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>

        <div class="me-filter-section is-collapsed" data-filter="me_access">
            <h4 class="me-filter-section-title">
                <?php esc_html_e('By Access', 'cine-movie-engine'); ?>
                <span class="fa-solid fa-angle-right"></span>
            </h4>
            <div class="me-filter-options me-inline-options">
                <label class="me-filter-pill">
                    <input type="checkbox" name="me_access[]" value="free" <?php checked(in_array('free', $movie_engine_active_access)); ?>>
                    <span><?php esc_html_e('Free', 'cine-movie-engine'); ?></span>
                </label>
                <label class="me-filter-pill">
                    <input type="checkbox" name="me_access[]" value="premium" <?php checked(in_array('premium', $movie_engine_active_access)); ?>>
                    <span><span class="fa-solid fa-crown"></span> <?php esc_html_e('Premium', 'cine-movie-engine'); ?></span>
                </label>
                <label class="me-filter-pill">
                    <input type="checkbox" name="me_access[]" value="rent" <?php checked(in_array('rent', $movie_engine_active_access)); ?>>
                    <span><?php esc_html_e('Rent', 'cine-movie-engine'); ?></span>
                </label>
            </div>
        </div>

        <div class="me-filter-section is-collapsed" data-filter="me_duration">
            <h4 class="me-filter-section-title">
                <?php esc_html_e('By Duration', 'cine-movie-engine'); ?>
                <span class="fa-solid fa-angle-right"></span>
            </h4>
            <div class="me-filter-options">
                <label class="me-filter-radio">
                    <input type="radio" name="me_duration" value="under_1" <?php checked($movie_engine_active_duration === 'under_1'); ?>>
                    <span class="me-radio-label"><?php esc_html_e('Under 1 Hour', 'cine-movie-engine'); ?></span>
                </label>
                <label class="me-filter-radio">
                    <input type="radio" name="me_duration" value="1_2" <?php checked($movie_engine_active_duration === '1_2'); ?>>
                    <span class="me-radio-label"><?php esc_html_e('1-2 Hours', 'cine-movie-engine'); ?></span>
                </label>
                <label class="me-filter-radio">
                    <input type="radio" name="me_duration" value="2_3" <?php checked($movie_engine_active_duration === '2_3'); ?>>
                    <span class="me-radio-label"><?php esc_html_e('2-3 Hours', 'cine-movie-engine'); ?></span>
                </label>
                <label class="me-filter-radio">
                    <input type="radio" name="me_duration" value="above_3" <?php checked($movie_engine_active_duration === 'above_3'); ?>>
                    <span class="me-radio-label"><?php esc_html_e('3+ Hours', 'cine-movie-engine'); ?></span>
                </label>
            </div>
        </div>

        <div class="me-filter-section is-collapsed" data-filter="me_rating_min">
            <h4 class="me-filter-section-title">
                <?php esc_html_e('By Rating', 'cine-movie-engine'); ?>
                <span class="fa-solid fa-angle-right"></span>
            </h4>
            <div class="me-filter-options">
                <?php for ($movie_engine_i = 9; $movie_engine_i >= 5; $movie_engine_i--) : ?>
                    <label class="me-filter-radio">
                        <input type="radio" name="me_rating_min" value="<?php echo esc_attr($movie_engine_i); ?>">
                        <span class="me-radio-label">
                            <?php printf(
                                /* translators: %d+: Stars */
                                esc_html__('%d+ Stars', 'cine-movie-engine'),
                                absint($movie_engine_i)
                            ); ?>
                        </span>
                    </label>
                <?php endfor; ?>
            </div>
        </div>

        <?php if (!empty($movie_engine_tags)) : ?>
            <div class="me-filter-section is-collapsed" data-filter="me_tag">
                <h4 class="me-filter-section-title">
                    <?php esc_html_e('By Tags', 'cine-movie-engine'); ?>
                    <span class="fa-solid fa-angle-right"></span>
                </h4>
                <div class="me-filter-options me-tag-cloud">
                    <?php foreach ($movie_engine_tags as $movie_engine_tag) : ?>
                        <label class="me-filter-tag">
                            <input type="checkbox" name="me_tag[]" value="<?php echo esc_attr($movie_engine_tag->slug); ?>">
                            <span class="me-tag-label"><?php echo esc_html($movie_engine_tag->name); ?></span>
                        </label>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
</aside>