<?php
// ------------------------------------------------------------------------------------------------
// Product Menues
// ------------------------------------------------------------------------------------------------

add_action('after_setup_theme', function (){
	register_nav_menus(array(
		'primary' => esc_html__('Primary', 'kadence'),
		'mobile'  => esc_html__('Mobile', 'kadence'),
		'secondary'  => esc_html__('Secondary', 'kadence'),
		'footer'  => esc_html__('Footer', 'kadence'),

		'mortgagehalo_primary'  => esc_html__('MortgageHalo Primary', 'kadence_child'),
		'realestatehalo_primary'  => esc_html__('RealEstateHalo Primary', 'kadence_child'),
		'contractorhalo_primary'  => esc_html__('ContractorHalo Primary', 'kadence_child'),

		'mortgagehalo_footer'  => esc_html__('MortgageHalo Footer', 'kadence_child'),
		'realestatehalo_footer'  => esc_html__('RealEstateHalo Footer', 'kadence_child'),
		'contractorhalo_footer'  => esc_html__('ContractorHalo Footer', 'kadence_child'),
	));
}, 20);

add_filter('wp_nav_menu_args', function ($args) {
	$halo_product = get_field('bw_halo_product');
	
	if (!is_page()) return $args;

	if ($halo_product === 'mortgagehalo') {
		if (isset($args['theme_location']) && 'primary' === $args['theme_location']) {
			$args['theme_location'] = 'mortgagehalo_primary';
		}
		if (isset($args['theme_location']) && 'footer' === $args['theme_location']) {
			$args['theme_location'] = 'mortgagehalo_footer';
		}
	} else if ($halo_product === 'realestatehalo') {
		if (isset($args['theme_location']) && 'primary' === $args['theme_location']) {
			$args['theme_location'] = 'realestatehalo_primary';
		}
		if (isset($args['theme_location']) && 'footer' === $args['theme_location']) {
			$args['theme_location'] = 'realestatehalo_footer';
		}
	} else if ($halo_product === 'contractorhalo') {
		if (isset($args['theme_location']) && 'primary' === $args['theme_location']) {
			$args['theme_location'] = 'contractorhalo_primary';
		}
		if (isset($args['theme_location']) && 'footer' === $args['theme_location']) {
			$args['theme_location'] = 'contractorhalo_footer';
		}
	}
	return $args;
}, 2);
// // ------------------------------------------------------------------------------------------------
// // Product Urls
// // ------------------------------------------------------------------------------------------------

// // rewrite rules
// add_action('init', function () {
// 	add_rewrite_rule('mortgagehalo/([^/]+)/?$', 'index.php?pagename=$matches[1]&haloproduct=mortgagehalo', 'top');
// 	add_rewrite_rule('realestatehalo/([^/]+)/?$', 'index.php?pagename=$matches[1]&haloproduct=realestatehalo', 'top');
// 	add_rewrite_rule('contractorhalo/([^/]+)/?$', 'index.php?pagename=$matches[1]&haloproduct=contractorhalo', 'top');

// 	// add_rewrite_rule('mortgagehalo/?$', 'index.php?pagename=mortgagehalo-home&haloproduct=mortgagehalo', 'top');
// 	// add_rewrite_rule('realestatehalo/?$', 'index.php?pagename=realestatehalo-home&haloproduct=realestatehalo', 'top');
// 	// add_rewrite_rule('contractorhalo/?$', 'index.php?pagename=contractorhalo-home&haloproduct=contractorhalo', 'top');
// });
// // register query variable
// add_filter('query_vars', function ($vars) {
// 	$vars[] = 'haloproduct';
// 	return $vars;
// });
// // change links for pages
// add_filter('page_link', function ($permalink, $post_id, $leavename) {

// 	$post = get_post($post_id); 
// 	$slug = $post->post_name;
// 	$product = get_field('bw_halo_product', $post);

// 	if (empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) {
// 		return $permalink;
// 	}

// 	if (empty($product) || $product === 'default') {
// 		return $permalink;
// 	}
// 	$permalink = site_url("$product/$slug/");

// 	return $permalink;
// }, 10, 3);


