<?php

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

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

$max_height         = (int) $attributes['max_height'];
$episodes_count     = (int) $attributes['count'];
$player_autoadvance = (int) $attributes['player_autoadvance'];
$player_loop        = (int) $attributes['player_loop'];
$featured_episode   = null;

if ( $aiopod_query->have_posts() && ! 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-compact<?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 esc_html( sprintf( _n( '%s episode found.', '%s episodes found.', $episodes_count, 'aio-podcast-studio' ), number_format_i18n( $episodes_count ) ) ); ?>
                </div>
            <?php endif; ?>

            <!-- Episodes -->
            <div class="aiopod-episodes__list aiopod-responsive-wrapper"<?php if ( ! empty( $max_height ) ) echo 'style="max-height: ' . (int) $max_height . 'px;"'; ?>>
                <div class="aiopod-dynamic-wrapper">
                    <?php
                    // Start the loop
                    global $post;

                    // The loop          
                    while ( $aiopod_query->have_posts() ) :        
                        $aiopod_query->the_post();  
                        
                        $episode_id       = get_the_ID();
                        $episode_title    = get_the_title();
                        $episode_file     = get_post_meta( $episode_id, 'episode_file', true );
                        $episode_duration = get_post_meta( $episode_id, 'episode_duration', true );

                        if ( empty( $episode_file ) ) {
                            continue;
                        }

                        $has_access = aiopod_current_user_can( 'view_aiopod_podcast', $episode_id );
                        ?>
                        <div class="aiopod-episode <?php if ( ! $has_access ) : ?>aiopod-episode--restricted<?php endif; ?>aiopod-responsive-wrapper" data-post_id="<?php echo esc_attr( $episode_id ); ?>">
                            <?php if ( $has_access ) : ?>
                                <?php if ( ! empty( $attributes['interactive_mode'] ) ) : ?>
                                    <button type="button" class="aiopod-episode__play aiopod-button aiopod-button--play">
                                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="32" height="32" class="aiopod-svg aiopod-svg--play">
                                            <path fill-rule="evenodd" d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12Zm14.024-.983a1.125 1.125 0 0 1 0 1.966l-5.603 3.113A1.125 1.125 0 0 1 9 15.113V8.887c0-.857.921-1.4 1.671-.983l5.603 3.113Z" clip-rule="evenodd" />
                                        </svg>
                                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="32" height="32" class="aiopod-svg aiopod-svg--pause" style="display: none;">
                                            <path fill-rule="evenodd" d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12ZM9 8.25a.75.75 0 0 0-.75.75v6c0 .414.336.75.75.75h.75a.75.75 0 0 0 .75-.75V9a.75.75 0 0 0-.75-.75H9Zm5.25 0a.75.75 0 0 0-.75.75v6c0 .414.336.75.75.75H15a.75.75 0 0 0 .75-.75V9a.75.75 0 0 0-.75-.75h-.75Z" clip-rule="evenodd" />
                                        </svg>
                                    </button>
                                <?php else : ?>
                                    <a href="<?php echo esc_url( get_permalink() ); ?>" class="aiopod-link-unstyled">
                                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="32" height="32" class="aiopod-svg aiopod-svg--play">
                                            <path fill-rule="evenodd" d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12Zm14.024-.983a1.125 1.125 0 0 1 0 1.966l-5.603 3.113A1.125 1.125 0 0 1 9 15.113V8.887c0-.857.921-1.4 1.671-.983l5.603 3.113Z" clip-rule="evenodd" />
                                        </svg>
                                    </a>
                                <?php endif; ?>
                            <?php else : ?>
                                <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" x="0px" y="0px" width="32" height="24" viewBox="0 0 50 50" class="aiopod-svg aiopod-svg--lock">
                                    <path d="M 25 3 C 18.363281 3 13 8.363281 13 15 L 13 20 L 9 20 C 7.300781 20 6 21.300781 6 23 L 6 47 C 6 48.699219 7.300781 50 9 50 L 41 50 C 42.699219 50 44 48.699219 44 47 L 44 23 C 44 21.300781 42.699219 20 41 20 L 37 20 L 37 15 C 37 8.363281 31.636719 3 25 3 Z M 25 5 C 30.566406 5 35 9.433594 35 15 L 35 20 L 15 20 L 15 15 C 15 9.433594 19.433594 5 25 5 Z M 25 30 C 26.699219 30 28 31.300781 28 33 C 28 33.898438 27.601563 34.6875 27 35.1875 L 27 38 C 27 39.101563 26.101563 40 25 40 C 23.898438 40 23 39.101563 23 38 L 23 35.1875 C 22.398438 34.6875 22 33.898438 22 33 C 22 31.300781 23.300781 30 25 30 Z"></path>
                                </svg>
                            <?php endif; ?>

                            <div class="aiopod-episode__content">
                                <div class="aiopod-episode__title">
                                    <?php 
                                    echo wp_kses_post( $episode_title ); 

                                    if ( ! $has_access ) {
                                        $access_denied_label = apply_filters( 'aiopod_episode_access_denied_label', '', $post_id );
                                        if ( ! empty( $access_denied_label ) ) {
                                            echo ' — ' . esc_html( $access_denied_label );
                                        }
                                    }
                                    ?>
                                </div>

                                <?php
                                if ( ! empty( $episode_duration ) && $has_access ) {
                                    echo sprintf( 
                                        '<div class="aiopod-episode__duration aiopod-leading-normal aiopod-text-muted aiopod-text-small">%s</div>', 
                                        esc_html( aiopod_format_duration( $episode_duration, 'player' ) )
                                    );
                                }
                                ?>
                            </div>

                            <?php if ( $has_access ) aiopod_the_episode_template( $post, $attributes ); ?>
                        </div>
                        <?php
                    endwhile;
                        
                    // Use reset postdata to restore orginal query
                    wp_reset_postdata();   
                    ?>
                </div> 
                
                <!-- Pagination -->
                <?php aiopod_the_pagination( $attributes ); ?>
            </div>
        </aiopod-episodes>
    </div>
<?php elseif ( 0 === (int) $attributes['hide_if_empty'] ) : ?>
    <div class="aiopod aiopod-episodes aiopod-episodes--layout-compact<?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;
