<?php

/**
 * @file
 * Contains social_font.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\file\Entity\File;
use Drupal\social_font\Entity\Font;

/**
 * Implements hook_help().
 */
function social_font_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    case 'help.page.social_font':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Social font module.') . '</p>';

      return $output;

    default:
  }
}

/**
 * The font renderer.
 *
 * @param int $font_id
 *   The id of the font.
 *
 * @return string
 *   Returns a string.
 */
function social_font_render($font_id = NULL) {

  if ($font_id == NULL) {
    $system_theme_settings = \Drupal::configFactory()->get('system.theme')->get('default');
    if (array_key_exists('socialbase', \Drupal::service('theme.manager')->getActiveTheme()->getBaseThemeExtensions())) {
      $config = \Drupal::config($system_theme_settings . '.settings');
      $font_id = $config->get('font_primary');
    }
  }

  $css = '';

  // Use empty() instead of a NULL comparison to guard for malformed
  // configuration.
  if (empty($font_id)) {
    return $css;
  }

  $font = Font::load($font_id);

  if ($font instanceof Font) {
    // Fontname.
    $fontname = $font->getName();
    // Fallback.
    $fallback = social_font_fallback_render($font->get('field_fallback')->getString());
    // Defaults.
    $fonts = [];

    // Amount of files 0.
    if (count($font->get('field_fonts')) === 0) {
      $css = "@font-face { font-family: '" . $fontname . "'; } body { font-family: '" . $fontname . "', " . $fallback . " !important; }";
    }
    else {
      $css = "@font-face { font-family: '" . $fontname . "'; ";
      // Loop through the font files.
      foreach ($font->get('field_fonts')->getValue() as $files) {
        /** @var \Drupal\file\Entity\File $file */
        if ($file = File::load($files['target_id'])) {
          $url = \Drupal::service('file_url_generator')->generateAbsoluteString($file->get('uri')->value);
          $fonts[] = $url;
        }
      }
      $css .= social_font_fontfiles_render($fonts);
      $css .= "} body { font-family: '" . $fontname . "', " . $fallback . " !important; } ";
      // font-weight is reset because uploaded fonts don't yet support
      // multiple font-weights which breaks strong tags.
      // @todo See issue #3045765.
      $css .= "strong { font-weight: initial !important; }";
    }
  }

  return $css;
}

/**
 * The fallback on the font renderer.
 *
 * @param string $fallback_value
 *   The string value for the fallback.
 *
 * @return string
 *   Returns a string.
 */
function social_font_fallback_render($fallback_value) {
  if ($fallback_value === '0') {
    return 'serif';
  }
  return 'sans-serif';
}

/**
 * The font file renderer.
 *
 * @param array $fonts
 *   Array with fonts.
 *
 * @return string
 *   Returns a string.
 */
function social_font_fontfiles_render(array $fonts) {
  $fontstring = '';
  foreach ($fonts as $key => $font) {
    if (pathinfo($font, PATHINFO_EXTENSION) == 'eot') {
      $fontstring .= "src: url('" . $font . "'), url('" . $font . "#iefix') format('embedded-opentype');";
      // Remove EOT.
      unset($fonts[$key]);
    }
  }

  // Loop 2.
  foreach ($fonts as $key => $font) {
    $format = pathinfo($font, PATHINFO_EXTENSION);
    if (pathinfo($font, PATHINFO_EXTENSION) == 'ttf') {
      $format = 'truetype';
    }
    $fontstring .= "src: url('" . $font . "') format('" . $format . "');";
  }
  return $fontstring;

}
