<?php

if (!function_exists('fgt_render_gt')) {
	function fgt_render_gt()
	{
		if (!function_exists('getStylekitFontStyle')) {
			return;
		}

		$data = flo_get_global_option_arr('fgt_options');
		if (gettype($data) === 'string') {
			$data = json_decode($data, TRUE);
		}

		$style = '';

		// Render for entire content
		$dFontStyle = getStylekitFontStyle($data['genericFontParagraph']);
		$style .= '.entry-content {';
		if (isset($dFontStyle["lineHeight"])) {
			$style .= 'line-height: '.$dFontStyle["lineHeight"].'em;';
		}
		if (isset($dFontStyle["fontSize"])) {
			$style .= 'font-size: '.($dFontStyle["fontSize"] / 16).'rem;';
		}
		if (isset($dFontStyle["letterSpacing"])) {
			$style .= 'letter-spacing: '.$dFontStyle["letterSpacing"].'em;';
		}
		if (isset($dFontStyle["fontFamily"])) {
			$style .= 'font-family: '.$dFontStyle["fontFamily"].';';
		}
		if (isset($dFontStyle["fontWeight"])) {
			$style .= 'font-weight: '.$dFontStyle["fontWeight"].';';
		}
		if (isset($dFontStyle["fontStyle"])) {
			$style .= 'font-style: '.$dFontStyle["fontStyle"].';';
		}
		if (isset($dFontStyle["textDecoration"])) {
			$style .= 'text-decoration: '.$dFontStyle["textDecoration"].';';
		}
		if (isset($dFontStyle["textTransform"])) {
			$style .= 'text-transform: '.$dFontStyle["textTransform"].';';
		}
		$style .= '}';

		$style .= '@media (max-width: 767px) {';

		$style .= '.entry-content {';

		if (isset($dFontStyle['mobile']["lineHeight"])) {
			$style .= 'line-height: '.$dFontStyle['mobile']["lineHeight"].'em;';
		}

		if (isset($dFontStyle['mobile']["fontSize"])) {
			$style .= 'font-size: '.($dFontStyle['mobile']["fontSize"] / 16).'rem;';
		}

		if (isset($dFontStyle['mobile']["letterSpacing"])) {
			$style .= 'letter-spacing: '.$dFontStyle['mobile']["letterSpacing"].'em;';
		}

		$style .= '
        }
      }
    ';

		// Render for selectors
		$fontsToRender = array(
	  'h1'  => 'genericFontH1',
	  'h2'  => 'genericFontH2',
	  'h3'  => 'genericFontH3',
	  'h4'  => 'genericFontH4',
	  'h5'  => 'genericFontH5',
	  'h6'  => 'genericFontH6',
	  'p'   => 'genericFontParagraph',
	  'blockquote p'   => 'genericFontPullQuote',
	  '.wp-block-preformatted'   => 'genericFontPreformatted',
	  // 'p a' => 'genericFontParagraphLink',
	);
		foreach ($fontsToRender as $selector => $key) {
			$fontName =  $data[$key];

			$fontStyle = getStylekitFontStyle($fontName);

			if (!isset($fontStyle) || !$fontStyle) {
				continue;
			}

			$style .= '.entry-content > *:not(.flex-block):not([class*="flo-"]) '.$selector.' ,';
			$style .= '.entry-content > '.$selector.' {';

			if (isset($fontStyle["lineHeight"])) {
				$style .= 'line-height: '.$fontStyle["lineHeight"].'em;';
			}
			if (isset($fontStyle["fontSize"])) {
				$style .= 'font-size: '.($fontStyle["fontSize"] / 16).'rem;';
			}
			if (isset($fontStyle["letterSpacing"])) {
				$style .= 'letter-spacing: '.$fontStyle["letterSpacing"].'em;';
			}
			if (isset($fontStyle["fontFamily"])) {
				$style .= 'font-family: '.$fontStyle["fontFamily"].';';
			}
			if (isset($fontStyle["fontWeight"])) {
				$style .= 'font-weight: '.$fontStyle["fontWeight"].';';
			}
			if (isset($fontStyle["fontStyle"])) {
				$style .= 'font-style: '.$fontStyle["fontStyle"].';';
			}
			if (isset($fontStyle["textDecoration"])) {
				$style .= 'text-decoration: '.$fontStyle["textDecoration"].';';
			}
			if (isset($fontStyle["textTransform"])) {
				$style .= 'text-transform: '.$fontStyle["textTransform"].';';
			}
			$style .='}';

			$style .= '@media (max-width: 767px) {';
			$style .= '.entry-content > *:not(.flex-block):not([class*="flo-"]) '.$selector.' ,';
			$style .= '.entry-content > '.$selector.' {';

			if (isset($fontStyle['mobile']["lineHeight"])) {
				$style .= 'line-height: '.$fontStyle['mobile']["lineHeight"].'em;';
			}

			if (isset($fontStyle['mobile']["fontSize"])) {
				$style .= 'font-size: '.($fontStyle['mobile']["fontSize"] / 16).'rem;';
			}

			if (isset($fontStyle['mobile']["letterSpacing"])) {
				$style .= 'letter-spacing: '.$fontStyle['mobile']["letterSpacing"].'em;';
			}

			$style .= '
          }
        }
      ';
		}

		echo '
      <style class="fgt-generic-typography">
        '.$style.'
      </style>
    ';
	}
}
