<?php

/**
 * Display an episode card layout with header, image, and metadata.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

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

$episode_id    = get_the_ID();
$podcast_id    = (int) wp_get_post_parent_id( $episode_id );
$episode_title = get_the_title();
$episode_url   = get_permalink();  
$episode_file  = get_post_meta( $episode_id, 'episode_file', true );

$image = aiopod_get_image( array(     
    'post'        => $post, 
    'placeholder' => AIOPOD_PLUGIN_PLACEHOLDER_IMAGE_URL 
) );

$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 <?php if ( 'slider' === $attributes['layout'] ) : ?>swiper-slide <?php endif; ?>" data-post_id="<?php echo esc_attr( $episode_id ); ?>">
    <div class="aiopod-card<?php if ( 'list' === $attributes['layout'] ) : ?> aiopod-card--horizontal<?php endif; ?> aiopod-card--episode">
        <?php
        // Episode image wrapper
        if ( ! empty( $attributes['interactive_mode'] ) ) {
            echo sprintf( 
                '<div class="aiopod-card__image-wrapper aiopod-episode__image-wrapper aiopod-link-unstyled" title="%s">', 
                esc_attr( $episode_title ) 
            );
        } else {
            echo sprintf( 
                '<a href="%s" class="aiopod-card__image-wrapper aiopod-episode__image-wrapper aiopod-link-unstyled" title="%s">', 
                esc_url( $episode_url ), 
                esc_attr( $episode_title ) 
            );
        }

        // Episode image
        aiopod_the_image( $image );

        // Episode image overlays
        if ( $has_access ) {
            // Play/Pause button for interactive mode
            echo '<button type="button" class="aiopod-button aiopod-button--play aiopod-button--overlay" aria-label="' . esc_attr__( 'Play episode', 'aio-podcast-studio' ) . '">';
            echo '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#ffffff" width="64" height="64" 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>';
            echo '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#ffffff" width="64" height="64" 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>';
            echo '</button>';

            // Overlay metadata
            $overlay_meta = array();
            
            if ( ! empty( $attributes['show_explicit_badge'] ) ) { // Explicit badge
                $is_explicit = get_post_meta( $episode_id, 'is_explicit', true );

                if ( ! empty( $is_explicit ) ) {
                    $overlay_meta[] = sprintf(
                        '<span class="aiopod-badge aiopod-badge--explicit">%s</span>',
                        esc_html__( 'Explicit', 'aio-podcast-studio' )
                    );
                }  
            }
                        
            if ( ! empty( $attributes['show_episode_duration'] ) ) { // Episode Duration
                $episode_duration = get_post_meta( $episode_id, 'episode_duration', true );

                if ( ! empty( $episode_duration ) ) {
                    $overlay_meta[] = sprintf( 
                        '<span class="aiopod-badge aiopod-badge--duration">%s</span>', 
                        esc_html( aiopod_format_duration( $episode_duration, 'player' ) )
                    );
                }
            }

            // Print overlay metadata
            if ( ! empty( $overlay_meta ) ) {
                echo implode( '', $overlay_meta ); 
            }
        } else {
            // Locked icon for restricted episodes
            echo '<svg xmlns="http://www.w3.org/2000/svg" fill="#ffffff" x="0px" y="0px" width="32" height="32" 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>';

            $access_denied_label = apply_filters( 'aiopod_episode_access_denied_label', '', $episode_id );
            if ( ! empty( $access_denied_label ) ) {
                echo sprintf( 
                    '<span class="aiopod-badge aiopod-badge--restricted">%s</span>', 
                    esc_html( $access_denied_label )
                );
            }
        }
        
        // Close image wrapper
        echo ( ! empty( $attributes['interactive_mode'] ) ) ? '</div>' : '</a>';
        ?>

        <?php
        // Episode content
        $episode_content = '';

        // Episode title
        if ( ! empty( $attributes['show_episode_title'] ) ) {
            $episode_content .= '<div class="aiopod-episode__title aiopod-text-medium">';
            $episode_content .= sprintf(
                '<a href="%s" class="aiopod-link-unstyled" title="%s">%s</a>',
                esc_url( $episode_url ),
                esc_attr( $episode_title ),
                wp_kses_post( $episode_title )
            );
            $episode_content .= '</div>';
        }

        // Primary metadata
        $primary_meta = array();

        if ( ! empty( $attributes['show_episode_type'] ) ) { // Episode type 
            $episode_type = get_post_meta( $episode_id, 'episode_type', true );
            
            if ( 'trailer' === $episode_type || 'bonus' === $episode_type ) {
                $episode_types = aiopod_get_episode_types(); 

                $primary_meta[] = sprintf( 
                    '<span class="aiopod-episode__type">%s</span>', 
                    esc_html( $episode_types[ $episode_type ] ) 
                );
            }
        }

        if ( ! empty( $attributes['show_season_number'] ) ) { // Season number
            $season_number = get_post_meta( $episode_id, 'season_number', true );

            if ( ! empty( $season_number ) ) {
                $primary_meta[] = sprintf(
                    '<span class="aiopod-episode__season-number">%s</span>',
                    sprintf( esc_html__( 'Season %s', 'aio-podcast-studio' ), esc_html( $season_number ) )
                );
            }
        }

        if ( ! empty( $attributes['show_episode_number'] ) ) { // Episode number
            $episode_number = get_post_meta( $episode_id, 'episode_number', true );
            
            if ( ! empty( $episode_number ) ) {
                $primary_meta[] = sprintf(
                    '<span class="aiopod-episode__number">%s</span>',
                    sprintf( esc_html__( 'Episode %s', 'aio-podcast-studio' ), esc_html( $episode_number ) )
                );
            }
        }

        if ( ! empty( $attributes['show_episode_file_size'] ) ) { // Episode File size
            $episode_file_size = get_post_meta( $episode_id, 'episode_file_size', true );

            if ( ! empty( $episode_file_size ) ) {
                $primary_meta[] = sprintf( 
                    '<span class="aiopod-episode__file-size">%s</span>', 
                    esc_html( aiopod_format_file_size( $episode_file_size ) )
                );
            }
        }

        if ( ! empty( $primary_meta ) ) {
            $episode_content .= '<div class="aiopod-episode__meta aiopod-episode__meta--primary aiopod-leading-normal aiopod-text-muted aiopod-text-small">';
            $episode_content .= implode( '<span class="aiopod-separator" aria-hidden="true">•</span>', $primary_meta );
            $episode_content .= '</div>';
        }
            
        // Secondary metadata
        $secondary_meta = array();

        if ( ! empty( $attributes['show_episode_date'] ) ) { // Date Added
            $html  = '<span class="aiopod-episode__date aiopod-ui-text">';
            $html .= aiopod_get_svg( 'calendar' );
            $html .= '<time>' . esc_html( aiopod_get_the_date( $episode_id ) ) . '</time>';
            $html .= '</span>';
            
            $secondary_meta[] = $html;
        }

        if ( ! empty( $attributes['show_podcast_title'] ) ) { // Podcast title
            if ( ! empty( $podcast_id ) ) {
                $html  = '<div class="aiopod-episode__podcast-title aiopod-ui-text">';
                $html .= aiopod_get_svg( 'microphone' );
                $html .= sprintf(
                    '<a href="%s">%s</a>',
                    esc_url( get_permalink( $podcast_id ) ),
                    esc_html( get_the_title( $podcast_id ) )
                );
                $html .= '</div>';

                $secondary_meta[] = $html;
            }
        }

        if ( ! empty( $attributes['show_episode_tags'] ) ) { // Tags
            if ( $term_links = aiopod_get_term_links( $episode_id, 'aiopod_tags' ) ) {
                $html  = '<div class="aiopod-episode__tags aiopod-ui-text">';
                $html .= aiopod_get_svg( 'tag' );
                $html .= '<span>' . implode( '<span class="aiopod-separator" aria-hidden="true">,</span> ', $term_links ) . '</span>';
                $html .= '</div>';

                $secondary_meta[] = $html;
            }   
        }                        

        if ( ! empty( $attributes['show_downloads_count'] ) ) { // Downloads count
            $downloads_count = (int) get_post_meta( $episode_id, 'total_downloads', true );

            $html  = '<span class="aiopod-episode__downloads-count aiopod-ui-text">';
            $html .= aiopod_get_svg( 'download' );
            $html .= sprintf(
                _n( '%s download', '%s downloads', $downloads_count, 'aio-podcast-studio' ),
                aiopod_format_number( $downloads_count )
            );
            $html .= '</span>';

            $secondary_meta[] = $html;
        }

        if ( ! empty( $attributes['show_plays_count'] ) ) { // Plays count
            $plays_count = (int) get_post_meta( $episode_id, 'total_plays', true );

            $html  = '<span class="aiopod-episode__plays-count aiopod-ui-text">';
            $html .= aiopod_get_svg( 'speaker' );
            $html .= sprintf(
                _n( '%s play', '%s plays', $plays_count, 'aio-podcast-studio' ),
                aiopod_format_number( $plays_count )
            );
            $html .= '</span>';

            $secondary_meta[] = $html;
        }

        if ( ! empty( $secondary_meta ) ) {
            $episode_content .= '<div class="aiopod-episode__meta aiopod-episode__meta--secondary aiopod-leading-normal aiopod-text-muted aiopod-text-small">';
            $episode_content .= implode( '', $secondary_meta );
            $episode_content .= '</div>';
        }

        // Excerpt
        if ( ! empty( $attributes['show_episode_excerpt'] ) ) {
            $excerpt = aiopod_get_excerpt( 0, $attributes['excerpt_length'] );

            if ( ! empty( $excerpt ) ) {
                $excerpt = wp_kses_post( $excerpt );
                $excerpt = do_shortcode( $excerpt );

                $episode_content .= '<div class="aiopod-episode__excerpt">';
                $episode_content .= $excerpt;
                $episode_content .= '</div>';
            }                
        }

        // Output the episode content
        if ( ! empty( $episode_content ) ) {
            echo '<div class="aiopod-card__content-wrapper aiopod-episode__content-wrapper">';
            echo $episode_content;
            echo '</div>';
        }
        ?>

        <?php if ( $has_access ) aiopod_the_episode_template( $post, $attributes ); ?>
    </div>
</div>