<?php

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

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

$image = aiopod_get_image( array(     
    'term'     => $term,
    'taxonomy' => 'aiopod_categories'
) );
?>
<div class="aiopod aiopod-single aiopod-single--category">            
    <?php 
    if ( ! empty( $attributes['show_breadcrumbs'] ) ) {
        aiopod_the_taxonomy_breadcrumbs( $term, 'aiopod_categories', $attributes['current_page_id'] );
    }

    if ( ! empty( $image['src'] ) || ! empty( $term->description ) ) {
        echo '<div class="aiopod-category aiopod-responsive-wrapper">';
        echo '<div class="aiopod-card aiopod-card--horizontal aiopod-card--no-border aiopod-card--category">';

        // Image
        if ( ! empty( $image['src'] ) ) {
            echo '<div class="aiopod-card__image-wrapper aiopod-category__image-wrapper">';
            aiopod_the_image( $image );
            echo '</div>';
        }

        // Description
        if ( ! empty( $term->description ) ) {
            echo '<div class="aiopod-card__content-wrapper aiopod-category__content-wrapper">';
            echo '<div class="aiopod-category__description">';
            echo wp_kses_post( wpautop( $term->description ) );
            echo '</div>';
            echo '</div>';
        }

        echo '</div>';
        echo '</div>';
    }

    // Subcategories
    $show_subcategories = ! empty( $attributes['show_subcategories'] );
    $subcategories      = '';

    if ( $show_subcategories ) {
        $subcategories_args = array( 
            'id'    => $term->term_id,
            'title' => $attributes['subcategories_title'],
            'echo'  => 0
        );

        $subcategories_args = apply_filters( 'aiopod_single_category_subcategories_args', $subcategories_args );
        $subcategories = aiopod_the_categories( $subcategories_args ); 
    }

    // Podcasts
    $show_podcasts = ! empty( $attributes['show_podcasts'] );
    $podcasts      = '';

    if ( $show_podcasts ) {
        $podcasts_args = array( 
            'categories' => $term->term_id,
            'title'      => $attributes['podcasts_title'],
            'echo'       => 0
        );

        $podcasts_args = apply_filters( 'aiopod_single_category_podcasts_args', $podcasts_args );
        $podcasts = aiopod_the_podcasts( $podcasts_args );
    } 
    
    // Output
    if ( $show_subcategories && $show_podcasts ) {
        $is_subcategories_empty = empty( $subcategories ) || str_contains( $subcategories, 'aiopod-categories__empty' );
        $is_podcasts_empty      = empty( $podcasts ) || str_contains( $podcasts, 'aiopod-podcasts__empty' );

        if ( ! $is_subcategories_empty && ! $is_podcasts_empty ) {
            if ( 'before_podcasts' === $attributes['subcategories_position'] ) {
                echo $subcategories;
            }

            echo $podcasts;

            if ( 'after_podcasts' === $attributes['subcategories_position'] ) {
                echo $subcategories;
            }
        } elseif ( ! $is_subcategories_empty && $is_podcasts_empty ) {
            echo $subcategories;
        } elseif ( $is_subcategories_empty && ! $is_podcasts_empty ) {
            echo $podcasts;
        }  else {
            echo $podcasts;
        }
    } elseif ( $show_subcategories ) {
        echo $subcategories;
    } elseif ( $show_podcasts ) {
        echo $podcasts;
    }
    ?> 
</div>