<?php

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

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

// Image
$image = aiopod_get_image( array( 
    'term'        => $term,
    'taxonomy'    => 'aiopod_participants',
    'placeholder' => AIOPOD_PLUGIN_PLACEHOLDER_AVATAR_URL
) );

// Build tab URLs
$base_url     = aiopod_get_participant_page_url( $term );
$podcasts_url = remove_query_arg( 'view', $base_url );
$episodes_url = add_query_arg( 'view', 'episodes', $base_url );

// Get active tab
$allowed_views = array( 'podcasts', 'episodes' );
$current_view  = get_query_var( 'view' );
$active_tab    = in_array( $current_view, $allowed_views ) ? $current_view : 'podcasts';

// Podcasts HTML
$podcasts = '';
if ( ! empty( $attributes['show_podcasts'] ) ) {
    $podcasts_args = array(
        'participants' => $term->term_id,
        'echo'         => 0
    );

    $podcasts_args = apply_filters( 'aiopod_single_participant_podcasts_args', $podcasts_args );
    $podcasts = aiopod_the_podcasts( $podcasts_args );
}

// Podcasts title
$podcasts_title = trim( $attributes['podcasts_title'] );
if ( '' === $podcasts_title ) {
    $podcasts_title = __( 'Podcasts', 'aio-podcast-studio' );
}

// Episodes HTML
$episodes = '';
if ( ! empty( $attributes['show_episodes'] ) ) {
    $episodes_args = array(
        'participants' => $term->term_id,
        'echo'         => 0
    );

    $episodes_args = apply_filters( 'aiopod_single_participant_episodes_args', $episodes_args );
    $episodes = aiopod_the_episodes( $episodes_args );
}

