<?php
/**
 * @author      CodeGearThemes
 * @category    WordPress
 * @package     Acoustics
 * @version     1.0.0
 *
 */
if ( ! function_exists( 'acoustics_home_sections' ) ) {
  function acoustics_home_sections(){
		$acoustics_sections = array(
			'hero' => 'hero',
			'featured' => 'featured-images',
			'newarrival' => 'newarrival',
			'product_category' => 'collection',
			'bestseller' => 'bestseller',
			'product_category_grid' => 'collection-grid',
			'values' => 'proposition',
		);
		$enabled_section = array();
		foreach ( $acoustics_sections as $section => $block ):
			if( get_theme_mod( 'acoustics_'.$section.'_section_enable' , false ) ){
				$enabled_section[] = array(
				    'section' => $section,
					'parts'   => $block
				);
			}
		endforeach;
		return $enabled_section;
	}
}

if( ! function_exists( 'acoustics_footer_social' ) ):
	function acoustics_footer_social() {
		$acoustics_twitter_link = get_theme_mod( 'acoustics_twitter_url', '' );
	    $acoustics_facebook_link = get_theme_mod( 'acoustics_facebook_url', '' );
	    $acoustics_linkedin_link = get_theme_mod( 'acoustics_linkedin_url', '' );
	    $acoustics_instagram_link = get_theme_mod( 'acoustics_instagram_url', '' );
	    $acoustics_pinterest_link = get_theme_mod( 'acoustics_pinterest_url', '' );
	    $acoustics_youtube_link = get_theme_mod( 'acoustics_youtube_url', '' );
	    $classes = 'round-icon';
		?>
	   <ul class="social-icons clearfix <?php echo esc_attr( $classes ); ?>">
		   <?php
		    if( ! empty( $acoustics_twitter_link ) ): ?>
		        <li class="text-center"><a href="<?php echo esc_url( $acoustics_twitter_link ); ?>" target="_blank"> <span class="fa fa-twitter"></span></a></li>
				<?php
		    endif;
			if( ! empty( $acoustics_facebook_link ) ): ?>
		        <li class="text-center"><a href="<?php echo esc_url( $acoustics_facebook_link ); ?>" target="_blank"> <span class="fa fa-facebook"></span></a></li>
				<?php
		    endif;
			if( ! empty( $acoustics_linkedin_link ) ): ?>
		        <li class="text-center"><a href="<?php echo esc_url( $acoustics_linkedin_link ); ?>" target="_blank"> <span class="fa fa-linkedin"></span></a></li>
				<?php
		    endif;
			if( ! empty( $acoustics_instagram_link ) ): ?>
		        <li class="text-center"><a href="<?php echo esc_url( $acoustics_instagram_link ); ?>" target="_blank"> <span class="fa fa-instagram"></span></a></li>
				<?php
		    endif;
			if( ! empty( $acoustics_pinterest_link ) ): ?>
		       <li class="text-center"><a href="<?php echo esc_url( $acoustics_pinterest_link ); ?>" target="_blank"> <span class="fa fa-pinterest"></span></a></li>
			   <?php
		    endif;
			if( ! empty( $acoustics_youtube_link ) ): ?>
		        <li class="text-center"><a href="<?php echo esc_url( $acoustics_youtube_link ); ?>" target="_blank"> <span class="fa fa-youtube"></span></a></li>
				<?php
		    endif;
			?>
		</ul>
	<?php
}

endif;

/**
 * Google Fonts URL
 */
function acoustics_google_fonts_url() {
	$fonts_url 	= '';
	$subsets 	= 'latin';

	$defaults = json_encode(
		array(
			'font' 			=> 'System default',
			'regularweight' => '400',
			'category' 		=> 'sans-serif'
		)
	);

	//Get and decode options
	$body_font		= get_theme_mod( 'acoustics_base_font', $defaults );
	$menu_font		= get_theme_mod( 'acoustics_menu_font', $defaults );
	$headings_font 	= get_theme_mod( 'acoustics_heading_font', $defaults );

	$body_font 		= json_decode( $body_font, true );
	$menu_font 		= json_decode( $menu_font, true );
	$headings_font 	= json_decode( $headings_font, true );

	if ( 'System default' === $body_font['font'] && 'System default' === $menu_font['font'] && 'System default' === $headings_font['font'] ) {
		return; //return early if defaults are active
	}

	/**
	 * Font weight loader.
	 *
	 * @since 0.0.1
	 */
	$body_font_weight    = str_replace(
		array( 'regular', 'italic' ),
		array( '400', '' ),
		$body_font['regularweight']
	);

	$menu_font_weight    = str_replace(
		array( 'regular', 'italic' ),
		array( '400', '' ),
		$menu_font['regularweight']
	);

	$heading_font_weight = str_replace(
		array( 'regular', 'italic' ),
		array( '400', '' ),
		$headings_font['regularweight']
	);

	$font_families = array(
		$headings_font['font'] . ':wght@' . $heading_font_weight,
		$menu_font['font'] . ':wght@' . $menu_font_weight,
		$body_font['font'] . ':wght@' . $body_font_weight
	);

	$fonts_url = add_query_arg( array(
		'family' => implode( '&family=', $font_families ),
		'display' => 'swap',
	), 'https://fonts.googleapis.com/css2' );

	// Load google fonts locally
	$acoustics_googlefont_load_locally = get_theme_mod('acoustics_load_google_fonts_locally', 0);
	if( $acoustics_googlefont_load_locally ) {
		require_once get_theme_file_path( 'vendor/wptt-webfont-loader/wptt-webfont-loader.php' ); // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound

		add_filter( 'wptt_get_local_fonts_base_path', function( $path ) {
			$path = wp_upload_dir();
			return $path['basedir'];
		} );

		add_filter( 'wptt_get_local_fonts_base_url', function( $url ) {
			$path = wp_upload_dir();
			return $path['baseurl'];
		} );

		add_filter( 'wptt_get_local_fonts_subfolder_name', function( $subfolder_name ) {
			return 'wp-fonts';
		} );

		return wptt_get_webfont_url( esc_url_raw( $fonts_url ) );
	}

	return esc_url_raw( $fonts_url );
}
