<?php

function renderSocialLinksElement($b, $element, $custom_classes, $elStyles, $colors, $viewport) {
  $data = $element['data'];
  $user_data = $data['user_data'];
  $socialLinksType = $user_data['socialLinksType'];
  $socialLinksStyle = $user_data['socialLinksStyle'];
  $socialLinksStyleType = $user_data['socialLinksStyleType'];
  $orientation = $user_data["orientation"];
  $socialLinks = $user_data['newSocialLinks'];
  $shareLinks = $user_data['newShareLinks'];

  $enableTitle = $user_data["enableTitle"];
  $titleText = $user_data["titleText"];
  $titleShareText = $user_data["titleShareText"];
  $titleColor = $user_data["titleColor"];
  $socialsTextColor = $user_data["socialsTextColor"];
  $socialsIconColor = $user_data["socialsIconColor"];
  $socialsBgColor = $user_data["socialsBgColor"];
  $gap = $user_data['gap'];
  $iconSize = $user_data['iconSize'];

  $list = $socialLinksType === "links" ? $socialLinks : $shareLinks;

  $links = [];
  $linkClass =

  // Sharing code
  $post = get_post();

  if ( !$post ) {
    $post = (object) array(
      'post_title' => get_bloginfo('name'),
    );

    $post_permalink = urlencode(get_home_url());

    $post_image = '';
  } else {
    $post_permalink = urlencode(get_permalink($post->ID));
    $post_image = urlencode(get_the_post_thumbnail_url($post->ID));
  }



	$social_mapping = array(
		'facebook' => 'http://www.facebook.com/sharer/sharer.php?u='.$post_permalink,
		'twitter' => 'http://twitter.com/home?status='.urlencode($post->post_title).'+'.$post_permalink,
		'googlePlus' => 'https://plus.google.com/share?url='.$post_permalink,
		'pinterest' => 'https://www.pinterest.com/pin/create/button/'
	);

  foreach ($list  as $itemKey => $item) {
    $attr = '';
    $url = false;

    if ($socialLinksType === 'share') {
      if ($item['type'] === 'pinterest') {
        $pinterest_sharing_enabled = true;
        $attr= 'data-pin-do="buttonBookmark" data-pin-custom="true" href="https://www.pinterest.com/pin/create/button/?url='.$post_permalink.'&description='.urlencode($post->post_title).'&media='.$post_image.'"';
      } else {
        $url = $social_mapping[$item['type']];
      }
    } else {
      $url = $item['link'];
    }

    if ($url) {
      $url = 'href="'.$url.'"';
    }

    $newLink = '
      <a
        class="flex-element-type-socialLinks__link flex-element-type-socialLinks__link--'.$socialLinksStyle.'"
        target="_blank"
        rel="noopener noreferrer"
        '.$url.'
        '.$attr.'
      >
    ';

    if ($socialLinksStyle === 'icons') {
      $newLink .= '<i class="flo-flex-icon-'.$item["type"].'"></i>';
    } else {
      $newLink .= '<span class="flex-element-type-socialLinks__item-text">'.$item["text"].'</span>';
    }

    $newLink .= '
      </a>
    ';
    $links[] = $newLink;
  }
  $fontData = getFontData($user_data, 'titleFont');
  $textTitleStyles = $fontData['style'];
  $dataFont = $fontData['dataFont'];

  $titleFinalText = $socialLinksType === 'links' ? $titleText : $titleShareText;
  $displayTitle = $enableTitle ? '<div data-font="'.$dataFont.'" style="'.$textTitleStyles.'" class="flex-element-type-socialLinks__title">'.$titleFinalText.'</div>' : '';

  $elMarkup = '
    <div class="'.$b.'__element '.$b.'__element--'.$element["id"].' '.$b.'__element--socialLinks '.$custom_classes.'" style="'.$elStyles.'">
      <div
        class="flex-element-type-socialLinks flex-element-type-socialLinks--'.$orientation.'"
        style="
          --title-color: '.getStyleKitColor($titleColor).';
          --socials-text-color: '.getStyleKitColor($socialsTextColor).';
          --socials-icon-color: '.getStyleKitColor($socialsIconColor).';
          --socials-bg-color: '.getStyleKitColor($socialsBgColor).';
          --gap: '.($gap / 16).'rem;
          --iconSize: '.($iconSize / 16).'rem;
        "
      >
        '.$displayTitle.'
        <div class="flex-element-type-socialLinks__links-wrap flex-element-type-socialLinks__links-wrap--'.$socialLinksStyleType.'">'.join($links).'</div>
      </div>
    </div>
  ';

  return $elMarkup;
}