// Episodes title
$episodes_title = trim( $attributes['episodes_title'] );
if ( '' === $episodes_title ) {
    $episodes_title = __( 'Episodes', 'aio-podcast-studio' );
}
?>
<div class="aiopod aiopod-single aiopod-single--participant">
    <?php
    // Breadcrumbs
    if ( ! empty( $attributes['show_breadcrumbs'] ) ) {
        aiopod_the_taxonomy_breadcrumbs( $term, 'aiopod_categories', $attributes['current_page_id'] );
    }
    ?>

    <!-- Podcast Details -->
    <div class="aiopod-participant aiopod-responsive-wrapper">
        <div class="aiopod-card aiopod-card--horizontal aiopod-card--no-border aiopod-card--participant">
            <?php if ( ! empty( $image['src'] ) ) : ?>
                <div class="aiopod-card__image-wrapper aiopod-participant__image-wrapper">
                    <?php aiopod_the_image( $image ); ?>
                </div>
            <?php endif; ?>

            <?php
            // Participant content
            $participant_content = '';

            // Primary metadata
            $primary_meta = array();

            if ( ! empty( $attributes['show_job_title'] ) || ! empty( $attributes['show_company_name'] ) || ! empty( $attributes['show_website_url'] ) ) {
                // Job title
                if ( ! empty( $attributes['show_job_title'] ) ) {
                    $job_title = get_term_meta( $term->term_id, 'job_title', true );
                    
                    if ( ! empty( $job_title ) ) {
                        $primary_meta[] = sprintf( 
                            '<span class="aiopod-participant__job-title aiopod-font-bold">%s</span>', 
                            esc_html( $job_title )
                        );
                    }
                }

                // Company name
                if ( ! empty( $attributes['show_company_name'] ) ) {
                    $company_name = get_term_meta( $term->term_id, 'company_name', true );
                    
                    if ( ! empty( $company_name ) ) {
                        $primary_meta[] = sprintf( 
                            '<span class="aiopod-participant__company-name aiopod-text-muted aiopod-text-small">%s</span>', 
                            esc_html( $company_name )
                        );
                    }
                }

                // Website url
                if ( ! empty( $attributes['show_website_url'] ) ) {
                    $website_url = get_term_meta( $term->term_id, 'website_url', true );
                    
                    if ( ! empty( $website_url ) ) {
                        $primary_meta[] = sprintf(
                            '<span class="aiopod-participant__website-url aiopod-ui-text aiopod-text-small">%1$s <a href="%2$s" target="_blank" rel="noopener noreferrer">%2$s</a></span>',
                            aiopod_get_svg( 'globe' ),
                            esc_url( $website_url )
                        );
                    }
                }
            }

            if ( ! empty( $primary_meta ) ) {
                $participant_content .= '<div class="aiopod-participant__meta aiopod-participant__meta--primary aiopod-leading-normal">';
                $participant_content .= implode( '', $primary_meta );
                $participant_content .= '</div>';
            }
            
            // Secondary metadata
            $secondary_meta = array();

            if ( ! empty( $attributes['show_podcasts_count'] ) || ! empty( $attributes['show_episodes_count'] ) ) {
                // Podcasts count
                if ( ! empty( $attributes['show_podcasts_count'] ) ) {
                    $podcasts_count = aiopod_get_participant_posts_count( $term->term_id, 'aiopod_podcasts' );
                    
                    $html  = '<span class="aiopod-participant__podcasts-count aiopod-ui-text">';
                    $html .= aiopod_get_svg( 'microphone' );
                    $html .= esc_html( sprintf(
                        _n( '%s podcast', '%s podcasts', $podcasts_count, 'aio-podcast-studio' ),
                        number_format_i18n( $podcasts_count )
                    ) );
                    $html .= '</span>';

                    $secondary_meta[] = $html;
                }

                // Episodes count
                if ( ! empty( $attributes['show_episodes_count'] ) ) {
                    $episodes_count = aiopod_get_participant_posts_count( $term->term_id, 'aiopod_episodes' );
                    
                    $html  = '<span class="aiopod-participant__episodes-count aiopod-ui-text">';
                    $html .= aiopod_get_svg( 'episodes' );
                    $html .= esc_html( sprintf(
                        _n( '%s episode', '%s episodes', $episodes_count, 'aio-podcast-studio' ),
                        number_format_i18n( $episodes_count )
                    ) );
                    $html .= '</span>';

                    $secondary_meta[] = $html;
                }

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

            // Description
            if ( ! empty( $term->description ) ) {
                $participant_content .= '<div class="aiopod-participant__description">';
                $participant_content .= wp_kses_post( wpautop( $term->description ) );
                $participant_content .= '</div>';
            }

            // Follow Us Buttons
            if ( ! empty( $attributes['show_follow_buttons'] ) ) {
                $follow_buttons = aiopod_get_follow_buttons( $term->term_id, 'term' );

                if ( ! empty( $follow_buttons ) ) {
                    $participant_content .= '<div class="aiopod-icons">';

                    foreach ( $follow_buttons as $follow_button ) {
                        $participant_content .= 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'] )
                        );
                    }

                    $participant_content .= '</div>';
                }
            }

            // Output the participant content
            if ( ! empty( $participant_content ) ) {
                echo '<div class="aiopod-card__content-wrapper aiopod-participant__content-wrapper">';
                echo wp_kses_post( $participant_content );
                echo '</div>';
            }            
            ?>
        </div>
    </div>

    <!-- Podcasts & Episodes -->
    <?php if ( ! empty( $attributes['show_podcasts'] ) && ! empty( $attributes['show_episodes'] ) ) : ?>
        <aiopod-tabs>
            <!-- Tabs -->
            <div class="aiopod-tabs__nav" role="tablist">
                <a href="<?php echo esc_url( $podcasts_url ); ?>" class="aiopod-tabs__tab<?php if ( 'podcasts' === $active_tab ) : ?> aiopod-tabs__tab--active<?php endif; ?> aiopod-link-unstyled" role="tab" aria-selected="<?php echo ( 'podcasts' === $active_tab ) ? 'true' : 'false'; ?>" aria-controls="aiopod-participant-tab-panel">
                    <?php echo esc_html( $podcasts_title ); ?>
                </a>

                <a href="<?php echo esc_url( $episodes_url ); ?>" class="aiopod-tabs__tab<?php if ( 'podcasts' !== $active_tab ) : ?> aiopod-tabs__tab--active<?php endif; ?> aiopod-link-unstyled" role="tab" aria-selected="<?php echo ( 'podcasts' !== $active_tab ) ? 'true' : 'false'; ?>" aria-controls="aiopod-participant-tab-panel">
                    <?php echo esc_html( $episodes_title ); ?>
                </a>
            </div>

            <!-- Tab Content -->
            <div class="aiopod-tabs__panel" id="aiopod-participant-tab-panel" role="tabpanel">
                <?php                 
                if ( 'podcasts' === $active_tab ) { // Podcasts                    
                    echo $podcasts; 
                } else { // Episodes                   
                    echo $episodes; 
                }
                ?>
            </div>
        </aiopod-tabs>
    <?php elseif ( ! empty( $attributes['show_podcasts'] ) ) : ?>
        <?php echo $podcasts; ?>
    <?php elseif ( ! empty( $attributes['show_episodes'] ) ) : ?>
        <?php echo $episodes; ?>
    <?php endif; ?>  
</div>