<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package design_studio
 */


// Output inline CSS based on Customizer setting

// Breadcrumb - Enable Breadcrumb.
function design_studio_customizer_css_inline() {
	$design_studio_custom_css = '';
	$design_studio_enable_breadcrumb = get_theme_mod( 'design_studio_enable_breadcrumb', true );

	if ( ! $design_studio_enable_breadcrumb ) {
		$design_studio_custom_css .= 'nav.woocommerce-breadcrumb { 
            display: none; 
        }';
	}

	if ( ! empty( $design_studio_custom_css ) ) {
		wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
	}
}
add_action( 'wp_enqueue_scripts', 'design_studio_customizer_css_inline' );

//Global Color
function design_studio_customize_css() {
	$design_studio_primary_color = get_theme_mod( 'design_studio_primary_color', '#29C684' );
	$design_studio_custom_css = ":root { --primary-color: {$design_studio_primary_color}; }";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_customize_css' );

//Google Fonts
function design_studio_enqueue_selected_fonts() {
    $design_studio_fonts_url = design_studio_get_fonts_url();
    if (!empty($design_studio_fonts_url)) {
        wp_enqueue_style('design-studio-google-fonts', $design_studio_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'design_studio_enqueue_selected_fonts');

//Set Width
function design_studio_layout_customizer_css() {
	$design_studio_margin = get_theme_mod( 'design_studio_layout_width_margin', 50 );
	$design_studio_custom_css = "body.site-boxed--layout #page { margin: 0 {$design_studio_margin}px; }";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_layout_customizer_css', 20 );

//Blog Content Alignment
function design_studio_blog_layout_customizer_css() {
	// Retrieve the blog layout option
	$design_studio_layout_option = get_theme_mod( 'design_studio_blog_layout_option_setting', 'Left' );

	// Determine text alignment based on option
	switch ( $design_studio_layout_option ) {
		case 'Right':
			$design_studio_text_align = 'right';
			break;
		case 'Default':
			$design_studio_text_align = 'center';
			break;
		case 'Left':
		default:
			$design_studio_text_align = 'left';
			break;
	}

	// Create custom CSS
	$design_studio_custom_css = ".mag-post-detail { text-align: {$design_studio_text_align}; }";

	// Attach CSS to theme style
	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_blog_layout_customizer_css' );

//Sidebar Width Setting
function design_studio_sidebar_width_customizer_css() {
	$design_studio_sidebar_width = get_theme_mod( 'design_studio_sidebar_width', '30' );

	$design_studio_custom_css = "
		.right-sidebar .asterthemes-wrapper .asterthemes-page {
			grid-template-columns: auto {$design_studio_sidebar_width}%;
		}
		.left-sidebar .asterthemes-wrapper .asterthemes-page {
			grid-template-columns: {$design_studio_sidebar_width}% auto;
		}
	";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_sidebar_width_customizer_css' );


if ( ! function_exists( 'design_studio_get_page_title' ) ) {
    function design_studio_get_page_title() {
        $design_studio_title = '';

        if (is_404()) {
            $design_studio_title = esc_html__('Page Not Found', 'design-studio');
        } elseif (is_search()) {
            $design_studio_title = esc_html__('Search Results for: ', 'design-studio') . esc_html(get_search_query());
        } elseif (is_home() && !is_front_page()) {
            $design_studio_title = esc_html__('Blogs', 'design-studio');
        } elseif (function_exists('is_shop') && is_shop()) {
            $design_studio_title = esc_html__('Shop', 'design-studio');
        } elseif (is_page()) {
            $design_studio_title = get_the_title();
        } elseif (is_single()) {
            $design_studio_title = get_the_title();
        } elseif (is_archive()) {
            $design_studio_title = get_the_archive_title();
        } else {
            $design_studio_title = get_the_archive_title();
        }

        return apply_filters('design_studio_page_title', $design_studio_title);
    }
}

if ( ! function_exists( 'design_studio_has_page_header' ) ) {
    function design_studio_has_page_header() {
        // Default to true (display header)
        $design_studio_return = true;

        // Custom conditions for disabling the header
        if ('hide-all-devices' === get_theme_mod('design_studio_page_header_visibility', 'all-devices')) {
            $design_studio_return = false;
        }

        // Apply filters and return
        return apply_filters('design_studio_display_page_header', $design_studio_return);
    }
}

if ( ! function_exists( 'design_studio_page_header_style' ) ) {
    function design_studio_page_header_style() {
        $design_studio_style = get_theme_mod('design_studio_page_header_style', 'default');
        return apply_filters('design_studio_page_header_style', $design_studio_style);
    }
}

//Page Title Options
function design_studio_page_title_customizer_css() {
	$design_studio_layout_option = get_theme_mod( 'design_studio_page_header_layout', 'left' );
	$design_studio_custom_css = '';

	if ( $design_studio_layout_option === 'flex' ) {
		$design_studio_custom_css .= '
			.asterthemes-wrapper.page-header-inner {
				display: flex;
				justify-content: space-between;
				align-items: center;
			}
		';
	} else {
		$design_studio_custom_css .= "
			.asterthemes-wrapper.page-header-inner {
				text-align: {$design_studio_layout_option};
			}
		";
	}

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_page_title_customizer_css' );

//Set Height
function design_studio_pagetitle_height_css() {
	$design_studio_height = get_theme_mod( 'design_studio_pagetitle_height', 50 );

	$design_studio_custom_css = "
		header.page-header {
			padding: {$design_studio_height}px 0;
		}
	";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_pagetitle_height_css' );

//Adjust Site Logo Width
function design_studio_site_logo_width() {
	$design_studio_logo_width = get_theme_mod( 'design_studio_site_logo_width' );

	$design_studio_custom_css = "
		.site-logo img {
			max-width: {$design_studio_logo_width}px;
		}
	";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_site_logo_width' );

//Menu Font Size
function design_studio_menu_font_size_css() {
	$design_studio_font_size = get_theme_mod( 'design_studio_menu_font_size', 15 );

	$design_studio_custom_css = "
		.main-navigation a {
			font-size: {$design_studio_font_size}px;
		}
	";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_menu_font_size_css' );

//Menu Font Weight
function design_studio_menu_font_weight_css() {
	$design_studio_font_weight = get_theme_mod( 'design_studio_menu_font_weight', 500 );

	$design_studio_custom_css = "
		.main-navigation a {
		    font-weight: {$design_studio_font_weight};
		}
	";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_menu_font_weight_css' );

//Menu text transform
function design_studio_menu_text_transform_css() {
	$design_studio_text_transform = get_theme_mod( 'design_studio_menu_text_transform', 'capitalize' );

	$design_studio_custom_css = "
		.main-navigation a {
			text-transform: {$design_studio_text_transform} !important;
		}
	";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_menu_text_transform_css' );

// Featured Image Dimension
function design_studio_custom_featured_image_css() {
    $design_studio_dimension = get_theme_mod('design_studio_blog_post_featured_image_dimension', 'default');
    $design_studio_width = get_theme_mod('design_studio_blog_post_featured_image_custom_width', '');
    $design_studio_height = get_theme_mod('design_studio_blog_post_featured_image_custom_height', '');
    
    if ($design_studio_dimension === 'custom' && $design_studio_width && $design_studio_height) {
        $design_studio_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$design_studio_width}px !important; height: {$design_studio_height}px !important; }";
        wp_add_inline_style('design-studio-style', $design_studio_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'design_studio_custom_featured_image_css');

// Featured Image Border Radius
function design_studio_featured_image_border_radius_css() {
	$design_studio_radius = get_theme_mod( 'design_studio_featured_image_border_radius', 10 );

	$design_studio_custom_css = "
		.mag-post-single img {
			border-radius: {$design_studio_radius}px;
		}
	";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_featured_image_border_radius_css' );

//Heading Font Size
function design_studio_sidebar_widget_font_size_css() {
	$design_studio_sidebar_widget_font_size = get_theme_mod( 'design_studio_sidebar_widget_font_size', 24 );

	$design_studio_custom_css = "
		h2.wp-block-heading,
		aside#secondary .widgettitle,
		aside#secondary .widget-title {
			font-size: {$design_studio_sidebar_widget_font_size}px;
		}
	";

	wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'design_studio_sidebar_widget_font_size_css' );

// Woocommerce Related Products Settings
function design_studio_related_product_css() {
    $design_studio_related_product_show_hide = get_theme_mod( 'design_studio_related_product_show_hide', true );

    if ( ! $design_studio_related_product_show_hide ) {
        $design_studio_custom_css = '
            .related.products {
                display: none;
            }
        ';
        wp_add_inline_style( 'design-studio-style', $design_studio_custom_css );
    }
}
add_action( 'wp_enqueue_scripts', 'design_studio_related_product_css' );

// Product Sale Badge Position
function design_studio_product_sale_position_customizer_css() {
    $design_studio_position = get_theme_mod('design_studio_product_sale_position', 'left');
    $design_studio_css = '.woocommerce ul.products li.product .onsale {';
    if ($design_studio_position === 'left') {
        $design_studio_css .= 'right: auto; left: 0px;';
    } else {
        $design_studio_css .= 'left: auto; right: 0px;';
    }
    $design_studio_css .= '}';
    wp_add_inline_style('design-studio-style', $design_studio_css);
}
add_action('wp_enqueue_scripts', 'design_studio_product_sale_position_customizer_css');

//Footer Social Icon Alignment
function design_studio_footer_icons_alignment_css() {
    $design_studio_footer_social_align = get_theme_mod( 'design_studio_footer_social_align', 'center' );   
    ?>
    <style type="text/css">
        .socialicons {
            text-align: <?php echo esc_attr( $design_studio_footer_social_align ); ?> 
        }

        /* Mobile Specific */
        @media screen and (max-width: 575px) {
            .socialicons {
                text-align: center;
            }
        }
    </style>
    <?php
}
add_action( 'wp_head', 'design_studio_footer_icons_alignment_css' );

// Footer Copyright Alignment
function design_studio_footer_copyright_alignment_css() {
    $design_studio_align = get_theme_mod('design_studio_footer_bottom_align', 'center');
    $design_studio_css = "
        .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
            justify-content: {$design_studio_align};
        }
        @media screen and (max-width: 575px) {
            .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
                justify-content: center;
                text-align: center;
            }
        }
    ";
    wp_add_inline_style('design-studio-style', $design_studio_css);
}
add_action('wp_enqueue_scripts', 'design_studio_footer_copyright_alignment_css');

// Footer Copyright Font Size
function design_studio_copyright_font_size_css() {
    $design_studio_font_size = get_theme_mod('design_studio_copyright_font_size', 16);
    $design_studio_css = ".site-footer-bottom .site-info span {
        font-size: {$design_studio_font_size}px;
    }";
    wp_add_inline_style('design-studio-style', $design_studio_css);
}
add_action('wp_enqueue_scripts', 'design_studio_copyright_font_size_css');

// Preloader Background Color
function design_studio_preloader_background_colors_css() {
    $design_studio_color = get_theme_mod('design_studio_preloader_background_color_setting', '');
    if (!empty($design_studio_color)) {
        $design_studio_css = "#loader {
            background-color: {$design_studio_color};
        }";
        wp_add_inline_style('design-studio-style', $design_studio_css);
    }
}
add_action('wp_enqueue_scripts', 'design_studio_preloader_background_colors_css');

// Preloader Background Image
function design_studio_preloader_background_image_css() {
    $design_studio_image_url = get_theme_mod('design_studio_preloader_background_image_setting', '');
    if (!empty($design_studio_image_url)) {
        $design_studio_css = "#loader {
            background-image: url('{$design_studio_image_url}');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }";
        wp_add_inline_style('design-studio-style', $design_studio_css);
    }
}
add_action('wp_enqueue_scripts', 'design_studio_preloader_background_image_css');

// Footer Heading Alignment
function design_studio_footer_heading_alignment_css() {
    $design_studio_align = get_theme_mod('design_studio_footer_header_align', 'left');
    $design_studio_css = "
        .site-footer h4,
        footer#colophon h2.wp-block-heading,
        footer#colophon .widgettitle,
        footer#colophon .widget-title {
            text-align: {$design_studio_align};
        }
    ";
    wp_add_inline_style('design-studio-style', $design_studio_css);
}
add_action('wp_enqueue_scripts', 'design_studio_footer_heading_alignment_css');

// Topbar Padding Dynamic CSS
function design_studio_topbar_padding_css(){
	$padding = get_theme_mod('design_studio_topbar_padding');
	$custom_css = ".top-header-part{
		padding-top: {$padding}px !important;
		padding-bottom: {$padding}px !important;
	}";
	wp_add_inline_style('design-studio-style',$custom_css);

}
add_action('wp_enqueue_scripts','design_studio_topbar_padding_css');

// Banner Button Color
function design_studio_banner_button_color_css() {
    $btn_color = get_theme_mod('design_studio_banner_btn_color');
    $btn_bg_color = get_theme_mod('design_studio_banner_btn_bg_color');
    $btn_border_color = get_theme_mod('design_studio_banner_btn_border_color');

    $custom_css = '';
    if ( !empty($btn_color) ) {
        $custom_css .= "
        .sale-banner__button,
        .sale-banner__button i {
            color: {$btn_color};
        }";
    }

    if ( !empty($btn_bg_color) ) {
        $custom_css .= "
        .sale-banner__button {
            background-color: {$btn_bg_color};
        }";
    }
     if ( !empty($btn_border_color) ) {
        $custom_css .= "
        .sale-banner__button {
            border: 1px solid {$btn_border_color};
        }";
    }

    wp_add_inline_style('design-studio-style', $custom_css);
}
add_action('wp_enqueue_scripts','design_studio_banner_button_color_css');
