<?php

function aurora_lite_empty_home_section( $is_hidden = false ) {
    if ( ! is_customize_preview() ) {
        return;
    }

    $message = $is_hidden
        ? __( 'This section is hidden. Click the pencil icon to restore it.', 'aurora-lite' )
        : __( 'No posts found for this source. Showing recent posts instead.', 'aurora-lite' );

    echo '<div class="aurora-lite-empty-section">';
    echo esc_html( $message );
    echo '</div>';
}

function aurora_lite_homepage_section_edit( $section_id ) {

    if ( ! is_customize_preview() ) { return; }

    if ( empty( $section_id ) ) { return; }

    $section_id = sanitize_key( $section_id );

    echo '<button 
        type="button"
        class="aurora-lite-edit-section"
        data-section="' . esc_attr( $section_id ) . '"
        title="' . esc_attr__( 'Click to edit this section', 'aurora-lite' ) . '"
    >';

    echo '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none"
        stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <path d="M12 20h9"></path>
        <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4 12.5-12.5z"></path>
    </svg>';

    echo '</button>';
}

function aurora_lite_get_section_query( $section_id ) {

    $base_args = [
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'posts_per_page' => 30,
        'orderby'        => 'date',
        'order'          => 'DESC',
    ];

    $source        = get_theme_mod("{$section_id}_source", 'category');
    $category      = get_theme_mod("{$section_id}_category", 0);
    $tag           = get_theme_mod("{$section_id}_tag", 0);
    $include_posts = get_theme_mod("{$section_id}_posts", []);
    $exclude_posts = get_theme_mod("{$section_id}_exclude_posts", []);

    if ( $source === 'none' ) {
        return null;
    }

    if ( is_string($include_posts) ) {
        $include_posts = array_filter( array_map( 'absint', explode( ',', $include_posts ) ) );
    }
    if ( is_string($exclude_posts) ) {
        $exclude_posts = array_filter( array_map( 'absint', explode( ',', $exclude_posts ) ) );
    }

    $fallback_args = $base_args;
    if ( ! empty($exclude_posts) ) {
        $fallback_args['post__not_in'] = $exclude_posts;
    }

    if ( $source === 'category' ) {
        $has_valid_source = ! empty( $category );
    } elseif ( $source === 'tag' ) {
        $has_valid_source = ! empty( $tag );
    } elseif ( $source === 'custom_posts' ) {
        $has_valid_source = ! empty( $include_posts );
    } else {
        $has_valid_source = false;
    }

    if ( ! $has_valid_source ) {
        return [ 'query' => new WP_Query( $fallback_args ), 'fallback' => true ];
    }

    $args = $base_args;

    if ( $source === 'custom_posts' ) {
        $args['post__in'] = $include_posts;
        $args['orderby']  = 'post__in';
    } elseif ( $source === 'category' ) {
        $args['cat'] = absint($category);
    } elseif ( $source === 'tag' ) {
        $args['tag_id'] = absint($tag);
    }

    if ( ! empty($exclude_posts) ) {
        $args['post__not_in'] = $exclude_posts;
    }

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        return [ 'query' => $query, 'fallback' => false ];
    }

    return [ 'query' => new WP_Query( $fallback_args ), 'fallback' => true ];
}

