<?php
if ( ! function_exists( 'loza_require_file' ) ) {
    /**
     * Check and require file if file exists
     */
    function loza_require_file( $path ) {
        if ( file_exists($path) ) {
            require $path;
        }
    }
}

/**
 * Setup theme
 */
if ( ! function_exists( 'loza_setup_theme' ) ) {
    function loza_setup_theme() {
        if ( ! isset( $content_width ) ) { $content_width = 1296; }

        /**
         * Theme text domain
         */
        load_theme_textdomain( 'loza', get_template_directory() . '/languages' );

    	/**
         * Theme support
         */
        add_theme_support( 'title-tag' );
        add_theme_support( 'woocommerce' );
        add_theme_support( 'wc-product-gallery-zoom' );
        add_theme_support( 'wc-product-gallery-lightbox' );
        add_theme_support( 'wc-product-gallery-slider' );
    	add_theme_support( 'automatic-feed-links' );
    	add_theme_support( 'post-thumbnails' );
        add_theme_support( 'responsive-embeds' );
        add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
        add_theme_support( 'custom-background', array( 'default-color' => 'f5f5f5' ) );
        add_theme_support( 'custom-logo', array(
            'height'      => 80,
            'width'       => 350,
            'flex-height' => true,
            'flex-width'  => true
        ) );

        /**
         * Theme navigation menus
         */
        register_nav_menus(
            array(
                'primary' => __( 'Primary Menu', 'loza' )
            )
        );
    }
}

add_action( 'after_setup_theme', 'loza_setup_theme' );

// Disables the block editor from managing widgets. renamed from wp_use_widgets_block_editor
add_filter( 'use_widgets_block_editor', '__return_false' );

if ( ! function_exists( 'loza_has_social_links' ) ) {
    /**
     * Check social links
     */
    function loza_has_social_links() {
        $facebook   = get_theme_mod( 'loza_facebook_url' );
        $twitter    = get_theme_mod( 'loza_twitter_url' );
        $instagram  = get_theme_mod( 'loza_instagram_url' );
        $pinterest  = get_theme_mod( 'loza_pinterest_url' );
        $youtube    = get_theme_mod( 'loza_youtube_url' );
        $tiktok     = get_theme_mod( 'loza_tiktok_url' );
        $linkedin   = get_theme_mod( 'loza_linkedin_url' );
        $soundcloud = get_theme_mod( 'loza_soundcloud_url' );
        $vimeo      = get_theme_mod( 'loza_vimeo_url' );
        
        if ( $facebook || $twitter || $instagram || $pinterest || $youtube || $soundcloud || $vimeo || $tiktok || $linkedin ) {
            return true;
        }
        return false;
    }
}

// Add specific CSS class by filter.

add_filter( 'body_class', function( $classes ) {
	if ( is_active_sidebar('sidebar') && !is_page() ) {
	   return array_merge( $classes, array( 'has-sidebar' ) );
	}
    return $classes;
} );

if ( ! function_exists( 'loza_enqueue_scripts' ) ) {
    /**
     *  Enqueue Styles / Scripts
     */
    function loza_enqueue_scripts() {
        $fonts_url  = '';
        $Lora       = _x( 'on', 'Lora font: on or off', 'loza' );
        $Jost = _x( 'on', 'Jost: on or off', 'loza' );
    
        if ( 'off' !== $Jost || 'off' !== $Lora ) {
            $font_families = array();
            if ( 'off' !== $Lora ) {
                $font_families[] = 'Lora:400,400i';
            }

            if ( 'off' !== $Jost ) {
                $font_families[] = 'Jost:400,400i,700,700i';
            }

            $query_args = array(
                'family' => urlencode( implode( '|', $font_families ) ),
                'subset' => urlencode( 'latin,latin-ext' ),
                'display' => 'swap'
            );

            $fonts_url = add_query_arg( $query_args, '//fonts.googleapis.com/css' );
        }

        wp_enqueue_style( 'loza-google-fonts', esc_url_raw( $fonts_url ), array(), null );

        // CSS
        wp_enqueue_style( 'select2', get_template_directory_uri() . '/libs/select2/css/select2.min.css' );
        wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/libs/font-awesome/css/all.min.css' );
        wp_enqueue_style( 'loza-style', get_template_directory_uri() . '/style.css', '', rand(0,9999) );
        wp_enqueue_style( 'loza-theme', get_template_directory_uri() . '/assets/css/theme.css', '', rand(0,9999) );
    
        // JS
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script( 'select2', get_template_directory_uri() . '/libs/select2/js/select2.min.js' );
        wp_enqueue_script( 'loza-scripts', get_template_directory_uri() . '/assets/js/scripts.js', array(), false, rand(0,9999) );

        if ( is_singular() && get_option( 'thread_comments' ) ) {
            wp_enqueue_script('comment-reply');
        }
    }
}

add_action( 'wp_enqueue_scripts', 'loza_enqueue_scripts' );

