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


// Output inline CSS based on Customizer setting

// Breadcrumb - Enable Breadcrumb.
function handmade_craft_customizer_css_inline() {
	$handmade_craft_custom_css = '';
	$handmade_craft_enable_breadcrumb = get_theme_mod( 'handmade_craft_enable_breadcrumb', true );

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

	if ( ! empty( $handmade_craft_custom_css ) ) {
		wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
	}
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_customizer_css_inline' );

//Global Color
function handmade_craft_customize_css() {
	$handmade_craft_primary_color = get_theme_mod( 'handmade_craft_primary_color', '#6B7050' );
    $handmade_craft_secondary_color = get_theme_mod( 'handmade_craft_secondary_color', '#657150' );
	$handmade_craft_custom_css = ":root { --primary-color: {$handmade_craft_primary_color}; }";
    $handmade_craft_custom_css = ":root { --secondary_color: {$handmade_craft_secondary_color}; }";

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_customize_css' );

//Google Fonts
function handmade_craft_enqueue_selected_fonts() {
    $handmade_craft_fonts_url = handmade_craft_get_fonts_url();
    if (!empty($handmade_craft_fonts_url)) {
        wp_enqueue_style('handmade-craft-google-fonts', $handmade_craft_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'handmade_craft_enqueue_selected_fonts');

//Set Width
function handmade_craft_layout_customizer_css() {
	$handmade_craft_margin = get_theme_mod( 'handmade_craft_layout_width_margin', 50 );
	$handmade_craft_custom_css = "body.site-boxed--layout #page { margin: 0 {$handmade_craft_margin}px; }";

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_layout_customizer_css', 20 );

//Blog Content Alignment
function handmade_craft_blog_layout_customizer_css() {
	// Retrieve the blog layout option
	$handmade_craft_layout_option = get_theme_mod( 'handmade_craft_blog_layout_option_setting', 'Left' );

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

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

	// Attach CSS to theme style
	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_blog_layout_customizer_css' );

//Sidebar Width Setting
function handmade_craft_sidebar_width_customizer_css() {
	$handmade_craft_sidebar_width = get_theme_mod( 'handmade_craft_sidebar_width', '30' );

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

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_sidebar_width_customizer_css' );


if ( ! function_exists( 'handmade_craft_get_page_title' ) ) {
    function handmade_craft_get_page_title() {
        $handmade_craft_title = '';

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

        return apply_filters('handmade_craft_page_title', $handmade_craft_title);
    }
}

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

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

        // Apply filters and return
        return apply_filters('handmade_craft_display_page_header', $handmade_craft_return);
    }
}

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

//Page Title Options
function handmade_craft_page_title_customizer_css() {
	$handmade_craft_layout_option = get_theme_mod( 'handmade_craft_page_header_layout', 'left' );
	$handmade_craft_custom_css = '';

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

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_page_title_customizer_css' );

//Set Height
function handmade_craft_pagetitle_height_css() {
	$handmade_craft_height = get_theme_mod( 'handmade_craft_pagetitle_height', 50 );

	$handmade_craft_custom_css = "
		header.page-header {
			padding: {$handmade_craft_height}px 0;
		}
	";

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_pagetitle_height_css' );

//Adjust Site Logo Width
function handmade_craft_site_logo_width() {
	$handmade_craft_logo_width = get_theme_mod( 'handmade_craft_site_logo_width', 150 );

	$handmade_craft_custom_css = "
		.site-logo img {
			max-width: {$handmade_craft_logo_width}px;
		}
	";

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_site_logo_width' );

//Menu Font Size
function handmade_craft_menu_font_size_css() {
	$handmade_craft_font_size = get_theme_mod( 'handmade_craft_menu_font_size', 14 );

	$handmade_craft_custom_css = "
		.main-navigation a {
			font-size: {$handmade_craft_font_size}px;
		}
	";

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_menu_font_size_css' );

//Menu Font Weight
function handmade_craft_menu_font_weight_css() {
	$handmade_craft_font_weight = get_theme_mod( 'handmade_craft_menu_font_weight', 400 );

	$handmade_craft_custom_css = "
		.main-navigation a {
		    font-weight: {$handmade_craft_font_weight};
		}
	";

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_menu_font_weight_css' );

//Menu text transform
function handmade_craft_menu_text_transform_css() {
	$handmade_craft_text_transform = get_theme_mod( 'handmade_craft_menu_text_transform', 'uppercase' );

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

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_menu_text_transform_css' );

// Featured Image Dimension
function handmade_craft_custom_featured_image_css() {
    $handmade_craft_dimension = get_theme_mod('handmade_craft_blog_post_featured_image_dimension', 'default');
    $handmade_craft_width = get_theme_mod('handmade_craft_blog_post_featured_image_custom_width', '');
    $handmade_craft_height = get_theme_mod('handmade_craft_blog_post_featured_image_custom_height', '');
    
    if ($handmade_craft_dimension === 'custom' && $handmade_craft_width && $handmade_craft_height) {
        $handmade_craft_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$handmade_craft_width}px !important; height: {$handmade_craft_height}px !important; }";
        wp_add_inline_style('handmade-craft-style', $handmade_craft_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'handmade_craft_custom_featured_image_css');

// Featured Image Border Radius
function handmade_craft_featured_image_border_radius_css() {
	$handmade_craft_radius = get_theme_mod( 'handmade_craft_featured_image_border_radius', 10 );

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

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_featured_image_border_radius_css' );

//Heading Font Size
function handmade_craft_sidebar_widget_font_size_css() {
	$handmade_craft_sidebar_widget_font_size = get_theme_mod( 'handmade_craft_sidebar_widget_font_size', 24 );

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

	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_sidebar_widget_font_size_css' );

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

    if ( ! $handmade_craft_related_product_show_hide ) {
        $handmade_craft_custom_css = '
            .related.products {
                display: none;
            }
        ';
        wp_add_inline_style( 'handmade-craft-style', $handmade_craft_custom_css );
    }
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_related_product_css' );

// Product Sale Badge Position
function handmade_craft_product_sale_position_customizer_css() {
    $handmade_craft_position = get_theme_mod('handmade_craft_product_sale_position', 'left');
    $handmade_craft_css = '.woocommerce ul.products li.product .onsale {';
    if ($handmade_craft_position === 'left') {
        $handmade_craft_css .= 'right: auto; left: 0px;';
    } else {
        $handmade_craft_css .= 'left: auto; right: 0px;';
    }
    $handmade_craft_css .= '}';
    wp_add_inline_style('handmade-craft-style', $handmade_craft_css);
}
add_action('wp_enqueue_scripts', 'handmade_craft_product_sale_position_customizer_css');

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

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

// Footer Copyright Alignment
function handmade_craft_footer_copyright_alignment_css() {
    $handmade_craft_align = get_theme_mod('handmade_craft_footer_bottom_align', 'center');
    $handmade_craft_css = "
        .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
            justify-content: {$handmade_craft_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('handmade-craft-style', $handmade_craft_css);
}
add_action('wp_enqueue_scripts', 'handmade_craft_footer_copyright_alignment_css');

// Footer Copyright Font Size
function handmade_craft_copyright_font_size_css() {
    $handmade_craft_font_size = get_theme_mod('handmade_craft_copyright_font_size', 16);
    $handmade_craft_css = ".site-footer-bottom .site-info span {
        font-size: {$handmade_craft_font_size}px;
    }";
    wp_add_inline_style('handmade-craft-style', $handmade_craft_css);
}
add_action('wp_enqueue_scripts', 'handmade_craft_copyright_font_size_css');

// Preloader Background Color
function handmade_craft_preloader_background_colors_css() {
    $handmade_craft_color = get_theme_mod('handmade_craft_preloader_background_color_setting', '');
    if (!empty($handmade_craft_color)) {
        $handmade_craft_css = "#loader {
            background-color: {$handmade_craft_color};
        }";
        wp_add_inline_style('handmade-craft-style', $handmade_craft_css);
    }
}
add_action('wp_enqueue_scripts', 'handmade_craft_preloader_background_colors_css');

// Preloader Background Image
function handmade_craft_preloader_background_image_css() {
    $handmade_craft_image_url = get_theme_mod('handmade_craft_preloader_background_image_setting', '');
    if (!empty($handmade_craft_image_url)) {
        $handmade_craft_css = "#loader {
            background-image: url('{$handmade_craft_image_url}');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }";
        wp_add_inline_style('handmade-craft-style', $handmade_craft_css);
    }
}
add_action('wp_enqueue_scripts', 'handmade_craft_preloader_background_image_css');

// Footer Heading Alignment
function handmade_craft_footer_heading_alignment_css() {
    $handmade_craft_align = get_theme_mod('handmade_craft_footer_header_align', 'left');
    $handmade_craft_css = "
        .site-footer h4,
        footer#colophon h2.wp-block-heading,
        footer#colophon .widgettitle,
        footer#colophon .widget-title {
            text-align: {$handmade_craft_align};
        }
    ";
    wp_add_inline_style('handmade-craft-style', $handmade_craft_css);
}
add_action('wp_enqueue_scripts', 'handmade_craft_footer_heading_alignment_css');

//First Capital Letter
function handmade_craft_show_first_caps() {
	$handmade_craft_first_caps = get_theme_mod('handmade_craft_show_first_caps', false);
	$handmade_craft_css = '';
	if ( $handmade_craft_first_caps ) {
	$handmade_craft_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
		    font-size: 55px;
			font-weight: 600;
			margin-right: 6px;
			line-height: 1;
			display: inline-block;
			vertical-align: baseline;
		}';
	} else {
		$handmade_craft_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
			display: none;
		}';
	}
	wp_add_inline_style( 'handmade-craft-style', $handmade_craft_css );
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_show_first_caps' );

// Topbar Padding Dynamic CSS
function handmade_craft_topbar_padding_css(){
	$padding = get_theme_mod('handmade_craft_topbar_padding');
	$custom_css = ".top-header-part{
		padding-top: {$padding}px !important;
		padding-bottom: {$padding}px !important;
	}";
	wp_add_inline_style('handmade-craft-style',$custom_css);

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

// Banner Button Color
function handmade_craft_banner_button_color_css() {
    $btn_color = get_theme_mod('handmade_craft_banner_btn_color');
	$btn_bg_color = get_theme_mod('handmade_craft_banner_btn_bg_color');

	$custom_css = '';
    if ( !empty($btn_color) ) {
		$custom_css .= "
		.banner-single .banner-btn a.asterthemes-button{
			color: {$btn_color};
		}";
	}

	if ( !empty($btn_bg_color) ) {
		$custom_css .= "
		.banner-single .banner-btn a.asterthemes-button{
			background-color: {$btn_bg_color};
		}";
	}

	wp_add_inline_style('handmade-craft-style', $custom_css);
}
add_action('wp_enqueue_scripts','handmade_craft_banner_button_color_css');