<?php

function getFontStylesData() {
  
  $theme_typography_option_name = 'flo-typography';
  $theme_typography_option_name = apply_filters('flo_theme_typography_option_name', $theme_typography_option_name);
  $flo_typography = get_field($theme_typography_option_name, 'options');
  $font_styles = [];
  
  if(isset($flo_typography['font_styles'])) {
    if (is_string($flo_typography['font_styles'])) {
      $font_styles_data = json_decode($flo_typography['font_styles']);
    } else {
      $font_styles_data = (array) $flo_typography['font_styles'];
    }
    
    if(is_object($font_styles_data)) {

      $theme_defaults = [];
      $user_specified = [];
      
      
      if(isset($font_styles_data->theme_defaults)) {
        $theme_defaults = $font_styles_data->theme_defaults;
      }
      if(isset($font_styles_data->user_specified)) {
        $user_specified = $font_styles_data->user_specified;
      }

      // merge the 2 objects to have only 1 iteration for both
      $typography_styles = (object) array_merge((array) $theme_defaults, (array) $user_specified);
      // iterate and construct font data

      foreach ($typography_styles as $fontName => $value) {
        $font_style_css = get_font_styles($value);
        
        $font_styles[$fontName] = [
          'name' => $value->font_style_name,
          'style' => $font_style_css['default'],
          'mobile_style' => $font_style_css['mobile']
        ];
      }
    }
  }
  
  return $font_styles;
}

function makeParentColorStyles($colorsObj) {
  $css = "";
  
  if(sizeof($colorsObj)) {
    $css .= ".flex-block__main-container, .flex-block__mobile-container {";
    foreach ($colorsObj as $colorName => $colorValue) {
      $css .= "--".$colorName .":".$colorValue.";";
    }
    $css .= "}";
  }
  
  return $css;
}

function formatColorName($colorName) {
  $newColorName = str_replace('/', '__SLASH__', $colorName);
  return $newColorName;
}

// check if given color value is a label that exists in the theme colors list
// if it is, return it as a css var
function parseColor($colorVal, $colors = null) {
  $newColorVal = $colorVal;
  if(is_array($colors) && sizeof($colors)) {
    if(isset($colors[$colorVal])) {
      $newColorVal = 'var(--'.formatColorName($colorVal).')';
    }
  }
  
  return $newColorVal;
}

function getThemeColors() {
  $colorsObj = [];
  
  if(function_exists('flo_get_current_style_kit')) {
    $current_stylekit = flo_get_current_style_kit();
    
    if(file_exists(get_template_directory()."/theme-files/flo_stylekits_config/".$current_stylekit."/colors_settings.php")) {
      include(get_template_directory()."/theme-files/flo_stylekits_config/".$current_stylekit."/colors_settings.php");
      
      if(isset($color_settings) && is_array($color_settings)) {
        $colorsObj = [];
        
        foreach ($color_settings as $color_name => $color_value) {
          $color_obj = get_field_object($color_name, 'options');
          $formattedColorName = formatColorName($color_name);
          
          if($color_obj['value']) {
            $colorsObj[$formattedColorName] = $color_obj['value'];
          } else {
            $colorsObj[$formattedColorName] = $color_obj['default_value'];
          }
        }
        
      }
    }
  }
  
  return $colorsObj;
}

function convertFontDataToCss($fontData) {
  $css = "";
  $mobile_css = "";
  if(sizeof($fontData)) {
    $mobile_css = "@media (max-width: 767px) {";
    foreach ($fontData as $fontKey => $fontObj) {
      $css .= 'body .flex-block__main-container [data-font="'.$fontKey.'"], body .flex-block__mobile-container [data-font="'.$fontKey.'"]{'.$fontObj["style"].'}';
      if(isset($fontObj['mobile_style'])) {
        $mobile_style = str_replace("rem", "em", $fontObj['mobile_style']);
        $mobile_css .= 'body .flex-block__main-container [data-font="'.$fontKey.'"], body .flex-block__mobile-container [data-font="'.$fontKey.'"] {'.$mobile_style.'}';
      }
    }
    $mobile_css .= "}";
  }
  
  return $css . $mobile_css;
}

function getParentThemeData() {
  $fontData = getFontStylesData();
  $fontDataCss = convertFontDataToCss($fontData);
  
  $flexThemeColors = getThemeColors();
  $parentStyles = makeParentColorStyles($flexThemeColors);

  $flexInlineStyles = $fontDataCss . $parentStyles;

  wp_add_inline_style( "flo-flex-builder", $flexInlineStyles );
}

add_action( 'wp_enqueue_scripts', 'getParentThemeData', 11 );

?>