// ------------------------------------------------------------------------------------------------
// Product Options Pages
// ------------------------------------------------------------------------------------------------
add_action('init', function () {
	if(function_exists('acf_add_options_page')) {
		acf_add_options_page(array(
			'page_title' 	=> 'Halo Product Settings',
			'menu_title'	=> 'Halo Products',
			'menu_slug' 	=> 'halo-product-settings',
			'capability'	=> 'edit_posts',
			'redirect'		=> true
		));
		acf_add_options_sub_page(array(
			'page_title' 	=> 'MortgageHalo Settings',
			'menu_title'	=> 'MortgageHalo',
			'parent_slug'	=> 'halo-product-settings',
		));
		acf_add_options_sub_page(array(
			'page_title' 	=> 'RealEstateHalo Settings',
			'menu_title'	=> 'RealEstateHalo',
			'parent_slug'	=> 'halo-product-settings',
		));
		acf_add_options_sub_page(array(
			'page_title' 	=> 'ContractorHalo Settings',
			'menu_title'	=> 'ContractorHalo',
			'parent_slug'	=> 'halo-product-settings',
		));
	}
});
// add colors from options pages
add_action('wp_enqueue_scripts', 'bw_add_custom_css');
function bw_add_custom_css () {
	if (is_singular('page')) {
		$product = get_field('bw_halo_product');
		if ($product === 'mortgagehalo') {
			$colors = array(
				get_field('mortgagehalo_palette1', 'option'),
				get_field('mortgagehalo_palette2', 'option'),
				get_field('mortgagehalo_palette3', 'option'),
				get_field('mortgagehalo_palette4', 'option'),
				get_field('mortgagehalo_palette5', 'option'),
				get_field('mortgagehalo_palette6', 'option'),
				get_field('mortgagehalo_palette7', 'option'),
				get_field('mortgagehalo_palette8', 'option'),
				get_field('mortgagehalo_palette9', 'option')
			);
		} else if ($product === 'contractorhalo') {
			$colors = array(
				get_field('contractorhalo_palette1', 'option'),
				get_field('contractorhalo_palette2', 'option'),
				get_field('contractorhalo_palette3', 'option'),
				get_field('contractorhalo_palette4', 'option'),
				get_field('contractorhalo_palette5', 'option'),
				get_field('contractorhalo_palette6', 'option'),
				get_field('contractorhalo_palette7', 'option'),
				get_field('contractorhalo_palette8', 'option'),
				get_field('contractorhalo_palette9', 'option')
			);
		} else if ($product === 'realestatehalo') {
			$colors = array(
				get_field('realestatehalo_palette1', 'option'),
				get_field('realestatehalo_palette2', 'option'),
				get_field('realestatehalo_palette3', 'option'),
				get_field('realestatehalo_palette4', 'option'),
				get_field('realestatehalo_palette5', 'option'),
				get_field('realestatehalo_palette6', 'option'),
				get_field('realestatehalo_palette7', 'option'),
				get_field('realestatehalo_palette8', 'option'),
				get_field('realestatehalo_palette9', 'option')
			);
		}
		if (isset($colors)) {
			$style = "body{";
			if (!empty($colors[0])) $style .= "--global-palette1: $colors[0];";
			if (!empty($colors[1])) $style .= "--global-palette2: $colors[1];";
			if (!empty($colors[2])) $style .= "--global-palette3: $colors[2];";
			if (!empty($colors[3])) $style .= "--global-palette4: $colors[3];";
			if (!empty($colors[4])) $style .= "--global-palette5: $colors[4];";
			if (!empty($colors[5])) $style .= "--global-palette6: $colors[5];";
			if (!empty($colors[6])) $style .= "--global-palette7: $colors[6];";
			if (!empty($colors[7])) $style .= "--global-palette8: $colors[7];";
			if (!empty($colors[8])) $style .= "--global-palette9: $colors[8];";

			$style .= "--global-palette-highlight: var(--global-palette1);";
			$style .= "--global-palette-highlight-alt: var(--global-palette2);";
			$style .= "--global-palette-highlight-alt2: var(--global-palette9);";
			$style .= "--global-palette-btn-bg: var(--global-palette1);";
			$style .= "--global-palette-btn-bg-hover: var(--global-palette2);";
			$style .= "--global-palette-btn: var(--global-palette9);";
			$style .= "--global-palette-btn-hover: var(--global-palette9);";

			$style .= "}
			.bw-green-check > g > path {
				fill: var(--global-palette1) !important;
			}";

			wp_add_inline_style('child-theme', $style);
		}
	}
}
add_action('enqueue_block_editor_assets', 'bw_editor_add_custom_css');
function bw_editor_add_custom_css () {
	wp_register_style( 'halo-product-editor-style', false );
	wp_enqueue_style( 'halo-product-editor-style' );
	
	$product = get_field('bw_halo_product');
	if ($product === 'mortgagehalo') {
		$colors = array(
			get_field('mortgagehalo_palette1', 'option'),
			get_field('mortgagehalo_palette2', 'option'),
			get_field('mortgagehalo_palette3', 'option'),
			get_field('mortgagehalo_palette4', 'option'),
			get_field('mortgagehalo_palette5', 'option'),
			get_field('mortgagehalo_palette6', 'option'),
			get_field('mortgagehalo_palette7', 'option'),
			get_field('mortgagehalo_palette8', 'option'),
			get_field('mortgagehalo_palette9', 'option')
		);
	} else if ($product === 'contractorhalo') {
		$colors = array(
			get_field('contractorhalo_palette1', 'option'),
			get_field('contractorhalo_palette2', 'option'),
			get_field('contractorhalo_palette3', 'option'),
			get_field('contractorhalo_palette4', 'option'),
			get_field('contractorhalo_palette5', 'option'),
			get_field('contractorhalo_palette6', 'option'),
			get_field('contractorhalo_palette7', 'option'),
			get_field('contractorhalo_palette8', 'option'),
			get_field('contractorhalo_palette9', 'option')
		);
	} else if ($product === 'realestatehalo') {
		$colors = array(
			get_field('realestatehalo_palette1', 'option'),
			get_field('realestatehalo_palette2', 'option'),
			get_field('realestatehalo_palette3', 'option'),
			get_field('realestatehalo_palette4', 'option'),
			get_field('realestatehalo_palette5', 'option'),
			get_field('realestatehalo_palette6', 'option'),
			get_field('realestatehalo_palette7', 'option'),
			get_field('realestatehalo_palette8', 'option'),
			get_field('realestatehalo_palette9', 'option')
		);
	}
	if (isset($colors)) {
		$style = "body{";
		if (!empty($colors[0])) $style .= "--global-palette1: $colors[0];";
		if (!empty($colors[1])) $style .= "--global-palette2: $colors[1];";
		if (!empty($colors[2])) $style .= "--global-palette3: $colors[2];";
		if (!empty($colors[3])) $style .= "--global-palette4: $colors[3];";
		if (!empty($colors[4])) $style .= "--global-palette5: $colors[4];";
		if (!empty($colors[5])) $style .= "--global-palette6: $colors[5];";
		if (!empty($colors[6])) $style .= "--global-palette7: $colors[6];";
		if (!empty($colors[7])) $style .= "--global-palette8: $colors[7];";
		if (!empty($colors[8])) $style .= "--global-palette9: $colors[8];";

		$style .= "--global-palette-highlight: var(--global-palette1);";
		$style .= "--global-palette-highlight-alt: var(--global-palette2);";
		$style .= "--global-palette-highlight-alt2: var(--global-palette9);";
		$style .= "--global-palette-btn-bg: var(--global-palette1);";
		$style .= "--global-palette-btn-bg-hover: var(--global-palette2);";
		$style .= "--global-palette-btn: var(--global-palette9);";
		$style .= "--global-palette-btn-hover: var(--global-palette9);";

		$style .= "}
		button.components-button.kadence-color-palette__item[aria-label=\"Color: Accent\"] {
			color: var(--global-palette1) !important;
		}
		button.components-button.kadence-color-palette__item[aria-label=\"Color: Accent - alt\"] {
			color: var(--global-palette2) !important;
		}
		button.components-button.kadence-color-palette__item[aria-label=\"Color: Strongest text\"] {
			color: var(--global-palette3) !important;
		}
		button.components-button.kadence-color-palette__item[aria-label=\"Color: Strong Text\"] {
			color: var(--global-palette4) !important;
		}
		button.components-button.kadence-color-palette__item[aria-label=\"Color: Medium text\"] {
			color: var(--global-palette5) !important;
		}
		button.components-button.kadence-color-palette__item[aria-label=\"Color: Subtle Text\"] {
			color: var(--global-palette6) !important;
		}
		button.components-button.kadence-color-palette__item[aria-label=\"Color: Subtle Background\"] {
			color: var(--global-palette7) !important;
		}
		button.components-button.kadence-color-palette__item[aria-label=\"Color: Lighter Background\"] {
			color: var(--global-palette8) !important;
		}
		button.components-button.kadence-color-palette__item[aria-label=\"Color: White or offwhite\"] {
			color: var(--global-palette9) !important;
		}
		.bw-green-check > g > path {
			fill: var(--global-palette1) !important;
		}";

		wp_add_inline_style('halo-product-editor-style', $style);
	}
}


