<?php

function aurora_lite_enqueue_single_post_scripts() {
    
    if ( ! is_single() ) {
        return;
    }

    $layout = strtolower( get_theme_mod( 'aurora_lite_single_post_layout', 'Default' ) );

    $def_path = get_template_directory() . '/assets/js/single-posts/def-com.js';
    $def_url  = get_template_directory_uri() . '/assets/js/single-posts/def-com.js';

    if ( file_exists( $def_path ) ) {
        wp_enqueue_script(
            'aurora-lite-single-def-com',
            $def_url,
            [],
            filemtime( $def_path ),
            true
        );
    }
    

    $copy_path = get_template_directory() . '/assets/js/single-posts/copy-link.js';
    $copy_url  = get_template_directory_uri() . '/assets/js/single-posts/copy-link.js';

    if ( file_exists( $copy_path ) ) {
        wp_enqueue_script(
            'aurora-lite-copy-link',
            $copy_url,
            [],
            filemtime( $copy_path ),
            true
        );

        wp_localize_script(
            'aurora-lite-copy-link',
            'aurora_lite_share_texts',
            [
                'copyLink' => esc_html__( 'Link copied to clipboard', 'aurora-lite' ),
            ]
        );
    }
}
add_action( 'wp_enqueue_scripts', 'aurora_lite_enqueue_single_post_scripts' );


function aurora_lite_single_post_vars() {

    if ( ! is_single() ) return;

    $featured_img_url = get_the_post_thumbnail_url( get_the_ID(), 'full' );

    if ( ! $featured_img_url ) return;

    wp_add_inline_style( 'aurora-lite-style', ':root { --post-bgi: url("' . esc_url( $featured_img_url ) . '"); }' );
}
add_action( 'wp_enqueue_scripts', 'aurora_lite_single_post_vars' );



function aurora_lite_dropcase_body_class($classes) {

    if (is_singular('post')) {
        $style = get_theme_mod('aurora_lite_dropcase_style', 'none');

        if ($style !== 'none') {
            $classes[] = 'dropcase-' . sanitize_html_class($style);
        }
    }

    return $classes;
}
add_filter('body_class', 'aurora_lite_dropcase_body_class');



function aurora_lite_display_share_buttons() {
    if ( ! is_single() ) return;

    $post_url   = urlencode( get_permalink() );
    $post_title = urlencode( get_the_title() );

    $share_links = array(
        'copy_link'  => "javascript:void(0)",
        'facebook'   => "https://www.facebook.com/sharer/sharer.php?u=$post_url",
        'twitter'    => "https://twitter.com/intent/tweet?text=$post_title&url=$post_url",
        'telegram'   => "https://t.me/share/url?url=$post_url",
        'messenger'  => "https://www.facebook.com/dialog/send?link=$post_url",
        'whatsapp'   => "https://api.whatsapp.com/send?text=$post_title%20$post_url",
        'reddit'     => "https://www.reddit.com/submit?url=$post_url",
        'tumblr'     => "https://www.tumblr.com/widgets/share/tool?canonicalUrl=$post_url",
        'email'      => "mailto:?subject=$post_title&body=$post_url",
        'sms'        => "sms:?&body=$post_title%20$post_url",
    );

    $share_details = [
        'copy_link' => [ 'title' => esc_html__('Copy link to clipboard', 'aurora-lite'), 'icon' => 'fa-regular fa-copy' ],
        'facebook'  => [ 'title' => esc_html__('Share on Facebook', 'aurora-lite'),      'icon' => 'fa-brands fa-facebook-f' ],
        'twitter'   => [ 'title' => esc_html__('Share on X', 'aurora-lite'),             'icon' => 'fa-brands fa-x-twitter' ],
        'telegram'  => [ 'title' => esc_html__('Share on Telegram', 'aurora-lite'),      'icon' => 'fa-brands fa-telegram' ],
        'messenger' => [ 'title' => esc_html__('Share on Messenger', 'aurora-lite'),     'icon' => 'fa-brands fa-facebook-messenger' ],
        'whatsapp'  => [ 'title' => esc_html__('Share on WhatsApp', 'aurora-lite'),      'icon' => 'fa-brands fa-whatsapp' ],
        'reddit'    => [ 'title' => esc_html__('Share on Reddit', 'aurora-lite'),        'icon' => 'fa-brands fa-reddit-alien' ],
        'tumblr'    => [ 'title' => esc_html__('Share on Tumblr', 'aurora-lite'),        'icon' => 'fa-brands fa-tumblr' ],
        'email'     => [ 'title' => esc_html__('Share via Email', 'aurora-lite'),        'icon' => 'fa-regular fa-envelope' ],
        'sms'       => [ 'title' => esc_html__('Share via SMS', 'aurora-lite'),          'icon' => 'fa-regular fa-message' ],
    ];

    foreach ( $share_links as $key => $url ) {
        if ( get_theme_mod( "aurora_lite_share_$key", true ) ) {
            $title      = $share_details[ $key ]['title'];
            $icon_class = $share_details[ $key ]['icon'];
            $target     = ( $key === 'copy_link' ) ? '' : 'target="_blank"';
            $href       = ( $key === 'copy_link' ) ? 'javascript:void(0)' : esc_url( $url );

            echo '<a href="' . $href . '" class="social-icon" data-share="' . esc_attr( $key ) . '" ' . $target . ' title="' . esc_attr( $title ) . '">'
                . '<i class="' . esc_attr( $icon_class ) . '"></i>'
                . '</a>';
        }
    }
}


function aurora_lite_breadcrumbs() {

    echo '<nav class="aurora-lite-breadcrumbs">';
    echo '<a href="' . home_url() . '">' . esc_html__('Home', 'aurora-lite') . '</a><span class="bd">›</span>';

    $category = get_the_category();
    if ($category) {
        echo '<a href="' . get_category_link($category[0]->term_id) . '">' . esc_html($category[0]->name) . '</a><span class="bd">›</span>';
    }

    echo '<span class="bcp">' . get_the_title() . '</span>';
    echo '</nav>';
}

function aurora_lite_reading_time( $post_id = null ) {
    $post = get_post( $post_id );

    if ( ! $post ) {
        return;
    }

    $content    = wp_strip_all_tags( $post->post_content );
    $word_count = str_word_count( $content );
    $minutes    = max( 1, (int) ceil( $word_count / 200 ) );

    $label = sprintf(
        /* translators: %d: estimated reading time in minutes */
        _n( '%d min read', '%d min read', $minutes, 'aurora-lite' ),
        $minutes
    );

    echo '<span class="aurora-lite-reading-time meta-text">';
    echo '<span class="aurora-lite-reading-time__label">' . esc_html( $label ) . '</span>';
    echo '</span>';
}