function aurora_lite_hs_bold( $posts_per_page = null ) {

    $result    = aurora_lite_get_section_query( 'bold' );
    $is_hidden = $result === null;
    $fallback  = ! $is_hidden && $result['fallback'];
    $query     = ! $is_hidden ? $result['query'] : null;
    $has_posts = ! $is_hidden && $query->have_posts();

    if ( $is_hidden && ! is_customize_preview() ) {
        return;
    }

    if ( ! $has_posts && ! is_customize_preview() ) {
        return;
    }
    ?>

    <section class="post-list bold homepage-section" data-section="bold" data-section-id="bold">

        <?php aurora_lite_homepage_section_edit( 'bold' ); ?>

        <?php if ( $is_hidden ) : ?>
            <?php aurora_lite_empty_home_section( true ); ?>
        <?php else : ?>

            <?php if ( $fallback ) : ?>
                <?php aurora_lite_empty_home_section( false ); ?>
            <?php endif; ?>

            <?php
            $count = 0;
            while ( $query->have_posts() ) : $query->the_post();

                if ( $posts_per_page !== null && $count >= $posts_per_page ) {
                    break;
                }
                $count++;
            ?>

                <article class="featured-post">

                    <div class="featured-image"
                         style="background-image: url('<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'large' ) ); ?>');">
                    </div>

                    <div class="post-content">

                        <div class="post-meta">
                            <span class="post-category"><?php aurora_lite_get_categories(); ?></span>
                            <time class="post-date" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date( aurora_lite_get_date_format() ) ); ?></time>
                        </div>

                        <a href="<?php the_permalink(); ?>">
                            <h3 class="post-title"><?php echo esc_html( get_the_title() ); ?></h3>
                        </a>

                        <p class="excerpt">
                            <?php echo esc_html( wp_trim_words( get_the_excerpt(), 20 ) ); ?>
                        </p>

                        <a class="read-more" href="<?php the_permalink(); ?>"><?php esc_html_e( 'View post', 'aurora-lite' ); ?></a>

                    </div>
                </article>

            <?php endwhile; wp_reset_postdata(); ?>

        <?php endif; ?>

    </section>

    <?php
}

function aurora_lite_hs_overlay( $posts_per_page = null ) {

    $result    = aurora_lite_get_section_query( 'overlay' );
    $is_hidden = $result === null;
    $fallback  = ! $is_hidden && $result['fallback'];
    $query     = ! $is_hidden ? $result['query'] : null;
    $has_posts = ! $is_hidden && $query->have_posts();

    if ( $is_hidden && ! is_customize_preview() ) {
        return;
    }

    if ( ! $has_posts && ! is_customize_preview() ) {
        return;
    }
    ?>

    <section class="homepage-section overlay-section" data-section="overlay" data-section-id="overlay">

        <?php aurora_lite_homepage_section_edit( 'overlay' ); ?>

        <?php if ( $is_hidden ) : ?>
            <?php aurora_lite_empty_home_section( true ); ?>
        <?php else : ?>

            <?php if ( $fallback ) : ?>
                <?php aurora_lite_empty_home_section( false ); ?>
            <?php endif; ?>

            <div class="post-cards homepage-posts">

                <?php
                $count = 0;
                while ( $query->have_posts() ) : $query->the_post();

                    if ( $posts_per_page !== null && $count >= $posts_per_page ) {
                        break;
                    }
                    $count++;
                ?>

                    <article class="preview-post post-card">

                        <div class="featured-image" style="background-image: url('<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'large' ) ); ?>');"></div>

                        <div class="post-content">
                            <div class="post-meta">
                                <span class="post-category"><?php aurora_lite_get_categories(); ?></span>
                                <span class="divider">•</span>
                                <time class="post-date" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date( aurora_lite_get_date_format() ) ); ?></time>
                            </div>

                            <h3 class="post-title">
                                <a href="<?php the_permalink(); ?>"><?php echo esc_html( get_the_title() ); ?></a>
                            </h3>
                        </div>

                    </article>

                <?php endwhile; wp_reset_postdata(); ?>

            </div>

        <?php endif; ?>

    </section>

    <?php
}

