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


// Output inline CSS based on Customizer setting

// Breadcrumb - Enable Breadcrumb.
function electrical_services_customizer_css_inline() {
	$electrical_services_custom_css = '';
	$electrical_services_enable_breadcrumb = get_theme_mod( 'electrical_services_enable_breadcrumb', true );

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

	if ( ! empty( $electrical_services_custom_css ) ) {
		wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
	}
}
add_action( 'wp_enqueue_scripts', 'electrical_services_customizer_css_inline' );

//Global Color
function electrical_services_customize_css() {
	$electrical_services_primary_color = get_theme_mod( 'electrical_services_primary_color', '#FF1921' );
	$electrical_services_custom_css = ":root { --primary-color: {$electrical_services_primary_color}; }";

    $electrical_services_secondary_color = get_theme_mod( 'electrical_services_secondary_color', '#2C2C2C' );
	$electrical_services_custom_css = ":root { --secondary-color: {$electrical_services_secondary_color}; }";

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_customize_css' );

//Google Fonts
function electrical_services_enqueue_selected_fonts() {
    $electrical_services_fonts_url = electrical_services_get_fonts_url();
    if (!empty($electrical_services_fonts_url)) {
        wp_enqueue_style('electrical-services-google-fonts', $electrical_services_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'electrical_services_enqueue_selected_fonts');

//Set Width
function electrical_services_layout_customizer_css() {
	$electrical_services_margin = get_theme_mod( 'electrical_services_layout_width_margin', 50 );
	$electrical_services_custom_css = "body.site-boxed--layout #page { margin: 0 {$electrical_services_margin}px; }";

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_layout_customizer_css', 20 );

//Blog Content Alignment
function electrical_services_blog_layout_customizer_css() {
	// Retrieve the blog layout option
	$electrical_services_layout_option = get_theme_mod( 'electrical_services_blog_layout_option_setting', 'Left' );

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

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

	// Attach CSS to theme style
	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_blog_layout_customizer_css' );

//Sidebar Width Setting
function electrical_services_sidebar_width_customizer_css() {
	$electrical_services_sidebar_width = get_theme_mod( 'electrical_services_sidebar_width', '30' );

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

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_sidebar_width_customizer_css' );


if ( ! function_exists( 'electrical_services_get_page_title' ) ) {
    function electrical_services_get_page_title() {
        $electrical_services_title = '';

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

        return apply_filters('electrical_services_page_title', $electrical_services_title);
    }
}

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

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

        // Apply filters and return
        return apply_filters('electrical_services_display_page_header', $electrical_services_return);
    }
}

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

//Page Title Options
function electrical_services_page_title_customizer_css() {
	$electrical_services_layout_option = get_theme_mod( 'electrical_services_page_header_layout', 'left' );
	$electrical_services_custom_css = '';

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

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_page_title_customizer_css' );

//Set Height
function electrical_services_pagetitle_height_css() {
	$electrical_services_height = get_theme_mod( 'electrical_services_pagetitle_height', 50 );

	$electrical_services_custom_css = "
		header.page-header {
			padding: {$electrical_services_height}px 0;
		}
	";

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_pagetitle_height_css' );

//Adjust Site Logo Width
function electrical_services_site_logo_width() {
	$electrical_services_logo_width = get_theme_mod( 'electrical_services_site_logo_width' );

	$electrical_services_custom_css = "
		.site-logo img {
			max-width: {$electrical_services_logo_width}px;
		}
	";

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_site_logo_width' );

//Menu Font Size
function electrical_services_menu_font_size_css() {
	$electrical_services_font_size = get_theme_mod( 'electrical_services_menu_font_size', 15 );

	$electrical_services_custom_css = "
		.main-navigation a {
			font-size: {$electrical_services_font_size}px;
		}
	";

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_menu_font_size_css' );

//Menu Font Weight
function electrical_services_menu_font_weight_css() {
	$electrical_services_font_weight = get_theme_mod( 'electrical_services_menu_font_weight', 500 );

	$electrical_services_custom_css = "
		.main-navigation a {
		    font-weight: {$electrical_services_font_weight};
		}
	";

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_menu_font_weight_css' );

//Menu text transform
function electrical_services_menu_text_transform_css() {
	$electrical_services_text_transform = get_theme_mod( 'electrical_services_menu_text_transform', 'capitalize' );

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

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_menu_text_transform_css' );

// Featured Image Dimension
function electrical_services_custom_featured_image_css() {
    $electrical_services_dimension = get_theme_mod('electrical_services_blog_post_featured_image_dimension', 'default');
    $electrical_services_width = get_theme_mod('electrical_services_blog_post_featured_image_custom_width', '');
    $electrical_services_height = get_theme_mod('electrical_services_blog_post_featured_image_custom_height', '');
    
    if ($electrical_services_dimension === 'custom' && $electrical_services_width && $electrical_services_height) {
        $electrical_services_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$electrical_services_width}px !important; height: {$electrical_services_height}px !important; }";
        wp_add_inline_style('electrical-services-style', $electrical_services_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'electrical_services_custom_featured_image_css');

// Featured Image Border Radius
function electrical_services_featured_image_border_radius_css() {
	$electrical_services_radius = get_theme_mod( 'electrical_services_featured_image_border_radius', 10 );

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

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_featured_image_border_radius_css' );

//Heading Font Size
function electrical_services_sidebar_widget_font_size_css() {
	$electrical_services_sidebar_widget_font_size = get_theme_mod( 'electrical_services_sidebar_widget_font_size', 24 );

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

	wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
}
add_action( 'wp_enqueue_scripts', 'electrical_services_sidebar_widget_font_size_css' );

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

    if ( ! $electrical_services_related_product_show_hide ) {
        $electrical_services_custom_css = '
            .related.products {
                display: none;
            }
        ';
        wp_add_inline_style( 'electrical-services-style', $electrical_services_custom_css );
    }
}
add_action( 'wp_enqueue_scripts', 'electrical_services_related_product_css' );

// Product Sale Badge Position
function electrical_services_product_sale_position_customizer_css() {
    $electrical_services_position = get_theme_mod('electrical_services_product_sale_position', 'left');
    $electrical_services_css = '.woocommerce ul.products li.product .onsale {';
    if ($electrical_services_position === 'left') {
        $electrical_services_css .= 'right: auto; left: 0px;';
    } else {
        $electrical_services_css .= 'left: auto; right: 0px;';
    }
    $electrical_services_css .= '}';
    wp_add_inline_style('electrical-services-style', $electrical_services_css);
}
add_action('wp_enqueue_scripts', 'electrical_services_product_sale_position_customizer_css');

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

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

// Footer Copyright Alignment
function electrical_services_footer_copyright_alignment_css() {
    $electrical_services_align = get_theme_mod('electrical_services_footer_bottom_align', 'center');
    $electrical_services_css = "
        .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
            justify-content: {$electrical_services_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('electrical-services-style', $electrical_services_css);
}
add_action('wp_enqueue_scripts', 'electrical_services_footer_copyright_alignment_css');

// Footer Copyright Font Size
function electrical_services_copyright_font_size_css() {
    $electrical_services_font_size = get_theme_mod('electrical_services_copyright_font_size', 16);
    $electrical_services_css = ".site-footer-bottom .site-info span {
        font-size: {$electrical_services_font_size}px;
    }";
    wp_add_inline_style('electrical-services-style', $electrical_services_css);
}
add_action('wp_enqueue_scripts', 'electrical_services_copyright_font_size_css');

// Preloader Background Color
function electrical_services_preloader_background_colors_css() {
    $electrical_services_color = get_theme_mod('electrical_services_preloader_background_color_setting', '');
    if (!empty($electrical_services_color)) {
        $electrical_services_css = "#loader {
            background-color: {$electrical_services_color};
        }";
        wp_add_inline_style('electrical-services-style', $electrical_services_css);
    }
}
add_action('wp_enqueue_scripts', 'electrical_services_preloader_background_colors_css');

// Preloader Background Image
function electrical_services_preloader_background_image_css() {
    $electrical_services_image_url = get_theme_mod('electrical_services_preloader_background_image_setting', '');
    if (!empty($electrical_services_image_url)) {
        $electrical_services_css = "#loader {
            background-image: url('{$electrical_services_image_url}');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }";
        wp_add_inline_style('electrical-services-style', $electrical_services_css);
    }
}
add_action('wp_enqueue_scripts', 'electrical_services_preloader_background_image_css');

// Footer Heading Alignment
function electrical_services_footer_heading_alignment_css() {
    $electrical_services_align = get_theme_mod('electrical_services_footer_header_align', 'left');
    $electrical_services_css = "
        .site-footer h4,
        footer#colophon h2.wp-block-heading,
        footer#colophon .widgettitle,
        footer#colophon .widget-title {
            text-align: {$electrical_services_align};
        }
    ";
    wp_add_inline_style('electrical-services-style', $electrical_services_css);
}
add_action('wp_enqueue_scripts', 'electrical_services_footer_heading_alignment_css');

// Topbar Padding Dynamic CSS
function electrical_services_topbar_padding_css(){
	$padding = get_theme_mod('electrical_services_topbar_padding');
	$custom_css = ".top-header-part{
		padding-top: {$padding}px !important;
		padding-bottom: {$padding}px !important;
	}";
	wp_add_inline_style('electrical-services-style',$custom_css);

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

// Banner Button Color
function electrical_services_banner_button_color_css() {
    $btn_color = get_theme_mod('electrical_services_banner_btn_color');
    $btn_bg_color = get_theme_mod('electrical_services_banner_btn_bg_color');
    $btn_border_color = get_theme_mod('electrical_services_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('electrical-services-style', $custom_css);
}
add_action('wp_enqueue_scripts','electrical_services_banner_button_color_css');
