<?php

/**
 * Display a podcast 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;
}

$post_id    = get_the_ID();
$post_title = get_the_title();
$post_url   = get_permalink();  

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

$has_access = aiopod_current_user_can( 'view_aiopod_podcast', $post_id );
?>
<div class="aiopod-podcast <?php if ( ! $has_access ) : ?>aiopod-podcast--restricted <?php endif; ?>aiopod-responsive-wrapper" data-post_id="<?php echo esc_attr( $post_id ); ?>">
    <div class="aiopod-card aiopod-card--podcast">
        <a href="<?php echo esc_url( $post_url ); ?>" class="aiopod-card__image-wrapper aiopod-podcast__image-wrapper aiopod-link-unstyled" title="<?php echo esc_attr( $post_title ); ?>">
            <?php   
            // Podcast image
            aiopod_the_image( $image );
            
            // Podcast image overlays
            if ( $has_access ) {   
                // Explicit badge            
                if ( ! empty( $attributes['show_explicit_badge'] ) ) {
                    $is_explicit = get_post_meta( $post_id, 'is_explicit', true );

                    if ( ! empty( $is_explicit ) ) {
                        echo sprintf(
                            '<span class="aiopod-badge aiopod-badge--explicit">%s</span>',
                            esc_html__( 'Explicit', 'aio-podcast-studio' )
                        );
                    }  
                }
            } else {
                // Locked icon for restricted podcasts
                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_podcast_access_denied_label', '', $post_id );
                if ( ! empty( $access_denied_label ) ) {
                    echo sprintf( 
                        '<span class="aiopod-badge aiopod-badge--restricted">%s</span>', 
                        esc_html( $access_denied_label )
                    );
                }
            }
            ?>
        </a>

        <?php
        // Podcast content
        $podcast_content = '';

        // Podcast title
        if ( ! empty( $attributes['show_podcast_title'] ) ) {
            $podcast_content .= '<div class="aiopod-podcast__title aiopod-text-medium">';
            $podcast_content .= sprintf(
                ' <a href="%s" class="aiopod-link-unstyled" title="%s">%s</a>',
                esc_url( $post_url ),
                esc_attr( $post_title ),
                wp_kses_post( $post_title )
            );
            $podcast_content .= '</div>';
        }

        // Podcast metadata
        $meta = array();
        
        if ( ! empty( $attributes['show_podcast_date'] ) ) { // Date Added
            $html  = '<span class="aiopod-podcast__date aiopod-ui-text">';
            $html .= aiopod_get_svg( 'calendar' );
            $html .= '<time>' . esc_html( aiopod_get_the_date( $post_id ) ) . '</time>';
            $html .= '</span>';
            
            $meta[] = $html;
        }

        if ( ! empty( $attributes['show_podcast_categories'] ) ) { // Categories
            if ( $term_links = aiopod_get_term_links( $post_id, 'aiopod_categories' ) ) {
                $html  = '<div class="aiopod-podcast__categories aiopod-ui-text">';
                $html .= aiopod_get_svg( 'folder' );
                $html .= '<span>' . implode( '<span class="aiopod-separator" aria-hidden="true">,</span> ', $term_links ) . '</span>';
                $html .= '</div>';

                $meta[] = $html;
            }
        }
        
        if ( ! empty( $attributes['show_episodes_count'] ) ) { // Episodes count
            $episodes_count = aiopod_get_podcast_episodes_count( $post_id );

            $html  = '<span class="aiopod-podcast__episodes-count aiopod-ui-text">';
            $html .= aiopod_get_svg( 'episodes' );
            $html .= sprintf( 
                _n( '%s episode', '%s episodes', $episodes_count, 'aio-podcast-studio' ), 
                number_format_i18n( $episodes_count ) 
            );
            $html .= '</span>';
            
            $meta[] = $html;
        }

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

            $html  = '<span class="aiopod-podcast__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>';

            $meta[] = $html;
        }

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

            $html  = '<span class="aiopod-podcast__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>';

            $meta[] = $html;
        }

        if ( ! empty( $meta ) ) {
            $podcast_content .= '<div class="aiopod-podcast__meta aiopod-leading-normal aiopod-text-muted aiopod-text-small">';
            $podcast_content .= implode( '', $meta );
            $podcast_content .= '</div>';
        }

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

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

                $podcast_content .= '<div class="aiopod-podcast__excerpt">';
                $podcast_content .= $excerpt;
                $podcast_content .= '</div>';
            }                
        }

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