<?php

function renderLogoElement($b, $element, $custom_classes, $elStyles, $colors, $viewport)
{
	$data = $element['data'];
	$user_data = $data['user_data'];
	$elMarkup = "";

	// Start: Prepare data
	$useCustomOptions = useVar($user_data, 'useCustomOptions', FALSE);
	$dataSource = $user_data;

	if ($useCustomOptions) {
		$custom_user_data = [];
		foreach ($user_data as $key => $value) {
			$custom_user_data[$key] = useVar($user_data, 'custom_'.$key, $value);
		}
		$dataSource = $custom_user_data;
	}
	// End: Prepare data

	$useTextLogo = isset($dataSource['useTextLogo']) && $dataSource['useTextLogo'];
	$wrapperClasses = "flex-element-type-logo";

	if ($useTextLogo) {
		$wrapperClasses .= " flex-element-type-logo--type-text";
	}

	$elMarkup .= '
    <div class="'.$b.'__element '.$b.'__element--'.$element["id"].' '.$b.'__element--logo '.$custom_classes.'" style="'.$elStyles.'">
      <a class="'.$wrapperClasses.'" href="'.get_home_url().'">
  ';

	// Content - Text or Image
	if ($useTextLogo) {
		// Text Logo
		$text_enc = urldecode($dataSource['textLogoText']);
		$text_enc = str_replace("__SLASH__", "--", $text_enc);
		$text_html = html_entity_decode($text_enc);
		$text = $text_html;

		$textLogoStyles = "";
		$fontData = getFontData($dataSource, 'textLogoFont');
		$textLogoStyles .= $fontData['style'];
		$dataFont = $fontData['dataFont'];

		$textLogoStyles .= "color: ".getStyleKitColor($dataSource["textLogoColor"]).";";

		$elMarkup .= '
      <span
        class="flex-element-type-logo__text '.(isset($dataSource['alignLogoText']) ? $dataSource['alignLogoText'] : '').'"
        style="'.$textLogoStyles.'"
        data-font="'.$dataFont.'"
      >
      '.$text.'
      </span>
    ';
	} else {
		// Image
		$defaultImage = '';
		$lightImage = '';

		if (isset($dataSource['defaultImage']) && isset($dataSource['defaultImage']['url'])) {
			$defaultImage = $dataSource['defaultImage']['url'];
			$lightImage = $defaultImage;

			if (isset($dataSource['lightImage']) && isset($dataSource['lightImage']['url'])) {
				$lightImage = $dataSource['lightImage']['url'];
			}
		}
		$elMarkup .= '
      <span
        class="flex-element-type-logo__image flex-element-type-logo__image--dark"
        style="background-image: url('.$defaultImage.')"
      ></span>
      <span
        class="flex-element-type-logo__image flex-element-type-logo__image--light"
        style="background-image: url('.$lightImage.')"
      ></span>
    ';
	}

	$elMarkup .= '
      </a>
    </div>
  ';

	return $elMarkup;
}