function loza_customizer_control_scripts() {
    wp_enqueue_script( 'loza-customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-controls' ), time() );
    wp_enqueue_style( 'loza-customizer', get_template_directory_uri() . '/assets/css/customizer.css' );
}

add_action( 'customize_controls_enqueue_scripts', 'loza_customizer_control_scripts', 99 );

if ( ! function_exists( 'loza_widgets_init' ) && function_exists( 'register_sidebar' ) ) {
    /**
     * Register Sidebars
     */
    function loza_widgets_init() {
        register_sidebar(array(
    		'name'            => __( 'Sidebar', 'loza' ),
    		'id'              => 'sidebar',
    		'before_widget'   => '<div id="%1$s" class="widget loza-box %2$s">',
    		'after_widget'    => '</div>',
    		'before_title'    => '<h2 class="widget-title">',
    		'after_title'     => '</h2>'
    	));
        register_sidebar(array(
    		'name'            => __( 'Instagram Footer', 'loza' ),
    		'id'              => 'footer-sidebar',
    		'before_widget'   => '<div id="%1$s" class="widget %2$s">',
    		'after_widget'    => '</div>',
    		'before_title'    => '<h2 class="widget-title"><span>',
    		'after_title'     => '</span></h2>',
            'description'     => __( 'Use the "Instagram" widget here. IMPORTANT: For best result set number of photos to 6.', 'loza' ),
    	));
    }
}

add_action( 'widgets_init', 'loza_widgets_init' );

if ( ! function_exists( 'loza_custom_comment' ) ) {
    /**
     * Custom comment html
     */
    function loza_custom_comment($comment, $args, $depth) {
    	extract($args, EXTR_SKIP);
    	if ( 'div' == $args['style'] ) {
    		$tag = 'div';
    		$add_below = 'comment';
    	} else {
    		$tag = 'li';
    		$add_below = 'div-comment';
    	} ?>
    	<<?php echo esc_attr($tag); ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>">
    	<?php if ( 'div' != $args['style'] ) : ?>
    	<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
    	<?php endif; ?>
    		<div class="comment-author">
    		<?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
    		</div>
    		<div class="comment-content">
    			<div class="comment-content-header">
                    <?php printf( __( '<h4 class="author-name">%s</h4>', 'loza' ), get_comment_author_link() ); ?>
        			<span class="date-comment">
        				<a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>">
        				<?php printf( __('%1$s at %2$s', 'loza'), get_comment_date(),  get_comment_time() ); ?></a>
        			</span>
        			<div class="reply">
        				<?php edit_comment_link( esc_html__( 'Edit', 'loza' ), '  ', '' );?>
        				<?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
        			</div>
                </div>
    			<?php if ( $comment->comment_approved == '0' ) : ?>
    				<em class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'loza' ); ?></em>
    				<br />
    			<?php endif; ?>
    			<div class="comment-text"><?php comment_text(); ?></div>
    		</div>	
    	<?php if ( 'div' != $args['style'] ) : ?>
    	</div>
    	<?php endif; ?>
    <?php
    }
}

if ( ! function_exists( 'loza_has_site_social_media_links' ) ) {
    /**
     * Check social media links
     */
    function loza_has_site_social_media_links() { 
        $facebook   = get_theme_mod('loza_facebook');
        $twitter    = get_theme_mod('loza_twitter');
        $instagram  = get_theme_mod('loza_instagram');
        $pinterest  = get_theme_mod('loza_pinterest');
        $bloglovin  = get_theme_mod('loza_bloglovin');
        $tumblr     = get_theme_mod('loza_tumblr');
        $youtube    = get_theme_mod('loza_youtube');
        $dribbble   = get_theme_mod('loza_dribbble');
        $soundcloud = get_theme_mod('loza_soundcloud');
        $vimeo      = get_theme_mod('loza_vimeo');
        $linkedin   = get_theme_mod('loza_linkedin');
        
        if ( !$facebook && !$twitter && !$instagram && !$pinterest && !$bloglovin && !$tumblr && !$youtube && !$dribbble && !$soundcloud && !$vimeo && !$linkedin ) {
            return false;
        }

        return true;
    }
}

if ( ! function_exists( 'loza_site_pagination' ) ) {
    /**
     * Site navigation
     */
    function loza_site_pagination() {
        if ( get_the_posts_pagination() ) { ?>
        <div class="loza-pagination"><?php
            $args = array(
                'prev_text' => '<span class="fa fa-angle-left"></span>',
                'next_text' => '<span class="fa fa-angle-right"></span>'
            );
            the_posts_pagination($args);
        ?>
        </div>
        <?php
        }
    }
}

/**
 * Theme widgets
 */
loza_require_file( get_template_directory() . '/core/widgets/about-me.php' );
loza_require_file( get_template_directory() . '/core/widgets/latest-posts.php' );
loza_require_file( get_template_directory() . '/core/widgets/social-media-links.php' );

/**
 * Require theme hooks
 */
loza_require_file( get_template_directory() . '/core/theme-hooks.php' );

/**
 * Theme customizer
 */
loza_require_file( get_template_directory() . '/core/theme-customizer.php' );
