<?php

/**
 * Single Podcast Page.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

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

$podcast_title = get_the_title();

$image = aiopod_get_image( array( 
    'post' => $post
) );
?>
<div class="aiopod aiopod-single aiopod-single--podcast">
    <?php
    // Podcast content
    $podcast_content = '';

    // Podcast metadata
    $meta = array();
    
    if ( ! empty( $attributes['show_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 ) ) . '</time>';
        $html .= '</span>';
        
        $meta[] = $html;
    }

    if ( ! empty( $attributes['show_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( $attributes['show_explicit_badge'] ) ) {  // Explicit badge
        $is_explicit = get_post_meta( $post->ID, 'is_explicit', true );

        if ( ! empty( $is_explicit ) ) {
            $meta[] = sprintf(
                '<span class="aiopod-podcast__explicit">%s</span>',
                esc_html__( 'Explicit', 'aio-podcast-studio' )
            );
        }  
    }

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

    // Description
    if ( ! empty( $content ) ) {
        $podcast_content .= '<div class="aiopod-podcast__description">';
        $podcast_content .= $content;
        $podcast_content .= '</div>';
    }

    // Actions: Subscribe, Follow, Share
    $subscribe_buttons = array();
    if ( ! empty( $attributes['show_subscribe_buttons'] ) ) {
        $subscribe_buttons = aiopod_get_subscribe_buttons( $post->ID );
    }

    $follow_buttons = array();
    if ( ! empty( $attributes['show_follow_buttons'] ) ) {
        $follow_buttons = aiopod_get_follow_buttons( $post->ID );
    }

    $share_buttons = array();
    if ( ! empty( $attributes['show_share_buttons'] ) ) {
        $share_buttons = aiopod_get_share_buttons( $post->ID );
    }

    if ( ! empty( $subscribe_buttons ) || ! empty( $follow_buttons ) || ! empty( $share_buttons ) ) {
        $html = '<div class="aiopod-podcast__meta aiopod-podcast__meta--buttons">';

        // Button: Subscribe
        if ( ! empty( $subscribe_buttons ) ) {
            $html .= '<aiopod-modal data-modal="subscribe">';
            $html .= '<button type="button" class="aiopod-button aiopod-button--modal">';
            $html .= '<svg xmlns="http://www.w3.org/2000/svg" fill="none" width="16" height="16" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 0 0 5.454-1.31A8.967 8.967 0 0 1 18 9.75V9A6 6 0 0 0 6 9v.75a8.967 8.967 0 0 1-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 0 1-5.714 0m5.714 0a3 3 0 1 1-5.714 0" />
            </svg>';
            $html .= esc_html__( 'Subscribe', 'aio-podcast-studio' );
            $html .= '</button>';

            // Modal: Subscribe
            $html .= '<dialog class="aiopod aiopod-modal aiopod-modal--subscribe">';
            $html .= '<div class="aiopod-modal__header">';
            $html .= '<div class="aiopod-modal__title">' . esc_html__( 'Listen on Your Favorite App', 'aio-podcast-studio' ) . '</div>';
            $html .= '<button type="button" class="aiopod-modal__close" aria-label="' . esc_attr__( 'Close', 'aio-podcast-studio' ) . '">&times;</button>';
            $html .= '</div>';
            $html .= '<div class="aiopod-modal__body">';
            $html .= '<div class="aiopod-icons">';

            foreach ( $subscribe_buttons as $subscribe_button ) {
                $html .= sprintf(
                    '<a href="%s" class="aiopod-link-unstyled" title="%s" target="_blank" rel="noopener noreferrer"><img src="%s" alt="%s" width="32" height="32" /></a>',
                    esc_url( $subscribe_button['url'] ),
                    esc_attr( sprintf( __( 'Listen on %s', 'aio-podcast-studio' ), $subscribe_button['label'] ) ),
                    esc_url( $subscribe_button['image'] ),
                    esc_attr( $subscribe_button['label'] )
                );
            }

            $html .= '</div>';
            $html .= '</div>';
            $html .= '</dialog>';
            $html .= '</aiopod-modal>';
        }

        // Button: Follow Us
        if ( ! empty( $follow_buttons ) ) {
            $html .= '<aiopod-modal data-modal="follow">';
            $html .= '<button type="button" class="aiopod-button aiopod-button--modal">';
            $html .= '<svg xmlns="http://www.w3.org/2000/svg" fill="none" width="16" height="16" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" d="M18 7.5v3m0 0v3m0-3h3m-3 0h-3m-2.25-4.125a3.375 3.375 0 1 1-6.75 0 3.375 3.375 0 0 1 6.75 0ZM3 19.235v-.11a6.375 6.375 0 0 1 12.75 0v.109A12.318 12.318 0 0 1 9.374 21c-2.331 0-4.512-.645-6.374-1.766Z" />
            </svg>';
            $html .= esc_html__( 'Follow Us', 'aio-podcast-studio' );
            $html .= '</button>';

            // Modal: Follow Us
            $html .= '<dialog class="aiopod aiopod-modal">';
            $html .= '<div class="aiopod-modal__header">';
            $html .= '<div class="aiopod-modal__title">' . esc_html__( 'Follow Us & Stay Updated', 'aio-podcast-studio' ) . '</div>';
            $html .= '<button type="button" class="aiopod-modal__close" aria-label="' . esc_attr__( 'Close', 'aio-podcast-studio' ) . '">&times;</button>';
            $html .= '</div>';
            $html .= '<div class="aiopod-modal__body">';
            $html .= '<div class="aiopod-icons">';

            foreach ( $follow_buttons as $follow_button ) {
                $html .= sprintf(
                    '<a href="%s" class="aiopod-link-unstyled" title="%s" target="_blank" rel="noopener noreferrer"><span class="aiopod-icon %s"></span></a>',
                    esc_url( $follow_button['url'] ),
                    esc_attr( $follow_button['label'] ),
                    esc_attr( $follow_button['icon'] )
                );
            }

            $html .= '</div>';
            $html .= '</div>';
            $html .= '</dialog>';
            $html .= '</aiopod-modal>';
        }

        // Button: Share Podcast
        if ( ! empty( $share_buttons ) ) {
            $html .= '<aiopod-modal data-modal="share">';
            $html .= '<button type="button" class="aiopod-button aiopod-button--modal">';
            $html .= '<svg xmlns="http://www.w3.org/2000/svg" fill="none" width="16" height="16" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" d="M7.217 10.907a2.25 2.25 0 1 0 0 2.186m0-2.186c.18.324.283.696.283 1.093s-.103.77-.283 1.093m0-2.186 9.566-5.314m-9.566 7.5 9.566 5.314m0 0a2.25 2.25 0 1 0 3.935 2.186 2.25 2.25 0 0 0-3.935-2.186Zm0-12.814a2.25 2.25 0 1 0 3.933-2.185 2.25 2.25 0 0 0-3.933 2.185Z" />
            </svg>';
            $html .= esc_html__( 'Share Podcast', 'aio-podcast-studio' );
            $html .= '</button>';

            // Modal: Share
            $html .= '<dialog class="aiopod aiopod-modal">';
            $html .= '<div class="aiopod-modal__header">';
            $html .= '<div class="aiopod-modal__title">' . esc_html__( 'Share This Podcast', 'aio-podcast-studio' ) . '</div>';
            $html .= '<button type="button" class="aiopod-modal__close" aria-label="' . esc_attr__( 'Close', 'aio-podcast-studio' ) . '">&times;</button>';
            $html .= '</div>';
            $html .= '<div class="aiopod-modal__body">';
            $html .= '<div class="aiopod-icons">';

            foreach ( $share_buttons as $share_button ) {
                $html .= sprintf(
                    '<a href="%s" class="aiopod-link-unstyled" title="%s" target="_blank" rel="noopener noreferrer"><span class="aiopod-icon %s"></span></a>',
                    esc_url( $share_button['url'] ),
                    esc_attr( $share_button['label'] ),
                    esc_attr( $share_button['icon'] )
                );
            }

            $html .= '</div>';
            $html .= '</div>';
            $html .= '</dialog>';
            $html .= '</aiopod-modal>';
        }

        $html .= '</div>';

        $podcast_content .= $html;
    }
    
    // Output the podcast content
    if ( ! empty( $image['src'] ) || ! empty( $podcast_content ) ) {  
        echo '<div class="aiopod-podcast aiopod-responsive-wrapper">';
        echo '<div class="aiopod-card aiopod-card--horizontal aiopod-card--no-border aiopod-card--podcast">';

         if ( ! empty( $image['src'] ) ) {
            echo '<div class="aiopod-card__image-wrapper aiopod-podcast__image-wrapper">';
            aiopod_the_image( $image );
            echo '</div>';
        }

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

        echo '</div>';
        echo '</div>';
    }

    // Full Episodes
    if ( ! empty( $attributes['show_episodes'] ) ) {
        $episodes_args = array( 
            'podcast'       => $post->ID,
            'title'         => $attributes['episodes_title'],
            'hide_if_empty' => 0
        );

       if ( ! empty( $attributes['show_trailer_episodes'] ) || ! empty( $attributes['show_bonus_episodes'] ) ) {
            // Exclude episode types that are shown in separate sections
            $types = array( 'full' );

            if ( empty( $attributes['show_trailer_episodes'] ) ) {
                $types[] = 'trailer';
            }

            if ( empty( $attributes['show_bonus_episodes'] ) ) {
                $types[] = 'bonus';
            }

            $episodes_args['type'] = $types;
        }

        $episodes_args = apply_filters( 'aiopod_single_podcast_episodes_args', $episodes_args );
        aiopod_the_episodes( $episodes_args ); 
    }

    // Trailers
    if ( ! empty( $attributes['show_trailer_episodes'] ) ) {
        $trailers_args = array( 
            'podcast'          => $post->ID,
            'title'            => $attributes['trailer_episodes_title'],
            'type'             => 'trailer',
            'show_count'       => 0,
            'layout'           => 'slider',
            'limit'            => 0,
            'slides_per_view'  => $attributes['slides_per_view'],
            'space_between'    => $attributes['space_between'],
            'show_dots'        => $attributes['show_dots'],
            'show_arrows'      => $attributes['show_arrows'],		
            'enable_player'    => 0,
            'interactive_mode' => $attributes['interactive_mode'],
            'enable_filters'   => 0,
            'hide_if_empty'    => 1
        );

        $trailers_args = apply_filters( 'aiopod_single_podcast_trailers_args', $trailers_args );
        aiopod_the_episodes( $trailers_args ); 
    }

    // Bonus Episodes
    if ( ! empty( $attributes['show_bonus_episodes'] ) ) {
        $bonus_episodes_args = array( 
            'podcast'          => $post->ID,
            'title'            => $attributes['bonus_episodes_title'],
            'type'             => 'bonus',
            'show_count'       => 0,
            'layout'           => 'slider',
            'limit'            => 0,
            'slides_per_view'  => $attributes['slides_per_view'],
            'space_between'    => $attributes['space_between'],
            'show_dots'        => $attributes['show_dots'],
            'show_arrows'      => $attributes['show_arrows'],		
            'enable_player'    => 0,
            'interactive_mode' => $attributes['interactive_mode'],
            'enable_filters'   => 0,
            'hide_if_empty'    => 1
        );

        $bonus_episodes_args = apply_filters( 'aiopod_single_podcast_bonus_episodes_args', $bonus_episodes_args );
        aiopod_the_episodes( $bonus_episodes_args ); 
    }

    // Participants
    $participants = array();
    if ( ! empty( $attributes['show_participants'] ) ) {
        $participants = aiopod_get_post_participants( $post->ID );
    }

    $participants_columns = ! empty( $attributes['participants_columns'] ) ? (int) $attributes['participants_columns'] : 3;

    if ( ! empty( $participants ) ) : ?>
        <div class="aiopod-participants">
            <!-- Title -->
            <?php if ( ! empty( $attributes['participants_title'] ) ) : ?>
                <div class="aiopod-participants__title aiopod-text-heading">
                    <?php echo esc_html( $attributes['participants_title']  ); ?>
                </div>
            <?php endif; ?>

            <!-- Participants -->
            <div class="aiopod-participants__list">
                <div class="aiopod-grid aiopod-col-<?php echo esc_attr( $participants_columns ); ?>">
                    <?php foreach ( $participants as $participant ) : ?>
                        <div class="aiopod-participant">
                            <div class="aiopod-card aiopod-card--participant">
                                <?php if ( ! empty( $participant['image']['src'] ) ) : ?>
                                    <a href="<?php echo esc_url( $participant['url'] ); ?>" class="aiopod-card__image-wrapper aiopod-participant__image-wrapper aiopod-link-unstyled" title="<?php echo esc_attr( $participant['name'] ); ?>">
                                        <?php aiopod_the_image( $participant['image'] ); ?>
                                    </a>
                                <?php endif; ?>

                                <div class="aiopod-card__content-wrapper aiopod-participant__content-wrapper">
                                    <div class="aiopod-participant__name aiopod-text-medium">
                                        <a href="<?php echo esc_url( $participant['url'] ); ?>" class="aiopod-link-unstyled" title="<?php echo esc_attr( $participant['name'] ); ?>">
                                            <?php echo wp_kses_post( $participant['name'] ); ?>
                                        </a>
                                    </div>
                                    
                                    <div class="aiopod-participant__meta aiopod-leading-normal aiopod-text-muted aiopod-text-small">
                                        <?php echo esc_html( $participant['role'] ); ?>
                                    </div>                            
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>
</div>