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


// Output inline CSS based on Customizer setting

// Breadcrumb - Enable Breadcrumb.
function property_dealer_customizer_css_inline() {
	$property_dealer_custom_css = '';
	$property_dealer_enable_breadcrumb = get_theme_mod( 'property_dealer_enable_breadcrumb', true );

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

	if ( ! empty( $property_dealer_custom_css ) ) {
		wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
	}
}
add_action( 'wp_enqueue_scripts', 'property_dealer_customizer_css_inline' );

//Global Color
function property_dealer_customize_css() {
	$property_dealer_primary_color = get_theme_mod( 'property_dealer_primary_color', '#C7805F' );
	$property_dealer_custom_css = ":root { --primary-color: {$property_dealer_primary_color}; }";

	$property_dealer_secondary_color = get_theme_mod( 'property_dealer_secondary_color', '#BD8660' );
	$property_dealer_custom_css = ":root { --secondary-color: {$property_dealer_secondary_color}; }";

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_customize_css' );

//Google Fonts
function property_dealer_enqueue_selected_fonts() {
    $property_dealer_fonts_url = property_dealer_get_fonts_url();
    if (!empty($property_dealer_fonts_url)) {
        wp_enqueue_style('property-dealer-google-fonts', $property_dealer_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'property_dealer_enqueue_selected_fonts');

//Set Width
function property_dealer_layout_customizer_css() {
	$property_dealer_margin = get_theme_mod( 'property_dealer_layout_width_margin', 50 );
	$property_dealer_custom_css = "body.site-boxed--layout #page { margin: 0 {$property_dealer_margin}px; }";

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_layout_customizer_css', 20 );

//Blog Content Alignment
function property_dealer_blog_layout_customizer_css() {
	// Retrieve the blog layout option
	$property_dealer_layout_option = get_theme_mod( 'property_dealer_blog_layout_option_setting', 'Left' );

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

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

	// Attach CSS to theme style
	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_blog_layout_customizer_css' );

//Sidebar Width Setting
function property_dealer_sidebar_width_customizer_css() {
	$property_dealer_sidebar_width = get_theme_mod( 'property_dealer_sidebar_width', '30' );

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

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_sidebar_width_customizer_css' );


if ( ! function_exists( 'property_dealer_get_page_title' ) ) {
    function property_dealer_get_page_title() {
        $property_dealer_title = '';

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

        return apply_filters('property_dealer_page_title', $property_dealer_title);
    }
}

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

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

        // Apply filters and return
        return apply_filters('property_dealer_display_page_header', $property_dealer_return);
    }
}

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

//Page Title Options
function property_dealer_page_title_customizer_css() {
	$property_dealer_layout_option = get_theme_mod( 'property_dealer_page_header_layout', 'left' );
	$property_dealer_custom_css = '';

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

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_page_title_customizer_css' );

//Set Height
function property_dealer_pagetitle_height_css() {
	$property_dealer_height = get_theme_mod( 'property_dealer_pagetitle_height', 50 );

	$property_dealer_custom_css = "
		header.page-header {
			padding: {$property_dealer_height}px 0;
		}
	";

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_pagetitle_height_css' );

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

	$property_dealer_custom_css = "
		.site-logo img {
			max-width: {$property_dealer_logo_width}px;
		}
	";

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_site_logo_width' );

//Menu Font Size
function property_dealer_menu_font_size_css() {
	$property_dealer_font_size = get_theme_mod( 'property_dealer_menu_font_size', 16 );

	$property_dealer_custom_css = "
		.main-navigation a {
			font-size: {$property_dealer_font_size}px;
		}
	";

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_menu_font_size_css' );

//Menu Font Weight
function property_dealer_menu_font_weight_css() {
	$property_dealer_font_weight = get_theme_mod( 'property_dealer_menu_font_weight', 500 );

	$property_dealer_custom_css = "
		.main-navigation a {
		    font-weight: {$property_dealer_font_weight};
		}
	";

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_menu_font_weight_css' );

//Menu text transform
function property_dealer_menu_text_transform_css() {
	$property_dealer_text_transform = get_theme_mod( 'property_dealer_menu_text_transform', 'capitalize' );

	$property_dealer_custom_css = "
		.main-navigation a {
			text-transform: {$property_dealer_text_transform};
		}
	";

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_menu_text_transform_css' );

// Featured Image Dimension
function property_dealer_custom_featured_image_css() {
    $property_dealer_dimension = get_theme_mod('property_dealer_blog_post_featured_image_dimension', 'default');
    $property_dealer_width = get_theme_mod('property_dealer_blog_post_featured_image_custom_width', '');
    $property_dealer_height = get_theme_mod('property_dealer_blog_post_featured_image_custom_height', '');
    
    if ($property_dealer_dimension === 'custom' && $property_dealer_width && $property_dealer_height) {
        $property_dealer_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$property_dealer_width}px !important; height: {$property_dealer_height}px !important; }";
        wp_add_inline_style('property-dealer-style', $property_dealer_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'property_dealer_custom_featured_image_css');

// Featured Image Border Radius
function property_dealer_featured_image_border_radius_css() {
	$property_dealer_radius = get_theme_mod( 'property_dealer_featured_image_border_radius', 10 );

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

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_featured_image_border_radius_css' );

//Heading Font Size
function property_dealer_sidebar_widget_font_size_css() {
	$property_dealer_sidebar_widget_font_size = get_theme_mod( 'property_dealer_sidebar_widget_font_size', 24 );

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

	wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_sidebar_widget_font_size_css' );

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

    if ( ! $property_dealer_related_product_show_hide ) {
        $property_dealer_custom_css = '
            .related.products {
                display: none;
            }
        ';
        wp_add_inline_style( 'property-dealer-style', $property_dealer_custom_css );
    }
}
add_action( 'wp_enqueue_scripts', 'property_dealer_related_product_css' );

// Product Sale Badge Position
function property_dealer_product_sale_position_customizer_css() {
    $property_dealer_position = get_theme_mod('property_dealer_product_sale_position', 'left');
    $property_dealer_css = '.woocommerce ul.products li.product .onsale {';
    if ($property_dealer_position === 'left') {
        $property_dealer_css .= 'right: auto; left: 0px;';
    } else {
        $property_dealer_css .= 'left: auto; right: 0px;';
    }
    $property_dealer_css .= '}';
    wp_add_inline_style('property-dealer-style', $property_dealer_css);
}
add_action('wp_enqueue_scripts', 'property_dealer_product_sale_position_customizer_css');

// Footer Copyright Alignment
function property_dealer_footer_copyright_alignment_css() {
    $property_dealer_align = get_theme_mod('property_dealer_footer_bottom_align', 'center');
    $property_dealer_css = "
        .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
            justify-content: {$property_dealer_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('property-dealer-style', $property_dealer_css);
}
add_action('wp_enqueue_scripts', 'property_dealer_footer_copyright_alignment_css');

// Footer Copyright Font Size
function property_dealer_copyright_font_size_css() {
    $property_dealer_font_size = get_theme_mod('property_dealer_copyright_font_size', 16);
    $property_dealer_css = ".site-footer-bottom .site-info span {
        font-size: {$property_dealer_font_size}px;
    }";
    wp_add_inline_style('property-dealer-style', $property_dealer_css);
}
add_action('wp_enqueue_scripts', 'property_dealer_copyright_font_size_css');

// Preloader Background Color
function property_dealer_preloader_background_colors_css() {
    $property_dealer_color = get_theme_mod('property_dealer_preloader_background_color_setting', '');
    if (!empty($property_dealer_color)) {
        $property_dealer_css = "#loader {
            background-color: {$property_dealer_color};
        }";
        wp_add_inline_style('property-dealer-style', $property_dealer_css);
    }
}
add_action('wp_enqueue_scripts', 'property_dealer_preloader_background_colors_css');

// Preloader Background Image
function property_dealer_preloader_background_image_css() {
    $property_dealer_image_url = get_theme_mod('property_dealer_preloader_background_image_setting', '');
    if (!empty($property_dealer_image_url)) {
        $property_dealer_css = "#loader {
            background-image: url('{$property_dealer_image_url}');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }";
        wp_add_inline_style('property-dealer-style', $property_dealer_css);
    }
}
add_action('wp_enqueue_scripts', 'property_dealer_preloader_background_image_css');

// Footer Heading Alignment
function property_dealer_footer_heading_alignment_css() {
    $property_dealer_align = get_theme_mod('property_dealer_footer_header_align', 'left');
    $property_dealer_css = "
        .site-footer h4,
        footer#colophon h2.wp-block-heading,
        footer#colophon .widgettitle,
        footer#colophon .widget-title {
            text-align: {$property_dealer_align};
        }
    ";
    wp_add_inline_style('property-dealer-style', $property_dealer_css);
}
add_action('wp_enqueue_scripts', 'property_dealer_footer_heading_alignment_css');

//First Capital Letter
function property_dealer_show_first_caps() {
	$property_dealer_first_caps = get_theme_mod('property_dealer_show_first_caps', false);
	$property_dealer_css = '';
	if ( $property_dealer_first_caps ) {
	$property_dealer_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 {
		$property_dealer_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
			display: none;
		}';
	}
	wp_add_inline_style( 'property-dealer-style', $property_dealer_css );
}
add_action( 'wp_enqueue_scripts', 'property_dealer_show_first_caps' );

// Topbar Padding Dynamic CSS
function property_dealer_topbar_padding_css(){
	$padding = get_theme_mod('property_dealer_topbar_padding');
	$custom_css = ".top-header-part{
		padding-top: {$padding}px !important;
		padding-bottom: {$padding}px !important;
	}";
	wp_add_inline_style('property-dealer-style',$custom_css);

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

// WooCommerce Product Border Radius
function property_dealer_woocommerce_border_radius_css() {
	$property_dealer_radius = get_theme_mod('property_dealer_woocommerce_product_border_radius', 3);
    $property_dealer_custom_css = "
		.woocommerce ul.products li.product {
			border-radius: {$property_dealer_radius}px;
			overflow: hidden;
		}
	";
    wp_add_inline_style('property-dealer-style', $property_dealer_custom_css);
}
add_action('wp_enqueue_scripts', 'property_dealer_woocommerce_border_radius_css');