function aurora_lite_hs_mag( $posts_per_page = null ) {

    $result    = aurora_lite_get_section_query( 'mag' );
    $is_hidden = $result === null;
    $fallback  = ! $is_hidden && $result['fallback'];
    $query     = ! $is_hidden ? $result['query'] : null;
    $has_posts = ! $is_hidden && $query->have_posts();

    if ( $is_hidden && ! is_customize_preview() ) {
        return;
    }

    if ( ! $has_posts && ! is_customize_preview() ) {
        return;
    }
    ?>

    <section class="homepage-section magazine-section" data-section="mag" data-section-id="mag">

        <?php aurora_lite_homepage_section_edit( 'mag' ); ?>

        <?php if ( $is_hidden ) : ?>
            <?php aurora_lite_empty_home_section( true ); ?>
        <?php else : ?>

            <?php if ( $fallback ) : ?>
                <?php aurora_lite_empty_home_section( false ); ?>
            <?php endif; ?>

            <div class="post-list homepage-posts">

                <?php
                $count = 0;
                while ( $query->have_posts() ) : $query->the_post();

                    if ( $posts_per_page !== null && $count >= $posts_per_page ) {
                        break;
                    }
                    $count++;
                ?>

                    <article class="preview-post mag">

                        <div class="featured-image" style="background-image: url('<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'large' ) ); ?>'); background-position: center; background-size: cover;"></div>

                        <div class="post-content">
                            <div class="post-meta">
                                <span class="post-category"><?php aurora_lite_get_categories(); ?></span>
                                <span class="divider">•</span>
                                <time class="post-date" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date( aurora_lite_get_date_format() ) ); ?></time>
                            </div>

                            <h3 class="post-title">
                                <a href="<?php the_permalink(); ?>"><?php echo esc_html( get_the_title() ); ?></a>
                            </h3>

                            <p class="post-excerpt">
                                <?php echo esc_html( wp_trim_words( get_the_excerpt(), 15 ) ); ?>
                            </p>

                        </div>

                    </article>

                <?php endwhile; wp_reset_postdata(); ?>

            </div>

        <?php endif; ?>

    </section>

    <?php
}

function aurora_lite_hs_embed( $posts_per_page = null ) {

    $result    = aurora_lite_get_section_query( 'embed' );
    $is_hidden = $result === null;
    $fallback  = ! $is_hidden && $result['fallback'];
    $query     = ! $is_hidden ? $result['query'] : null;
    $has_posts = ! $is_hidden && $query->have_posts();

    if ( $is_hidden && ! is_customize_preview() ) {
        return;
    }

    if ( ! $has_posts && ! is_customize_preview() ) {
        return;
    }
    ?>

    <section class="homepage-section embed-section" data-section="embed" data-section-id="embed">

        <?php aurora_lite_homepage_section_edit( 'embed' ); ?>

        <?php if ( $is_hidden ) : ?>
            <?php aurora_lite_empty_home_section( true ); ?>
        <?php else : ?>

            <?php if ( $fallback ) : ?>
                <?php aurora_lite_empty_home_section( false ); ?>
            <?php endif; ?>

            <div class="embed">

                <?php
                $count = 0;
                while ( $query->have_posts() ) : $query->the_post();

                    if ( $posts_per_page !== null && $count >= $posts_per_page ) {
                        break;
                    }
                    $count++;
                ?>

                    <article class="card-embed post-preview"
                         style="background: linear-gradient(rgba(146, 146, 146, 0), rgba(0, 0, 0, 0.48)), url('<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'large' ) ); ?>'); background-size: cover; background-position: center;">

                        <span class="post-category">
                            <?php aurora_lite_get_categories(); ?>
                        </span>

                        <div class="post-details">
                            <time class="post-date" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date( aurora_lite_get_date_format() ) ); ?></time>

                            <h3 class="post-title">
                                <a href="<?php the_permalink(); ?>">
                                    <?php echo esc_html( get_the_title() ); ?>
                                </a>
                            </h3>

                            <p class="post-excerpt">
                                <?php echo esc_html( wp_trim_words( get_the_excerpt(), 18 ) ); ?>
                            </p>
                        </div>

                    </article>

                <?php endwhile; wp_reset_postdata(); ?>

            </div>

        <?php endif; ?>

    </section>

    <?php
}

