<?php
function flo_get_render_data_flex($attributes) {

  $block_obj = new Flo_Flex_Builder_Render_Block($attributes);
  return $block_obj ? $block_obj->render() : '';

  $b = "flex-block"; // To be used inside HTML

  $anchor = isset($attributes['blockAnchor']) ? $attributes['blockAnchor'] : '';
  // deb($attributes);

  // Start: Class name automation
  $block_id = mt_rand(1, 999);
  $b__for_css = "." . $b; // To be used inside CSS
  $b__uniq = $b . "--" . $block_id; // To be used inside HTML
  $b__uniq_for_css = "." . $b__uniq; // To be used inside CSS
  // End: Class name automation

  if (is_array($attributes) && isset($attributes['data'])) {
    // GUTEMBERG
    $encoded_data = $attributes['data'];
    $custom_block_class = '';
    $hide_on_desktop = isset($attributes['hideOnDesktop']) && $attributes['hideOnDesktop'] ? true : false;
    $hide_on_mobile = isset($attributes['hideOnMobile']) && $attributes['hideOnMobile'] ? true : false;
  } else {
    if ($attributes && sizeof($attributes)) {
      // FLEX THEME

      $encoded_data = flo_data($attributes, "flo_flex_builder_obj");
      $margins = flo_data($attributes, "flex_margins");
      $hide_on_desktop = flo_data($attributes, "flex__hide-desktop");
      $hide_on_mobile = flo_data($attributes, "flex__hide-mobile");
      $custom_block_id = flo_data($attributes, "flex__block-custom-id");
      $custom_block_class = flo_data($attributes, "flex__block-custom-class");
    } else {
      if (!function_exists('get_sub_field')) return '';
      // FLOTHEMES SITE
      $builder_obj = get_sub_field("flo_flex_builder_obj");
      if ($builder_obj) {
        $encoded_data = $builder_obj;
        $margins = get_sub_field("flex_margins");
        $hide_on_desktop = get_sub_field("flex__hide-desktop");
        $hide_on_mobile = get_sub_field("flex__hide-mobile");
        $custom_block_id = get_sub_field("flex__block-custom-id");
        $custom_block_class = get_sub_field("flex__block-custom-class");
      } else {
        return '';
      }
    }
  }

  $custom_block_id = isset($custom_block_id) && $custom_block_id ? 'id="' . $custom_block_id . '"' : '';
  $decoded_data = json_decode($encoded_data, true);

  if (!function_exists('flex_em_calc')) {
    function flex_em_calc($val, $font_size, $viewport) {
      $numVal = floatval($val);
      $viewportScale = $viewport === 'desktop' ? 12.8 : 3.75;
      $emVal = $numVal / ($viewportScale * $font_size);
      return $emVal;
    }
  }

  // get color names and send to renderer
  $colors = null;
  if (
    isset($decoded_data['globalOptions']['themeColors']) &&
    sizeof($decoded_data['globalOptions']['themeColors'])
  ) {
    $colors = $decoded_data['globalOptions']['themeColors'];
  }

  $desktop_font_size = 1.25;
  if (isset($decoded_data['globalOptions']['htmlFontSize'])) {
    $desktop_font_size = $decoded_data['globalOptions']['htmlFontSize'];
  }

  $mob_height_em = 0;
  $mobile_font_size = 4.26;
  if (isset($decoded_data['mobile_elements']) && is_array($decoded_data['mobile_elements']) && sizeof($decoded_data['mobile_elements'])) {
    if (isset($desktop_font_size) && strlen($desktop_font_size)) {
      $desktopFontSizePx = $desktop_font_size * (1280 / 100);
      $mobile_font_size = $desktopFontSizePx / (375 / 100);
    } else {
      $desktopFontSizePx = 16;
      $mobile_font_size = 4.26;
    }

    // MOBILE HEIGHT
    $wasChangedAfterUpdate = isset($decoded_data['globalOptions']['heightFixFlag']) && isset($decoded_data['globalOptions']['heightFixFlag']) === true;
    if ($wasChangedAfterUpdate || defined('FLEX_MOBILE_HEIGHT_FLAG')) {
      $mob_height_em = str_replace('rem', 'em', $decoded_data['globalOptions']['globalMobileBlockHeightRem']);
    } else {
      $mob_height_em = $decoded_data['globalOptions']['globalMobileBlockHeight'] / $desktopFontSizePx . "em";
    }
  }

  // DESKTOP HEIGHT
  if (isset($decoded_data['globalOptions']['globalBlockHeightRem']) && strlen($decoded_data['globalOptions']['globalBlockHeightRem'])) {
    $blockHeightVal = str_replace('rem', 'em', $decoded_data['globalOptions']['globalBlockHeightRem']);
  } else {
    $blockHeightVal = '46.875em';
  }

  $globalBlockBgColor = isset($decoded_data['globalOptions']['globalBlockBgColor']) ? $decoded_data['globalOptions']['globalBlockBgColor'] : '';
  $desktopBg = parseColor($globalBlockBgColor, $colors);

  $topMargin = isset($margins['flex_top_margin_desktop']) ? $margins['flex_top_margin_desktop'] : 0;
  $bottomMargin = isset($margins['flex_bottom_margin_desktop']) ? $margins['flex_bottom_margin_desktop'] : 0;

  $desktop_styles = "
      --html-font-size: " . $desktop_font_size . "vw;
      --height:" . $blockHeightVal . ";
      --bg-color: " . getStyleKitColor($desktopBg) . ";
      --margin-top: " . flex_em_calc($topMargin, $desktop_font_size, "desktop") . "em;
      --margin-bottom: " . flex_em_calc($bottomMargin, $desktop_font_size, "desktop") . "em;
    ";
  $desktop_styles = preg_replace('/\s/', '', $desktop_styles);

  // We need these in any of the element PHP to calculate sizes for srcsets
  global $flex_desktop_font_size;
  global $flex_mobile_font_size;
  $flex_desktop_font_size = (float) $desktop_font_size;
  $flex_mobile_font_size = (float) $mobile_font_size;

  $globalMobileBlockHeight = isset($decoded_data['globalOptions']['globalMobileBlockHeight']) ? $decoded_data['globalOptions']['globalMobileBlockHeight'] : '';
  $mob_height =
    $globalMobileBlockHeight !== false && strlen($globalMobileBlockHeight)
    ? $globalMobileBlockHeight
    : 0;

  $globalMobileBlockBgColor = isset($decoded_data['globalOptions']['globalMobileBlockBgColor']) ? $decoded_data['globalOptions']['globalMobileBlockBgColor'] : '';
  $mobBg = parseColor($globalMobileBlockBgColor, $colors);

  $topMarginMob = isset($margins['flex_top_margin_mobile']) ? $margins['flex_top_margin_mobile'] : 0;
  $bottomMarginMob = isset($margins['flex_bottom_margin_mobile']) ? $margins['flex_bottom_margin_mobile'] : 0;

  $mobile_styles = "
      --mob-height: " . $mob_height_em . ";
      --mob-bg-color: " . getStyleKitColor($mobBg) . ";
      --margin-top: " . flex_em_calc($topMarginMob, $mobile_font_size, "mobile") . "em;
      --margin-bottom: " . flex_em_calc($bottomMarginMob, $mobile_font_size, "mobile") . "em;
    ";
  $mobile_styles = preg_replace('/\s/', '', $mobile_styles);

  if (isset($decoded_data['mobile_elements']) && is_array($decoded_data['mobile_elements']) && sizeof($decoded_data['mobile_elements'])) {
    $mobile_styles .= "--html-font-size: " . $mobile_font_size . "vw;";
  }

  $desktopElements = renderElements($decoded_data, $viewport = "desktop");
  $mobileElements = renderElements($decoded_data, $viewport = "mobile");



  $desktopGenerated = $hide_on_desktop !== true ? 'data-flex-desktop="' . htmlentities('<div class="' . $b . '__main-container" style="' . $desktop_styles . '">' . $desktopElements . '</div>', ENT_QUOTES, 'UTF-8') . '"' : '';
  $mobileGenerated = $hide_on_mobile !== true ? 'data-flex-mobile="' . htmlentities('<div class="' . $b . '__mobile-container" style="' . $mobile_styles . '">' . $mobileElements . '</div>', ENT_QUOTES, 'UTF-8') . '"' : '';
  $blockID = isset($decoded_data['blockID']) ? $decoded_data['blockID'] : rand();

  $desktopFullscreenClass = isset($decoded_data['globalOptions']['globalBlockHeightIsFullscreen']) && $decoded_data['globalOptions']['globalBlockHeightIsFullscreen'] ? $b . '--desktop-fullscreen' : '';
  $mobileFullscreenClass = isset($decoded_data['globalOptions']['globalMobileBlockHeightIsFullscreen']) && $decoded_data['globalOptions']['globalMobileBlockHeightIsFullscreen'] ? $b . '--mobile-fullscreen' : '';

  // Popup code
  $isPopup = isset($attributes['isPopup']) && $attributes['isPopup'];
  $isPopupClass = $isPopup ? $b . '--is-popup' : '';

  $popupSettingsDataAttribute = '';
  if ($isPopup) {
    $popupSettings = isset($decoded_data['globalOptions']['popupSettings']) ? $decoded_data['globalOptions']['popupSettings'] : null;
    if ($popupSettings) {
      $popupSettingsDataAttribute = 'data-popup-settings="' . htmlspecialchars(json_encode($popupSettings)) . '"';
    }
  }

  $classes = '
    alignfull
    ' . $b . '
    ' . $b . '--' . $blockID . '
    ' . $custom_block_class . '
    ' . $desktopFullscreenClass . '
    ' . $mobileFullscreenClass . '
    ' . $isPopupClass . '
  ';
  return '
    <div
      ' . $custom_block_id . '
        class="' . $classes . '"
        data-block-id="' . $blockID . '"' . ' ' . '
        ' . $desktopGenerated  . ' ' . '
        ' . $mobileGenerated  . ' ' . '
        id="' . $anchor . '"
        ' . $popupSettingsDataAttribute . '
      ></div>';
}
