<?php

/**
 * @file
 * Defines common functionality for intercept_ils.
 */

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Implements hook_help().
 */
function intercept_ils_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the intercept_ils module.
    case 'help.page.intercept_ils':
      $output = '';
      $output .= '<h3>' . new TranslatableMarkup('About') . '</h3>';
      $output .= '<p>' . new TranslatableMarkup('Provides ILS integration for Intercept.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_page_attachments().
 */
function intercept_ils_page_attachments(array &$attachments) {
  $current_user = \Drupal::currentUser();
  $authmap = \Drupal::service('externalauth.authmap');
  $plugin_id = \Drupal::config('intercept_ils.settings')->get('intercept_ils_plugin', '');
  if (!empty($plugin_id) && $authdata = $authmap->getAuthdata($current_user->id(), $plugin_id)) {
    $authdata_data = unserialize($authdata['data']);
    if (isset($authdata_data)) {
      $attachments['#attached']['drupalSettings']['intercept']['user']['telephone'] = $authdata_data->PhoneNumber;
      $attachments['#attached']['drupalSettings']['intercept']['user']['email'] = $authdata_data->EmailAddress;
    }
  }

  $attachments['#cache']['contexts'][] = 'user';
  $attachments['#cache']['tags'][] = 'user:' . $current_user->id();
}

/**
 * Implements hook_entity_extra_field_info().
 */
function intercept_ils_entity_extra_field_info() {
  $extra['profile']['customer']['form']['pin'] = [
    'label' => new TranslatableMarkup('PIN'),
    'description' => new TranslatableMarkup('ILS PIN'),
    'visible' => FALSE,
  ];

  return $extra;
}

/**
 * Implements hook_preprocess_views_view_field().
 */
function intercept_ils_preprocess_views_view_field(&$variables) {
  $view = $variables['view'];
  $row = $variables['row'];
  $field = $variables['field']->field;
  // If it's the field in the customer lookup, show the latest authdata here too.
  if ($view->id() == 'intercept_customers' && $view->current_display == 'manage') {
    $uid = $row->uid;
    // Now let's find the authdata for this uid.
    if ($field == 'field_phone') {
      $authdata = intercept_base_get_authdata($uid);
      if ($authdata) {
        // Change output.
        $variables['output'] =  [
          '#markup' => $authdata->PhoneNumber
        ];
      }
    }
    elseif ($field == 'field_address') {
      $authdata = intercept_base_get_authdata($uid);
      if ($authdata) {
        // Change output.
        $address = $authdata->PatronAddresses[0]->StreetOne;
        if (!empty($authdata->PatronAddresses[0]->StreetTwo)) {
          $address .= ', ' . $authdata->PatronAddresses[0]->StreetTwo;
        }
        $address .= '<br>' . $authdata->PatronAddresses[0]->City . ', ' . $authdata->PatronAddresses[0]->State . ' ' . $authdata->PatronAddresses[0]->PostalCode;
        $variables['output'] =  [
          '#markup' => $address
        ];
      }
    }
  }
}