function aurora_lite_hs_plain( $posts_per_page = null ) {

    $result    = aurora_lite_get_section_query( 'plain' );
    $is_hidden = $result === null;
    $fallback  = ! $is_hidden && $result['fallback'];
    $query     = ! $is_hidden ? $result['query'] : null;
    $has_posts = ! $is_hidden && $query->have_posts();

    if ( $is_hidden && ! is_customize_preview() ) {
        return;
    }

    if ( ! $has_posts && ! is_customize_preview() ) {
        return;
    }
    ?>

    <section class="homepage-section plain-section" data-section="plain" data-section-id="plain">

        <?php aurora_lite_homepage_section_edit( 'plain' ); ?>

        <?php if ( $is_hidden ) : ?>
            <?php aurora_lite_empty_home_section( true ); ?>
        <?php else : ?>

            <?php if ( $fallback ) : ?>
                <?php aurora_lite_empty_home_section( false ); ?>
            <?php endif; ?>

            <div class="plain-posts">

                <?php
                $count = 0;
                while ( $query->have_posts() ) : $query->the_post();

                    if ( $posts_per_page !== null && $count >= $posts_per_page ) {
                        break;
                    }
                    $count++;
                ?>

                    <article class="post-preview plain-post">

                        <h3 class="post-title">
                            <a href="<?php the_permalink(); ?>">
                                <?php echo esc_html( get_the_title() ); ?>
                            </a>
                        </h3>

                        <p class="post-excerpt">
                            <?php echo esc_html( wp_trim_words( get_the_excerpt(), 13 ) ); ?>
                        </p>

                        <div class="post-meta">
                            <span class="post-cat"><?php aurora_lite_get_categories(); ?></span>
                            <span class="divider">•</span>
                            <time class="post-date" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date( aurora_lite_get_date_format() ) ); ?></time>
                        </div>

                    </article>

                <?php endwhile; wp_reset_postdata(); ?>

            </div>

        <?php endif; ?>

    </section>

    <?php
}

function aurora_lite_hs_genteel( $posts_per_page = null ) {

    $result    = aurora_lite_get_section_query( 'genteel' );
    $is_hidden = $result === null;
    $fallback  = ! $is_hidden && $result['fallback'];
    $query     = ! $is_hidden ? $result['query'] : null;
    $has_posts = ! $is_hidden && $query->have_posts();

    if ( $is_hidden && ! is_customize_preview() ) {
        return;
    }

    if ( ! $has_posts && ! is_customize_preview() ) {
        return;
    }
    ?>

    <section class="homepage-section genteel-section" data-section="genteel" data-section-id="genteel">

        <?php aurora_lite_homepage_section_edit( 'genteel' ); ?>

        <?php if ( $is_hidden ) : ?>
            <?php aurora_lite_empty_home_section( true ); ?>
        <?php else : ?>

            <?php if ( $fallback ) : ?>
                <?php aurora_lite_empty_home_section( false ); ?>
            <?php endif; ?>

            <div class="post-cards homepage-posts">

                <?php
                $count = 0;
                while ( $query->have_posts() ) : $query->the_post();

                    if ( $posts_per_page !== null && $count >= $posts_per_page ) {
                        break;
                    }
                    $count++;
                ?>

                    <article class="preview-post post-card">

                        <div class="featured-image" style="background-image: url('<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'large' ) ); ?>');"></div>

                        <div class="post-content">
                            <div class="post-meta">
                                <span class="post-category"><?php aurora_lite_get_categories(); ?></span>
                                <span class="divider">•</span>
                                <time class="post-date" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date( aurora_lite_get_date_format() ) ); ?></time>
                            </div>

                            <h3 class="post-title">
                                <a href="<?php the_permalink(); ?>"><?php echo esc_html( get_the_title() ); ?></a>
                            </h3>
                        </div>

                    </article>

                <?php endwhile; wp_reset_postdata(); ?>

            </div>

        <?php endif; ?>

    </section>

    <?php
}

