<?php

/**
 * Display a player 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    = $post_id;
$podcast_id    = (int) wp_get_post_parent_id( $episode_id );
$episode_title = get_the_title( $episode_id );
$episode_url   = get_permalink( $episode_id );
$episode_file  = get_post_meta( $episode_id, 'episode_file', true );

if ( empty( $episode_file ) ) {
    return false;
}

$image = aiopod_get_image( array(
    'post' => $post
) );
?>
<aiopod-player class="aiopod-player aiopod-responsive-wrapper" data-post_id="<?php echo esc_attr( $episode_id ); ?>">
    <div class="aiopod-card aiopod-card--horizontal aiopod-card--player">
        <?php if ( empty( $image['src'] ) || aiopod_is_video_supported( $episode_id, $episode_file ) ) : ?>
            <div class="aiopod-card__image-wrapper aiopod-player__image-wrapper" style="display: none;">
                <img alt="" />
        <?php else : ?>
            <div class="aiopod-card__image-wrapper aiopod-player__image-wrapper">
                <?php aiopod_the_image( $image ); ?>
        <?php endif; ?>
            <button type="button" class="aiopod-button aiopod-button--play aiopod-button--overlay" aria-label="<?php esc_attr_e( 'Play episode', 'aio-podcast-studio' ); ?>">
                <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>
                <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>
            </button>
        </div>

        <div class="aiopod-card__content-wrapper aiopod-player__content-wrapper">
            <?php if ( ! empty( $attributes['show_player_episode_title'] ) ) : ?>
                <div class="aiopod-player__title aiopod-text-large">
                    <a href="<?php echo esc_url( $episode_url ); ?>" class="aiopod-link-unstyled" title="<?php echo esc_attr( $episode_title ); ?>">
                        <?php echo wp_kses_post( $episode_title ); ?>
                    </a>
                </div>
            <?php endif; ?>

            <?php
            // Prepare primary metadata
            $primary_meta = array();

            // Episode type    
            if ( ! empty( $attributes['show_player_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-player__episode-type">%s</span>', 
                        esc_html( $episode_types[ $episode_type ] ) 
                    );
                }
            }

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

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

            // Episode File size
            if ( ! empty( $attributes['show_player_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-player__episode-file-size">%s</span>', 
                        esc_html( aiopod_format_file_size( $episode_file_size ) )
                    );
                }
            }

            // Episode Duration
            if ( ! empty( $attributes['show_player_episode_duration'] ) ) { 
                $episode_duration = get_post_meta( $episode_id, 'episode_duration', true );

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

            // Explicit badge
            if ( ! empty( $attributes['show_player_explicit_badge'] ) ) { 
                $is_explicit = get_post_meta( $episode_id, 'is_explicit', true );

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

            // Print primary metadata
            if ( ! empty( $primary_meta ) ) {
                echo '<div class="aiopod-player__meta aiopod-player__meta--primary aiopod-leading-normal aiopod-text-muted aiopod-text-small">';
                echo implode( '<span class="aiopod-separator" aria-hidden="true">•</span>', $primary_meta );
                echo '</div>';
            } else {
                echo '<div class="aiopod-player__meta aiopod-player__meta--primary aiopod-leading-normal aiopod-text-muted aiopod-text-small" style="display: none;"></div>';
            }
            ?>

            <?php               
            // Prepare secondary metadata
            $secondary_meta = array();

            // Date Added
            if ( ! empty( $attributes['show_player_episode_date'] ) ) {
                $html  = '<span class="aiopod-player__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;
            }

            // Podcast title
            if ( ! empty( $attributes['show_player_podcast_title'] ) ) {
                if ( ! empty( $podcast_id ) ) {
                    $html  = '<div class="aiopod-player__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;
                }
            }

            // Tags
            if ( ! empty( $attributes['show_player_episode_tags'] ) ) {
                if ( $term_links = aiopod_get_term_links( $episode_id, 'aiopod_tags' ) ) {
                    $html  = '<div class="aiopod-player__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;
                }   
            }                        

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

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

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

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

            // Print secondary metadata
            if ( ! empty( $secondary_meta ) ) {
                echo '<div class="aiopod-player__meta aiopod-player__meta--secondary aiopod-leading-normal aiopod-text-muted aiopod-text-small">';
                echo implode( '', $secondary_meta );
                echo '</div>';
            } else {
                echo '<div class="aiopod-player__meta aiopod-player__meta--secondary aiopod-leading-normal aiopod-text-muted aiopod-text-small" style="display: none;"></div>';
            }
            ?>

            <!-- Player -->
            <?php aiopod_the_player( $post, $attributes ); ?>

            <?php
            // Actions: Subscribe, Follow, Share
            $subscribe_buttons = array();
            if ( ! empty( $attributes['show_player_subscribe_buttons'] ) ) {
                $subscribe_buttons = aiopod_get_subscribe_buttons( $podcast_id );
            }

            $follow_buttons = array();
            if ( ! empty( $attributes['show_player_follow_buttons'] ) ) {
                $follow_buttons = aiopod_get_follow_buttons( $podcast_id );
            }

            $share_buttons = array();
            if ( ! empty( $attributes['show_player_share_buttons'] ) ) {
                $share_buttons = aiopod_get_share_buttons( $episode_id );
            }

            if ( ! empty( $subscribe_buttons ) || ! empty( $follow_buttons ) || ! empty( $share_buttons ) ) :
                ?>
                <div class="aiopod-player__meta aiopod-player__meta--buttons">
                    <!-- Button: Subscribe -->
                    <?php if ( ! empty( $subscribe_buttons ) ) : ?> 
                        <aiopod-modal data-modal="subscribe">
                            <button type="button" class="aiopod-button aiopod-button--modal">
                                <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>
                                <?php esc_html_e( 'Subscribe', 'aio-podcast-studio' ); ?>
                            </button>

                            <!-- Modal: Subscribe -->
                            <dialog class="aiopod aiopod-modal">
                                <div class="aiopod-modal__header">
                                    <div class="aiopod-modal__title"><?php esc_html_e( 'Listen on Your Favorite App', 'aio-podcast-studio' ); ?></div>
                                    <button type="button" class="aiopod-modal__close" aria-label="<?php esc_attr_e( 'Close', 'aio-podcast-studio' ); ?>">&times;</button>
                                </div>
                                <div class="aiopod-modal__body">
                                    <div class="aiopod-icons">
                                        <?php foreach ( $subscribe_buttons as $subscribe_button ) : ?>
                                            <a href="<?php echo esc_url( $subscribe_button['url'] ); ?>" class="aiopod-link-unstyled" title="<?php echo esc_attr( sprintf( __( 'Listen on %s', 'aio-podcast-studio' ), $subscribe_button['label'] ) ); ?>" target="_blank" rel="noopener noreferrer">
                                                <img src="<?php echo esc_url( $subscribe_button['image'] ); ?>" alt="<?php echo esc_attr( $subscribe_button['label'] ); ?>" width="32" height="32" />
                                            </a>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </dialog>
                        </aiopod-modal>
                    <?php endif; ?>

                    <!-- Button: Follow Us -->
                    <?php if( ! empty( $follow_buttons ) ) : ?> 
                        <aiopod-modal data-modal="follow">
                            <button type="button" class="aiopod-button aiopod-button--modal">
                                <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>
                                <?php esc_html_e( 'Follow Us', 'aio-podcast-studio' ); ?>
                            </button>

                            <!-- Modal: Follow -->
                            <dialog class="aiopod aiopod-modal">
                                <div class="aiopod-modal__header">
                                    <div class="aiopod-modal__title"><?php esc_html_e( 'Follow Us & Stay Updated', 'aio-podcast-studio' ); ?></div>
                                    <button type="button" class="aiopod-modal__close" aria-label="<?php esc_attr_e( 'Close', 'aio-podcast-studio' ); ?>">&times;</button>
                                </div>
                                <div class="aiopod-modal__body">
                                    <div class="aiopod-icons">
                                        <?php foreach ( $follow_buttons as $follow_button ) : ?>
                                            <a href="<?php echo esc_url( $follow_button['url'] ); ?>" class="aiopod-link-unstyled" title="<?php echo esc_attr( $follow_button['label'] ); ?>" target="_blank" rel="noopener noreferrer">
                                                <span class="aiopod-icon <?php echo esc_attr( $follow_button['icon'] ); ?>"></span>
                                            </a>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </dialog>
                        </aiopod-modal>
                    <?php endif; ?>

                    <!-- Button: Share Episode -->
                    <?php if( ! empty( $share_buttons ) ) : ?> 
                        <aiopod-modal data-modal="share">
                            <button type="button" class="aiopod-button aiopod-button--modal">
                                <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>
                                <?php esc_html_e( 'Share Episode', 'aio-podcast-studio' ); ?>
                            </button>

                            <!-- Modal: Share -->
                            <dialog class="aiopod aiopod-modal">
                                <div class="aiopod-modal__header">
                                    <div class="aiopod-modal__title"><?php esc_html_e( 'Share This Episode', 'aio-podcast-studio' ); ?></div>
                                    <button type="button" class="aiopod-modal__close" aria-label="<?php esc_attr_e( 'Close', 'aio-podcast-studio' ); ?>">&times;</button>
                                </div>
                                <div class="aiopod-modal__body">
                                    <div class="aiopod-icons">
                                        <?php foreach ( $share_buttons as $share_button ) : ?>
                                            <a href="<?php echo esc_url( $share_button['url'] ); ?>" class="aiopod-link-unstyled" title="<?php echo esc_attr( $share_button['label'] ); ?>" target="_blank" rel="noopener noreferrer">
                                                <span class="aiopod-icon <?php echo esc_attr( $share_button['icon'] ); ?>"></span>
                                            </a>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </dialog>
                        </aiopod-modal>
                    <?php endif; ?>
                </div>
            <?php else : ?>
                <div class="aiopod-player__meta--buttons" style="display: none;"></div>
            <?php endif; ?>
        </div>
    </div>
</aiopod-player>