<?php

/**
 * Layout: Slider.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

$episodes_count     = (int) $attributes['count'];
$slides_per_view    = ! empty( $attributes['slides_per_view'] ) ? (int) $attributes['slides_per_view'] : 3;
$space_between      = ! empty( $attributes['space_between'] ) ? (int) $attributes['space_between'] : 24;
$show_dots          = (int) $attributes['show_dots'];
$show_arrows        = (int) $attributes['show_arrows'];
$player_autoadvance = (int) $attributes['player_autoadvance'];
$player_loop        = (int) $attributes['player_loop'];
$featured_episode   = null;

if ( $aiopod_query->have_posts() ) {
    $episodes_count = count( $aiopod_query->posts );

    if ( ! empty( $attributes['enable_player'] ) ) {
        foreach ( $aiopod_query->posts as $post ) {
            $has_access = aiopod_current_user_can( 'view_aiopod_podcast', $post->ID );
            if ( $has_access ) {
                $featured_episode = $post;
                break;
            }
        }
    }
}

if ( ! $featured_episode ) {
    $attributes['enable_player']    = 0;
    $attributes['interactive_mode'] = 0;
}

if ( $episodes_count > 1 && $player_autoadvance && $player_loop ) {
    $attributes['player_loop'] = 0;
}

// Render HTML output
if ( $aiopod_query->have_posts() ) : ?>
    <div class="aiopod aiopod-episodes aiopod-episodes--layout-slider<?php if ( ! empty( $attributes['interactive_mode'] ) ) : ?> aiopod-episodes--interactive<?php endif; ?>">
        <?php 
        if ( ! wp_doing_ajax() ) {
            // Title
            if ( ! empty( $attributes['title'] ) ) {
                echo sprintf(
                    '<div class="aiopod-episodes__title aiopod-text-heading">%s</div>',
                    esc_html( $attributes['title'] )
                );
            }

            // Player
            if ( ! empty( $attributes['enable_player'] ) && $featured_episode ) {
                aiopod_the_player_card( $featured_episode, $attributes );
            }

            // Filters
            if ( ! empty( $attributes['enable_filters'] ) ) {
                aiopod_the_filters_form( $attributes );
            }
        }
        ?>

        <aiopod-episodes<?php if ( $player_autoadvance ) : ?> autoadvance<?php endif; ?><?php if ( $player_loop ) : ?> loop<?php endif; ?>>
            <!-- Count -->
            <?php if ( ! empty( $attributes['show_count'] ) ) : ?>
                <div class="aiopod-episodes__count aiopod-text-muted">
                    <?php echo sprintf( _n( '%s episode found.', '%s episodes found.', $episodes_count, 'aio-podcast-studio' ), number_format_i18n( $episodes_count ) ); ?>
                </div>
            <?php endif; ?>

            <!-- Episodes -->
            <aiopod-slider slides-per-view="<?php echo esc_attr( $slides_per_view ); ?>" space-between="<?php echo esc_attr( $space_between ); ?>"<?php if ( ! empty( $show_dots ) ) : ?> dots<?php endif; ?><?php if ( ! empty( $show_arrows ) ) : ?> arrows<?php endif; ?>>
                <div class="aiopod-episodes__list aiopod-responsive-wrapper swiper">
                    <div class="aiopod-dynamic-wrapper swiper-wrapper">
                        <?php
                        // Start the loop
                        global $post;

                        // The loop          
                        while ( $aiopod_query->have_posts() ) :        
                            $aiopod_query->the_post();  
                            aiopod_the_episode_card( $post, $attributes );  
                        endwhile;
                            
                        // Use reset postdata to restore orginal query
                        wp_reset_postdata();   
                        ?>
                    </div>
                    <?php
                    // Arrows
                    if ( ! empty( $show_arrows ) ) {
                        echo '<div class="swiper-button-prev"></div>';
                        echo '<div class="swiper-button-next"></div>';
                    }
                    ?>
                </div>

                <?php
                // Dots
                if ( ! empty( $show_dots ) ) {
                    echo '<div class="swiper-pagination"></div>';
                }
                ?>
            </aiopod-slider>
        </aiopod-episodes>
    </div>
<?php elseif ( 0 === (int) $attributes['hide_if_empty'] ) : ?>
    <div class="aiopod aiopod-episodes aiopod-episodes--layout-slider<?php if ( ! empty( $attributes['interactive_mode'] ) ) : ?> aiopod-episodes--interactive<?php endif; ?>">
        <?php
        if ( ! wp_doing_ajax() ) {
            // Filters
            if ( ! empty( $attributes['enable_filters'] ) ) {
                aiopod_the_filters_form( $attributes );
            }
        }
        ?>

        <aiopod-episodes>
            <div class="aiopod-episodes__empty aiopod-text-muted">
                <?php esc_html_e( 'No episodes found.', 'aio-podcast-studio' ); ?>
            </div>
        </aiopod-episodes>
    </div>
<?php endif;
