<?php
/**
 * Template part for displaying Video Format
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package handmade_craft
 */

?>
<?php $handmade_craft_readmore = get_theme_mod( 'handmade_craft_readmore_button_text','Read More');?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<div class="mag-post-single">
        <?php
			// Get the post ID
			$handmade_craft_post_id = get_the_ID();

			// Check if there are videos embedded in the post content
			$handmade_craft_post = get_post($handmade_craft_post_id);
			$handmade_craft_content = do_shortcode(apply_filters('the_content', $handmade_craft_post->post_content));
			$handmade_craft_embeds = get_media_embedded_in_content($handmade_craft_content);

			if (!empty($handmade_craft_embeds)) {
			    // Loop through embedded media and display videos
			    foreach ($handmade_craft_embeds as $handmade_craft_embed) {
			        // Check if the embed code contains a video tag or specific video providers like YouTube or Vimeo
			        if (strpos($handmade_craft_embed, 'video') !== false || strpos($handmade_craft_embed, 'youtube') !== false || strpos($handmade_craft_embed, 'vimeo') !== false || strpos($handmade_craft_embed, 'dailymotion') !== false || strpos($handmade_craft_embed, 'vine') !== false || strpos($handmade_craft_embed, 'wordPress.tv') !== false || strpos($handmade_craft_embed, 'hulu') !== false) {
			            ?>
			            <div class="custom-embedded-video">
			                <div class="video-container">
			                    <?php echo esc_url($handmade_craft_embed); ?>
			                </div>
			                <div class="video-comments">
			                    <?php
			                    // Add your comments section here
			                    comments_template(); // This will include the default WordPress comments template
			                    ?>
			                </div>
			            </div>
			            <?php
			        }
			    }
			}
	    ?>
		<div class="mag-post-detail">
			<div class="mag-post-category">
				<?php handmade_craft_categories_list(); ?>
			</div>
			<?php
			if ( is_singular() ) :
				the_title( '<h1 class="entry-title mag-post-title">', '</h1>' );
			else :
				if ( get_theme_mod( 'handmade_craft_post_hide_post_heading', true ) ) { 
					the_title( '<h2 class="entry-title mag-post-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
			    }
			endif;
			?>
			<div class="mag-post-meta">
				<?php
				handmade_craft_posted_on();
				handmade_craft_posted_by();
				handmade_craft_posted_comments();
				handmade_craft_posted_time();
				?>
			</div>
			<?php if ( get_theme_mod( 'handmade_craft_post_hide_post_content', true ) ) { ?>
				<div class="mag-post-excerpt">
					<?php the_excerpt(); ?>
				</div>
		    <?php } ?>
			<?php if ( get_theme_mod( 'handmade_craft_post_readmore_button', true ) === true ) : ?>
				<div class="mag-post-read-more">
					<a href="<?php the_permalink(); ?>" class="read-more-button">
						<?php if ( ! empty( $handmade_craft_readmore ) ) { ?> <?php echo esc_html( $handmade_craft_readmore ); ?> <?php } ?>
						<i class="<?php echo esc_attr( get_theme_mod( 'handmade_craft_readmore_btn_icon', 'fas fa-chevron-right' ) ); ?>"></i>
					</a>
				</div>
			<?php endif; ?>
		</div>
	</div>

</article><!-- #post-<?php the_ID(); ?> -->