// ------------------------------------------------------------------------------------------------
// Product Before Header Hooks
//     bw_before_header_mortgagehalo
//     bw_before_header_contractorhalo
//     bw_before_header_realestatehalo
//     bw_before_header_haloprograms
// ------------------------------------------------------------------------------------------------
add_action('kadence_before_header', function () {
	remove_action( 'kadence_render_header_column', 'top', 'left_center' );
	if (is_singular('page')) {
		$product = get_field('bw_halo_product');
		if ($product === 'mortgagehalo') {
			do_action('bw_before_header_mortgagehalo');
			return;
		}
		if ($product === 'contractorhalo') {
			do_action('bw_before_header_contractorhalo');
			return;
		}
		if ($product === 'realestatehalo') {
			do_action('bw_before_header_realestatehalo');
			return;
		}
	}
	do_action('bw_before_header_haloprograms');
});

// ------------------------------------------------------------------------------------------------
// Product Header Logo
// ------------------------------------------------------------------------------------------------
add_filter('kadence_extra_custom_logo', function ($html, $option_string) {
	if (is_singular('page')) {
		$product = get_field('bw_halo_product');
		if ($product === 'mortgagehalo') {
			if ($option_string === 'transparent_header_logo') $src = get_field('mortgagehalo_logo_t', 'option');
			if (empty($src)) $src = get_field('mortgagehalo_logo', 'option');
		}
		if ($product === 'contractorhalo') {
			if ($option_string === 'transparent_header_logo') $src = get_field('contractorhalo_logo_t', 'option');
			if (empty($src)) $src = get_field('contractorhalo_logo', 'option');
		}
		if ($product === 'realestatehalo') {
			if ($option_string === 'transparent_header_logo') $src = get_field('realestatehalo_logo_t', 'option');
			if (empty($src)) $src = get_field('realestatehalo_logo', 'option');
		}
		if (!empty($src)) {
			$html = preg_replace('`src="[^"]*"`', 'src="' . $src . '"', $html);
		}
	}
	return $html;
}, 10, 2);
add_filter('kadence_custom_logo', function ($html, $blog_id) {
	if (is_singular('page')) {
		$product = get_field('bw_halo_product');
		if ($product === 'mortgagehalo') {
			$src = get_field('mortgagehalo_logo', 'option');
		}
		if ($product === 'contractorhalo') {
			$src = get_field('contractorhalo_logo', 'option');
		}
		if ($product === 'realestatehalo') {
			$src = get_field('realestatehalo_logo', 'option');
		}
		if (!empty($src)) {
			$html = preg_replace('`src="[^"]*"`', 'src="' . $src . '"', $html);
		}
	}
	return $html;
}, 10, 2);
add_filter('kadence_logo_url', function ($url) {
	if (is_singular('page')) {
		$product = get_field('bw_halo_product');
		if ($product === 'mortgagehalo' || $product === 'contractorhalo' || $product === 'realestatehalo') {
			$url .= $product . '/';
		}
	}
	return $url;
});

