<?php

add_theme_support('woocommerce');

function init_flo_functions()
{
	if (!is_admin()) {
		add_action('wp', 'flo_wp_action_trigger');
	} else {
	}
}

function fgt_reduce_fields($posts)
{
	$filtered_result = [];
	$invalid_fields = ['post_content'];

	foreach ($posts as $post) {
		foreach ($invalid_fields as $field) {
			unset($post->{$field});
		}

		$filtered_result[] = $post;
	}

	return $filtered_result;
}

function fgt_render_header_custom_code($data)
{
	$html = '';

	if (isset($data['trackingCode']) && $data['trackingCode']) {
		$html .= $data['trackingCode'];
	}

	if (isset($data['cssCode']) && $data['cssCode']) {
		$html .= '<style>' . $data['cssCode'] . '</style>';
	}

	return $html;
}

function fgt_render_footer_custom_code($data)
{
	$html = '';

	if (isset($data['jsCode']) && $data['jsCode']) {
		$html .= '<script>' . $data['jsCode'] . '</script>';
	}

	return $html;
}

function flo_wp_action_trigger()
{
	flo_set_archive_layout();
	flo_set_header_content();
}

function flo_set_header_content()
{
	// Add code that way or rethink.
}

function flo_set_archive_layout()
{
	$is_archive = is_archive();
	$is_search = is_search();
	$is_404 = is_404();

	$templates_array = get_option('fgt_templates_array') ? json_decode(get_option('fgt_templates_array'), TRUE) : FALSE;

	if ($templates_array) {
		if ($is_archive || $is_search || $is_404) {
			if ($is_archive) {
				$current_wrapper = $templates_array['archive'];
			} elseif ($is_search) {
				$current_wrapper = $templates_array['search'];
			} else {
				$current_wrapper = $templates_array['page_404'];
			}
		}
	}

	if (isset($current_wrapper)) {
		global $fgt_global_wrapper;
		$fgt_global_wrapper = get_post($current_wrapper);
	}
}

function flo_app_render_sidebar()
{
	global $post;

	if (!isset($post->ID)) {
		return;
	}
	$value = get_post_meta($post->ID, '_flothemes_sidebar_metabox_field', TRUE);

	if ($value === 'left_sidebar' || $value === 'right_sidebar') {
		return get_sidebar();
	}
}

/**
 * Returns a normalized array with options from Options table.
 * Will merge with default options declared in /config/global-options.config.php
 * @param (string) $option_name
 */
function flo_get_global_option_arr($option_name)
{
	global $fgt_global_options;

	$data = get_option($option_name);
	if (!is_array($data)) {
		$data = array();
	}
	return array_merge($fgt_global_options[$option_name]['default'], $data);
}

// PHP program to carry out multidimensional array
// search by key=>value

// Function to recursively search for a
// given key=>value
function flo_search_array_key_value($array, $key, $value)
{
	$results = array();

	// if it is array
	if (is_array($array)) {

	// if array has required key and value
		// matched store result
		if (isset($array[$key]) && $array[$key] == $value) {
			$results[] = $array;
		}

		// Iterate for each element in array
		foreach ($array as $subarray) {

	  // recur through each element and append result
			$results = array_merge(
				$results,
				flo_search_array_key_value($subarray, $key, $value)
			);
		}
	}

	return $results;
}

// Fallback in case Flex is not active
if (!function_exists('getStyleKitColor')) {
	function getStyleKitColor()
	{
	}
}

// Render Preloader
if (!function_exists('fgt_render_preloader')) {
	function fgt_render_preloader($data)
	{
		if (!isset($data) || isset($data) && !$data['enableSitePreloader']) {
			return;
		}

		$markup = '
			<div
				class="fgt-preloader"
				style="
					--backgroundColor: ' . getStyleKitColor($data['preloaderBackgroundColor']) . ';
					--preloaderDesktopSize: ' . ($data['preloaderDesktopSize'] / 16) . 'rem;
					--preloaderMobileSize: ' . ($data['preloaderMobileSize'] / 16) . 'rem;
				"
			>
		';

		$useText = $data['preloaderUseText'];
		$useLottie = $data['preloaderLottie'];

		if ($useText) {
			$markup .= '
				<div
					class="fgt-preloader__text"
					data-font="' . $data['preloaderTextFont'] . '"
					style="
						--colorText: ' . getStyleKitColor($data['preloaderColorText']) . ';
					"
				>
					' . $data['preloaderText'] . '
				</div>
			';
		} elseif ($useLottie) {
			$markup .= '
				<div class="fgt-preloader__lottie" data-lottie="' . $data['preloaderLottieFile']['url'] . '">
					<div id="lottieLodaer"></div>
				</div>
			';
		} else {
			$imgsrc = isset($data['preloaderCustomImage']['url']) ? $data['preloaderCustomImage']['url'] : '';
			$markup .= '
				<img class="fgt-preloader__custom-image" src="' . $imgsrc . '" alt="">
			';
		}

		$markup .= '</div>';
		echo $markup;
	}
}

// Render Link Styles
if (!function_exists('fgt_render_link_styles')) {
	function fgt_render_link_styles($data)
	{
		if (
	  !isset($data)
	  || (!$data['styleFlexLinks'] && !$data['styleContentLinks'])
	) {
			return;
		}

		$styles = '
			<style class="fgt-link-styles">
		';

		if ($data['styleFlexLinks']) {
			$styles .= '
					.entry-content > .flex-block > .flex-block__main-container .flex-block__element--text a,
					.entry-content > .flex-block > .flex-block__main-container .flex-block__element--text .flex-block__text-content:not(.old-editor-content)[data-font] a,
					.entry-content > .flex-block > .flex-block__main-container .flex-block__element--button .flex-block__text-content:not(.old-editor-content)[data-font] a {
			';

			if ($data['changeFlexLinksColor']) {
				$styles .= 'color: ' . $data["flexLinkColor"] . ';';
			}
			$flexLinkFormat = $data['flexLinkFormat'];
			if ($flexLinkFormat['bold']) {
				$styles .= 'font-weight: bold!important;';
			}
			if ($flexLinkFormat['italic']) {
				$styles .= 'font-style: italic!important;';
			}
			if ($flexLinkFormat['underline']) {
				$styles .= 'text-decoration: underline!important;
			}';
			}
		}

		if ($data['styleContentLinks']) {
			$styles .= '
					.entry-content > *:not(.flex-block):not([class*="flo-"]) a:not(.wp-block-button__link),
					.entry-content > a, .flo-block-text__text > a {
			';
			if ($data['changeContentLinksColor']) {
				$styles .= 'color: ' . $data['linkColor'] . ';';
			}
			$linkFormat = $data['linkFormat'];
			if ($linkFormat['bold']) {
				$styles .= 'font-weight: bold!important;';
			}
			if ($linkFormat['italic']) {
				$styles .= 'font-style: italic!important;';
			}
			if ($linkFormat['underline']) {
				$styles .= 'text-decoration: underline!important;';
			}
		}

		$styles .= '
				}
			</style>
		';

		echo $styles;
	}
}

/**
 *
 * In an given string look for '%year%' and replace it with the current Year
 * @param string $str - the given string
 * @return string
 */
function flo_get_copyright_year($str)
{
	return str_replace('%year%', date('Y'), $str);
}

init_flo_functions();
