<?php
/**
* Enqueue scripts and styles
*
* @package GridRead WordPress Theme
* @copyright Copyright (C) 2025 ThemesDNA
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
* @author ThemesDNA <themesdna@gmail.com>
*/

function gridread_scripts() {
    wp_enqueue_style('gridread-maincss', get_stylesheet_uri(), array(), null);
    wp_enqueue_style('fontawesome', get_template_directory_uri() . '/assets/css/all.min.css', array(), null );
    wp_enqueue_style('gridread-webfont', '//fonts.googleapis.com/css?family=Oswald:400,500,700|Poppins:400,400i,500,500i,700,700i|Domine:400,500,700&amp;display=swap', array(), null);

    $gridread_fitvids_active = false;
    if ( gridread_is_fitvids_active() ) {
        $gridread_fitvids_active = true;
    }
    if ( $gridread_fitvids_active ) {
        wp_enqueue_script('fitvids', get_template_directory_uri() .'/assets/js/jquery.fitvids.min.js', array( 'jquery' ), null, true);
    }

    $gridread_backtotop_active = false;
    if ( gridread_is_backtotop_active() ) {
        $gridread_backtotop_active = true;
    }

    $gridread_primary_menu_active = false;
    if ( gridread_is_primary_menu_active() ) {
        $gridread_primary_menu_active = true;
    }
    $gridread_secondary_menu_active = false;
    if ( gridread_is_secondary_menu_active() ) {
        $gridread_secondary_menu_active = true;
    }

    $gridread_sticky_sidebar_active = true;
    if ( is_singular() ) {
        if ( is_page_template( array( 'template-full-width-page.php', 'template-full-width-post.php' ) ) ) {
           $gridread_sticky_sidebar_active = false;
        }
    } else {
        $gridread_sticky_sidebar_active = false;
    }
    if ( $gridread_sticky_sidebar_active ) {
        wp_enqueue_script('ResizeSensor', get_template_directory_uri() .'/assets/js/ResizeSensor.min.js', array( 'jquery' ), null, true);
        wp_enqueue_script('theia-sticky-sidebar', get_template_directory_uri() .'/assets/js/theia-sticky-sidebar.min.js', array( 'jquery' ), null, true);
    }

    $gridread_slider_active = false;
    if ( gridread_is_slider_active() ) {
        if ( 'front-page' === gridread_get_option('slider_display') ) {
            if ( is_front_page() && is_home() && !is_paged() ) {
                $gridread_slider_active = true;
            }
        } elseif ( 'front-page-static' === gridread_get_option('slider_display') ) {
            if ( is_front_page() && is_singular() ) {
                $gridread_slider_active = true;
            }
        } elseif ( 'blog-posts-index' === gridread_get_option('slider_display') ) {
            if ( is_home() && !is_paged() ) {
                $gridread_slider_active = true;
            }
        } elseif ( 'everywhere' === gridread_get_option('slider_display') ) {
                $gridread_slider_active = true;
        } else {
            if ( is_front_page() && is_home() && !is_paged() ) {
                $gridread_slider_active = true;
            }
        }
    }
    if ( $gridread_slider_active ) {
        wp_enqueue_style('owl-carousel', get_template_directory_uri() . '/assets/css/owl.carousel.min.css', array(), null );
        wp_enqueue_script('owl-carousel', get_template_directory_uri() .'/assets/js/owl.carousel.min.js', array( 'jquery', 'imagesloaded' ), null, true);
    }

    $gridread_slider_autoplay = false;
    if ( gridread_get_option('slider_autoplay') ) {
        $gridread_slider_autoplay = true;
    }
    $gridread_slider_loop = false;
    if ( gridread_get_option('slider_loop') ) {
        $gridread_slider_loop = true;
    }
    $gridread_slider_autoplayhoverpause = false;
    if ( gridread_get_option('slider_autoplayhoverpause') ) {
        $gridread_slider_autoplayhoverpause = true;
    }
    $gridread_slider_autoplaytimeout = 1000;
    if ( gridread_get_option('slider_autoplaytimeout') ) {
        $gridread_slider_autoplaytimeout = gridread_get_option('slider_autoplaytimeout');
    }
    $gridread_slider_autoplayspeed = 200;
    if ( gridread_get_option('slider_autoplayspeed') ) {
        $gridread_slider_autoplayspeed = gridread_get_option('slider_autoplayspeed');
    }
    $gridread_show_dots_pagination = false;
    if ( gridread_get_option('show_dots_pagination') ) {
        $gridread_show_dots_pagination = true;
    }
    $gridread_show_next_prev_navigation = true;
    if ( gridread_get_option('hide_next_prev_navigation') ) {
        $gridread_show_next_prev_navigation = false;
    }
    $gridread_autoheight_active = true;
    if ( gridread_get_option('disable_autoheight') ) {
        $gridread_autoheight_active = false;
    }
    $gridread_slider_rtl = false;
    if ( is_rtl() ) {
        $gridread_slider_rtl = true;
    }

    wp_enqueue_script('gridread-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array(), null, true );
    wp_enqueue_script('gridread-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), null, true );
    wp_enqueue_script('gridread-customjs', get_template_directory_uri() .'/assets/js/custom.js', array( 'jquery', 'imagesloaded' ), null, true);

    wp_localize_script( 'gridread-customjs', 'gridread_ajax_object',
        array(
            'ajaxurl' => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
            'primary_menu_active' => $gridread_primary_menu_active,
            'secondary_menu_active' => $gridread_secondary_menu_active,
            'sticky_sidebar_active' => $gridread_sticky_sidebar_active,
            'slider_active' => $gridread_slider_active,
            'slider_autoplay' => $gridread_slider_autoplay,
            'slider_loop' => $gridread_slider_loop,
            'slider_autoplayhoverpause' => $gridread_slider_autoplayhoverpause,
            'slider_autoplaytimeout' => $gridread_slider_autoplaytimeout,
            'slider_autoplayspeed' => $gridread_slider_autoplayspeed,
            'show_dots_pagination' => $gridread_show_dots_pagination,
            'show_next_prev_navigation' => $gridread_show_next_prev_navigation,
            'autoheight_active' => $gridread_autoheight_active,
            'slider_rtl' => $gridread_slider_rtl,
            'fitvids_active' => $gridread_fitvids_active,
            'backtotop_active' => $gridread_backtotop_active,
        )
    );

    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'gridread_scripts' );

/**
 * Enqueue styles for the block-based editor.
 */
function gridread_block_editor_styles() {
    wp_enqueue_style( 'gridread-block-editor-style', get_template_directory_uri() . '/assets/css/editor-blocks.css', array(), null );
}
add_action( 'enqueue_block_editor_assets', 'gridread_block_editor_styles' );

/**
 * Enqueue customizer styles.
 */
function gridread_enqueue_customizer_styles() {
    wp_enqueue_style( 'gridread-customizer-styles', get_template_directory_uri() . '/inc/admin/css/customizer-style.css', array(), null );
    wp_enqueue_style('fontawesome', get_template_directory_uri() . '/assets/css/all.min.css', array(), null );
}
add_action( 'customize_controls_enqueue_scripts', 'gridread_enqueue_customizer_styles' );