<?php
if ( ! isset( $content_width ) ) {
    $content_width = 1180;
}

//Load customizer functions
require get_template_directory() . '/inc/customizer/sbwhea-customizer-main.php';
require get_template_directory() . '/inc/functions-comments.php';

//Load stylesheets
function sbwhea_scripts() {
    wp_enqueue_style( 'bootstrapcss', get_template_directory_uri() .'/assets/bootstrap/css/bootstrap.min.css');
    wp_enqueue_script( 'bootstrapjs', get_template_directory_uri() .'/assets/bootstrap/js/bootstrap.min.js', array( 'jquery'), '4.3.1', true );

    if( is_singular() && comments_open() && ( get_option( 'thread_comments' ) == 1) ) {
        wp_enqueue_script( 'comment-reply', 'wp-includes/js/comment-reply', array(), false, true );
    }

	wp_enqueue_style( 'font-awesome', get_template_directory_uri() .'/assets/fontawesome/css/all.css' );
    wp_enqueue_style( 'style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'sbwhea_scripts' );

function sbwhea_enqueue_admin_scripts(){
    global $pagenow;
    if ( $pagenow == 'index.php' || $pagenow == 'plugins.php' ) {
        wp_enqueue_script( 'sbw-bespoke-js', get_template_directory_uri() .'/assets/js/bespoke.js', array( 'jquery'), '1.0', true );
    }
}
add_action( 'admin_enqueue_scripts', 'sbwhea_enqueue_admin_scripts' );

function sbwhea_theme_support_setup() {
    $defaults = array(
        'height'      => 100,
        'width'       => 400,
        'flex-height' => true,
        'flex-width'  => true,
        'header-text' => array( 'site-title', 'site-description' ),
    );
    add_theme_support( 'custom-logo', $defaults );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'title-tag' );
    $args = array(
        'default-image'     => get_template_directory_uri() . '/inc/img/wedding_theme_wordpress_couple_standing_in_a_dessert.jpg',
        'width'             => 1920,
        'height'            => 800,
        'flex-width'        => true,
        'flex-height'       => true,
        'header-text'       => false
    );
    add_theme_support( 'custom-header', $args );
}
add_action( 'after_setup_theme', 'sbwhea_theme_support_setup' );

add_action( 'widgets_init', 'sbwhea_widgets_init' );
function sbwhea_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Page Sidebar', 'sbw-wedding' ),
        'id' => 'page-sidebar',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>',
    ) );
}

function sbwhea_excerpt_more( $more ) {
    return sprintf( '<a href="%1$s" class="more-link">%2$s</a>',
          esc_url( get_permalink( get_the_ID() ) ),
          sprintf( __( 'Continue reading <i class="fas fa-long-arrow-alt-right"></i> %s ', 'sbw-wedding' ), '<span class="screen-reader-text">' . get_the_title( get_the_ID() ) . '</span>' )
    );
}
add_filter( 'excerpt_more', 'sbwhea_excerpt_more' );

//Add an admin notice if SBW Timeline plugin is not installed
function sbwhea_admin_notices() {
    global $pagenow; print_r( $pagenow );
    if ( $pagenow == 'index.php' || $pagenow == 'plugins.php' ) {
        //if( ! function_exists( 'sbwhea_get_timeline' ) && empty( get_option( 'timeline_plugin_notice_dismissed' ) ) ) {
            $plugin_url = network_admin_url( 'plugin-install.php?s=SBW+Timeline&tab=search&type=term' );
            $message = sprintf( wp_kses( __( 'To add a timeline section to your website, please install the <a href="%s">SBW Timeline</a> plugin.', 'sbw-wedding' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( $plugin_url ) );
            ?>
            <div id="timeline-plugin-notice" class="notice notice-warning is-dismissible">
                <p>
                    <?php echo $message; ?>
                </p>
            </div>
            <?php
        //}
    }
}
add_action( 'admin_notices', 'sbwhea_admin_notices' );

function sbwhea_remove_timeline_plugin_notice(){
    update_option( 'timeline_plugin_notice_dismissed', 1 );
    wp_die();
}
add_action( 'wp_ajax_remove_timeline_plugin_notice', 'sbwhea_remove_timeline_plugin_notice' );

function sbwhea_add_banner_css( $img_url ) {
    if ( !wp_style_is( 'bespoke-customizer-styles' ) ) {
        wp_enqueue_style( 'bespoke-customizer-styles', get_theme_file_uri( 'inc/customizer/bespoke-customizer-styles.css' ) );
    }

    $banner_url_css = '#page-banner{
        background-image: url( "'. $img_url .'" );
    }';

    wp_add_inline_style( 'bespoke-customizer-styles', $banner_url_css );
}

function sbwhea_get_timer( $timer_date ) {
    //  Get current date time
    $current_time = time();
    //  Get specific date time
    $countdown_time = strtotime( $timer_date );
    $js_date_string = date('Y-m-d\TH:i:s', $countdown_time );
    //  Check if current date time is after specific and display 'Missed it'
    if ( $current_time > $countdown_time ) {
        $msg = __( 'Missed it!', 'sbw-wedding' );

    //  Check if current date time is same day as specific and display 'Todays the day!!'
    } elseif ( date('d-m-Y', $current_time ) == date('d-m-Y', $countdown_time ) ) {
        $msg = __( 'Today\'s the day!!', 'sbw-wedding' );

    //  Check if specific date is tomorrow and display 'Its tomorrow!'
    } elseif ( date('d-m-Y', $current_time ) == date('d-m-Y', ( $countdown_time - ( 24 * 60 * 60 ) ) ) ) { // 24 * 60 * 60 = a day in seconds
        $msg = __( 'It\'s tomorrow!', 'sbw-wedding' );

    //  Calculate the difference and display time left
    } else {
        $difference_in_time = ( $countdown_time - $current_time );
        //  work out days, minutes, hours, seconds
        $days = max( floor( $difference_in_time / ( 24 * 60 * 60 ) ), 0 );
        $difference_in_time -= $days * 24 * 60 * 60;
        $hours = max( floor( $difference_in_time / ( 60 * 60 ) ), 0 );
        $difference_in_time -= $hours * 60 * 60;
        $minutes = max( floor( $difference_in_time / 60 ), 0 );
        $seconds = max( $difference_in_time - ( $minutes * 60 ), 0 );
        $msg = sprintf('<div><span class="number">%d</span>days</div><div><span class="number">%d</span>hours</div><div><span class="number">%d</span>minutes</div>',
            $days,
            $hours,
            $minutes/*,
            $seconds*/ );

        wp_register_script( 'timer-js', get_template_directory_uri() .'/assets/js/timer.js' );

        $timer_data = array(
            'jsDateString' => $js_date_string
        );

        wp_localize_script( 'timer-js', 'timer_data', $timer_data );
        wp_enqueue_script( 'timer-js' );

        //  Add JS script to count down
        ?>


        <div class="countdown-timer-wrap">
            <div id="countdown_timer_value"><?php echo $msg; ?></div>
        </div>
        <?php
    }
}