// ------------------------------------------------------------------------------------------------
// Kadence Pro filter to enable/disable extra headers
// ------------------------------------------------------------------------------------------------
// 'kadence_conditional_header_display' : filter

// ------------------------------------------------------------------------------------------------
// Footer hooks
// ------------------------------------------------------------------------------------------------
add_action('kadence_top_footer', function () {
	if (is_singular('page')) {
		$product = get_field('bw_halo_product');
		if ($product === 'mortgagehalo') {
			remove_action('kadence_top_footer', 'Kadence\top_footer');
			remove_action('kadence_middle_footer', 'Kadence\middle_footer');
			remove_action('kadence_bottom_footer', 'Kadence\bottom_footer');
			do_action('bw_footer_mortgagehalo');
			return;
		}
		if ($product === 'contractorhalo') {
			remove_action('kadence_top_footer', 'Kadence\top_footer');
			remove_action('kadence_middle_footer', 'Kadence\middle_footer');
			remove_action('kadence_bottom_footer', 'Kadence\bottom_footer');
			do_action('bw_footer_contractorhalo');
			return;
		}
		if ($product === 'realestatehalo') {
			remove_action('kadence_top_footer', 'Kadence\top_footer');
			remove_action('kadence_middle_footer', 'Kadence\middle_footer');
			remove_action('kadence_bottom_footer', 'Kadence\bottom_footer');
			do_action('bw_footer_realestatehalo');
			return;
		}
	}
}, 0);

