<?php

/**
 * Outputs the <template> used to update the player when this episode is selected.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

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

$episode_id    = get_the_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;
}

$episode_file_type = aiopod_get_episode_file_type( $episode_id, $episode_file );

$image = aiopod_get_image( array( 
    'post' => $post,
    'alt'  => ''
) );
?>
<template class="aiopod-template">
    <!-- JSON data to populate the player -->
    <?php
    $json_data = array(
        'id'        => $episode_id,
        'title'     => $episode_title,
        'src'       => $episode_file,
        'type'      => $episode_file_type,
        'download'  => aiopod_get_episode_file_url( $post ),
        'image'     => $image['src'],
        'image_alt' => $image['alt']
    );

    // Add chapters if available.
	$chapters = aiopod_get_chapters( $episode_id );

	if ( ! empty( $chapters ) ) {
		$json_data['chapters'] = $chapters;
	}
    ?>
    <script type="application/json" class="aiopod-template__data">
    <?php echo wp_json_encode( $json_data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ); ?>
    </script>

    <!-- HTML fragments to populate the player -->
    <?php if ( ! empty( $attributes['show_player_episode_title'] ) ) : ?>
        <div class="aiopod-template__title">
            <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();

    if ( ! empty( $attributes['show_player_episode_type'] ) ) { // 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 ] ) 
            );
        }
    }

    if ( ! empty( $attributes['show_player_season_number'] ) ) { // 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 ) )
            );
        }
    }

    if ( ! empty( $attributes['show_player_episode_number'] ) ) { // 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 ) )
            );
        }
    }    

    if ( ! empty( $attributes['show_player_episode_file_size'] ) ) { // 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 ) )
            );
        }
    }

    if ( ! empty( $attributes['show_player_episode_duration'] ) ) { // 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 ) )
            );
        }
    }

    if ( ! empty( $attributes['show_player_explicit_badge'] ) ) { // 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-template__meta aiopod-template__meta--primary">';
        echo implode( '<span class="aiopod-separator" aria-hidden="true">•</span>', $primary_meta );
        echo '</div>';
    }
              
    // Prepare secondary metadata
    $secondary_meta = array();

    if ( ! empty( $attributes['show_player_episode_date'] ) ) {  // Date Added
        $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;
    }

    if ( ! empty( $attributes['show_player_podcast_title'] ) ) { // 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;
        }
    }

    if ( ! empty( $attributes['show_player_episode_tags'] ) ) { // 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;
        } 
    }                          

    if ( ! empty( $attributes['show_player_downloads_count'] ) ) { // 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 .= esc_html( sprintf(
            _n( '%s download', '%s downloads', $downloads_count, 'aio-podcast-studio' ),
            aiopod_format_number( $downloads_count )
        ) );
        $html .= '</span>';

        $secondary_meta[] = $html;
    }

    if ( ! empty( $attributes['show_player_plays_count'] ) ) { // 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 .= esc_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-template__meta aiopod-template__meta--secondary">';
        echo implode( '', $secondary_meta );
        echo '</div>';
    }

    // 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-template__meta aiopod-template__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 Us -->
                    <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 endif; ?>
</template>