function aurora_lite_hs_poster( $posts_per_page = null ) {

    $result    = aurora_lite_get_section_query( 'poster' );
    $is_hidden = $result === null;
    $fallback  = ! $is_hidden && $result['fallback'];
    $query     = ! $is_hidden ? $result['query'] : null;
    $has_posts = ! $is_hidden && $query->have_posts();

    if ( $is_hidden && ! is_customize_preview() ) {
        return;
    }

    if ( ! $has_posts && ! is_customize_preview() ) {
        return;
    }
    ?>

    <section class="homepage-section poster-section" data-section="poster" data-section-id="poster">

        <?php aurora_lite_homepage_section_edit( 'poster' ); ?>

        <?php if ( $is_hidden ) : ?>
            <?php aurora_lite_empty_home_section( true ); ?>
        <?php else : ?>

            <?php if ( $fallback ) : ?>
                <?php aurora_lite_empty_home_section( false ); ?>
            <?php endif; ?>

            <div class="poster">

                <?php
                $count = 0;
                while ( $query->have_posts() ) : $query->the_post();

                    if ( $posts_per_page !== null && $count >= $posts_per_page ) {
                        break;
                    }
                    $count++;
                ?>

                    <article class="card-poster post-preview"
                         style="background: linear-gradient(rgba(146, 146, 146, 0), rgba(0, 0, 0, 0.48)), url('<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'large' ) ); ?>'); background-size: cover; background-position: center;">

                        <span class="post-category"><?php aurora_lite_get_categories(); ?></span>

                        <div class="post-details">

                            <h3 class="post-title">
                                <a href="<?php the_permalink(); ?>">
                                    <?php echo esc_html( get_the_title() ); ?>
                                </a>
                            </h3>

                            <div class="metas">
                                <span class="post-author">
                                    <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
                                        <?php echo esc_html( get_the_author() ); ?>
                                    </a>
                                </span>
                                <time class="post-date" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date( aurora_lite_get_date_format() ) ); ?></time>
                            </div>

                        </div>

                    </article>

                <?php endwhile; wp_reset_postdata(); ?>

            </div>

        <?php endif; ?>

    </section>

    <?php
}

function aurora_lite_hs_sweet( $posts_per_page = null ) {

    $result    = aurora_lite_get_section_query( 'sweet' );
    $is_hidden = $result === null;
    $fallback  = ! $is_hidden && $result['fallback'];
    $query     = ! $is_hidden ? $result['query'] : null;
    $has_posts = ! $is_hidden && $query->have_posts();

    if ( $is_hidden && ! is_customize_preview() ) {
        return;
    }

    if ( ! $has_posts && ! is_customize_preview() ) {
        return;
    }
    ?>

    <section class="post-list sweet homepage-section" data-section="sweet" data-section-id="sweet">

        <?php aurora_lite_homepage_section_edit( 'sweet' ); ?>

        <?php if ( $is_hidden ) : ?>
            <?php aurora_lite_empty_home_section( true ); ?>
        <?php else : ?>

            <?php if ( $fallback ) : ?>
                <?php aurora_lite_empty_home_section( false ); ?>
            <?php endif; ?>

            <?php
            $count = 0;
            while ( $query->have_posts() ) : $query->the_post();

                if ( $posts_per_page !== null && $count >= $posts_per_page ) {
                    break;
                }
                $count++;
            ?>

                <article class="featured-post">

                    <div class="featured-image"
                         style="background-image: url('<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'large' ) ); ?>');">
                    </div>

                    <div class="post-content">

                        <span class="post-category"><?php aurora_lite_get_categories(); ?></span>

                        <a href="<?php the_permalink(); ?>">
                            <h3 class="post-title"><?php echo esc_html( get_the_title() ); ?></h3>
                        </a>

                        <div class="post-meta">
                            <span class="post-author">
                                <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
                                    <?php echo esc_html( get_the_author() ); ?>
                                </a>
                            </span>
                            <time class="post-date" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date( aurora_lite_get_date_format() ) ); ?></time>
                        </div>

                        <a class="read-more" href="<?php the_permalink(); ?>"><?php esc_html_e( 'View post', 'aurora-lite' ); ?></a>

                    </div>
                </article>

            <?php endwhile; wp_reset_postdata(); ?>

        <?php endif; ?>

    </section>

    <?php
}