// ------------------------------------------------------------------------------------------------
// Body Classes
// ------------------------------------------------------------------------------------------------
add_filter('body_class', function ($classes) {
	$new_classes = array();
	if (is_singular('page')) {
		$product = get_field('bw_halo_product');
		if ($product === 'mortgagehalo') {
			$new_classes[] = 'bw-mortgagehalo-page';
		} else if ($product === 'contractorhalo') {
			$new_classes[] = 'bw-contractorhalo-page';
		} else if ($product === 'realestatehalo') {
			$new_classes[] = 'bw-realestatehalo-page';
		}
	}
	return array_merge($classes, $new_classes);
});

// ------------------------------------------------------------------------------------------------
// Kadence Pro Conditional headers
// ------------------------------------------------------------------------------------------------
add_filter('kadence_conditional_header_display', function ($result, $header) {
	if (is_singular('page')) {
		$product = get_field('bw_halo_product');
		if ($product === 'mortgagehalo') {
			if (get_field('mortgagehalo_custom_menu', 'option')) {
				if ($header['label'] === 'Halo Product Menu') return true;
			}
		} else if ($product === 'contractorhalo') {
			if (get_field('contractorhalo_custom_menu', 'option')) {
				if ($header['label'] === 'Halo Product Menu') return true;
			}
		} else if ($product === 'realestatehalo') {
			if (get_field('realestatehalo_custom_menu', 'option')) {
				if ($header['label'] === 'Halo Product Menu') return true;
			}
		}
	}
	return $result;
}, 5, 2);


// ------------------------------------------------------------------------------------------------
// Check Mark Shortcode
// ------------------------------------------------------------------------------------------------
add_shortcode('yes', function () {ob_start();
	?>
	<svg class="bw-green-check" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="512" height="512" x="0" y="0" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512" xml:space="preserve" class="">
		<g>
			<path style="" d="M433.139,67.108L201.294,298.953c-6.249,6.249-16.381,6.249-22.63,0L78.861,199.15L0,278.011
		l150.547,150.549c10.458,10.458,24.642,16.333,39.431,16.333l0,0c14.788,0,28.973-5.876,39.43-16.333L512,145.968L433.139,67.108z" fill="#a5eb78" data-original="#a5eb78" class=""></path>
			<g style="opacity:0.1;">
				<path d="M485.921,119.888L187.59,418.22c-8.254,8.253-18.633,13.882-29.847,16.391c9.363,6.635,20.608,10.28,32.235,10.28l0,0
			c14.788,0,28.973-5.876,39.43-16.333L512,145.966L485.921,119.888z" fill="#000000" data-original="#000000" class=""></path>
			</g>
		</g>
	</svg>
	<?php
	return ob_get_clean();
});