<?php

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

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

if ( ! empty( $content ) ) {
    $content = $this->wrap_timestamps_with_links( $content );
}

$enable_transcription = get_post_meta( $post->ID, 'enable_transcription', true );
$transcription        = '';
if ( ! empty( $enable_transcription ) ) {
    $transcription = get_post_meta( $post->ID, 'transcription', true );
    
    if ( ! empty( $transcription ) ) {
        $transcription = $this->wrap_timestamps_with_links( $transcription );
    }
}
?>
<div class="aiopod aiopod-single aiopod-single--episode">
    <!-- Player -->
    <?php aiopod_the_player_card( $post, $attributes ); ?>

    <!-- Description & Transcription -->
    <?php if ( ! empty( $content ) && ! empty( $transcription ) ) : ?>
        <aiopod-tabs class="aiopod-tabs" role="tablist">
            <!-- Tabs -->
            <div class="aiopod-tabs__nav">
                <button type="button" class="aiopod-tabs__tab aiopod-tabs__tab--active" role="tab" aria-selected="true" tabindex="0" data-tab="#aiopod-tabs__panel-description">
                    <?php esc_html_e( 'Episode Details', 'aio-podcast-studio' ); ?>
                </button>
                
                <button type="button" class="aiopod-tabs__tab" role="tab" aria-selected="false" tabindex="-1" data-tab="#aiopod-tabs__panel-transcript">
                    <?php esc_html_e( 'Transcript', 'aio-podcast-studio' ); ?>
                </button>
            </div>

            <!-- Tab Content: Episode Details -->
            <div id="aiopod-tabs__panel-description" class="aiopod-tabs__panel aiopod-episode__description" role="tabpanel" aria-hidden="false">
                <?php echo $content; ?>
            </div>

            <!-- Tab Content: Transcript -->
            <div id="aiopod-tabs__panel-transcript" class="aiopod-tabs__panel aiopod-episode__transcription" role="tabpanel" aria-hidden="true" style="display: none;">
                <?php echo wp_kses_post( wpautop( $transcription ) ); ?>
            </div>
        </aiopod-tabs>
    <?php elseif ( ! empty( $content ) ) : ?>
        <div class="aiopod-episode__description">
            <?php echo $content; ?>
        </div>
    <?php elseif ( ! empty( $transcription ) ) : ?>
        <div class="aiopod-episode__transcription">
            <?php echo wp_kses_post( wpautop( $transcription ) ); ?>
        </div>
    <?php endif; ?>    

    <?php 
    // Related Episodes
    if ( ! empty( $attributes['show_related_episodes'] ) ) {
        $related_episodes_settings = aiopod_get_option( 'aiopod_related_episodes_settings' );

        $related_episodes_args = array( 
            'podcast'                => (int) $post->post_parent,
            'title'                  => $attributes['related_episodes_title'],
            'exclude'                => $post->ID,
            'show_count'             => 0,
            'layout'                 => 'grid',
            'columns'                => $related_episodes_settings['columns'],
            'limit'                  => $related_episodes_settings['limit'],
            'orderby'                => $related_episodes_settings['orderby'],
            'order'                  => $related_episodes_settings['order'],					
            'show_episode_title'     => isset( $related_episodes_settings['display_options']['episode_title'] ),
            'show_episode_excerpt'   => isset( $related_episodes_settings['display_options']['episode_excerpt'] ),
            'show_episode_date'      => isset( $related_episodes_settings['display_options']['episode_date'] ),
            'show_podcast_title'     => isset( $related_episodes_settings['display_options']['podcast_title'] ),
            'show_episode_tags'      => isset( $related_episodes_settings['display_options']['episode_tags'] ),
            'show_season_number'     => isset( $related_episodes_settings['display_options']['season_number'] ),
            'show_episode_number'    => isset( $related_episodes_settings['display_options']['episode_number'] ),
            'show_episode_file_size' => isset( $related_episodes_settings['display_options']['episode_file_size'] ),
            'show_episode_duration'  => isset( $related_episodes_settings['display_options']['episode_duration'] ),
            'show_explicit_badge'    => isset( $related_episodes_settings['display_options']['explicit_badge'] ),
            'show_downloads_count'   => isset( $related_episodes_settings['display_options']['downloads_count'] ),
            'show_plays_count'       => isset( $related_episodes_settings['display_options']['plays_count'] ),
            'excerpt_length'         => $related_episodes_settings['excerpt_length'],
            'enable_player'          => 0,
            'interactive_mode'       => 0,
            'enable_filters'         => $related_episodes_settings['enable_filters'],
            'pagination'             => $related_episodes_settings['pagination'],
            'hide_if_empty'          => 0
        );

        $related_episodes_args = apply_filters( 'aiopod_single_episode_related_episodes_args', $related_episodes_args );
        aiopod_the_episodes( $related_episodes_args); 
    }
    ?> 

    <?php
    // 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>
