<?php
/**
 * @file
 * navbar.install
 */

/**
 * Implements hook_requirements().
 */
function navbar_requirements($phase) {
  $requirements = array();

  if ($phase == 'runtime') {
    $libraries = array(
      'modernizr' => 'Modernizr',
      'underscore' => 'Underscore',
      'backbone' => 'Backbone',
    );
    foreach ($libraries as $lib => $label) {
      $requirements['navbar_' . $lib] = array(
        'title' => t('Navbar: @library library', array('@library' => $label)),
        'value' => t('The @library library is not present', array('@library' => $label)),
        'severity' => REQUIREMENT_ERROR,
      );
      if (function_exists('libraries_detect')) {
        if (($library = libraries_detect($lib)) && !empty($library['installed'])) {
          $requirements['navbar_' . $lib]['value'] = t('@version (@variant)', array(
            '@version' => $library['version'],
            // @todo This is problematic, because there is no way to enforce a
            //   certain variant.
            '@variant' => _navbar_libraries_get_preferred_variant_name($library),
          ));
          $requirements['navbar_' . $lib]['severity'] = REQUIREMENT_OK;
        }
        elseif (!empty($library['error'])) {
          $requirements['navbar_' . $lib]['description'] = $library['error message'];
        }
      }
    }
  }

  return $requirements;
}

/**
 * Update the breakpoints.
 *
 * narrow: 'only screen and (min-width: 16.5em)'
 * standard: 'only screen and (min-width: 38.125em)'
 * wide: 'only screen and (min-width: 52em)'
 *
 * The D8 Toolbar changes introduced two new breakpoints. We need to update
 * the existing Navbar installs with these two new breakpoints.
 */
function navbar_update_7000() {
  // Increase the weight of the wide breakpoint.
  $wide = breakpoints_breakpoint_load('wide', 'navbar', 'module');
  $wide->weight = 2;
  breakpoints_breakpoint_save($wide);
  // Add a breakpoint for narrow screens.
  $narrow = breakpoints_breakpoint_load('narrow', 'navbar', 'module');
  if (empty($narrow)) {
    $breakpoint = new stdClass();
    $breakpoint->disabled = FALSE;
    $breakpoint->api_version = 1;
    $breakpoint->name = 'narrow';
    $breakpoint->breakpoint = 'only screen and (min-width: 16.5em)';
    $breakpoint->source = 'navbar';
    $breakpoint->source_type = 'module';
    $breakpoint->status = 1;
    $breakpoint->weight = 0;
    $breakpoint->multipliers = array();
    breakpoints_breakpoint_save($breakpoint);
  }
  $standard = breakpoints_breakpoint_load('standard', 'navbar', 'module');
  if (empty($standard)) {
    // Add a breakpoint for standard screens.
    $breakpoint = new stdClass();
    $breakpoint->disabled = FALSE;
    $breakpoint->api_version = 1;
    $breakpoint->name = 'standard';
    $breakpoint->breakpoint = 'only screen and (min-width: 38.125em)';
    $breakpoint->source = 'navbar';
    $breakpoint->source_type = 'module';
    $breakpoint->status = 1;
    $breakpoint->weight = 1;
    $breakpoint->multipliers = array();
    breakpoints_breakpoint_save($breakpoint);
  }
}

/**
 * Remove the dependency on the Breakpoints module.
 */
function navbar_update_7001() {
  // Try to load the breakpoints module first. There is no guarantee that it is
  // loaded.
  if (drupal_load('module', 'breakpoints')) {
    foreach (breakpoints_breakpoint_load_all() as $breakpoint) {
      $identifiers = explode('.', $breakpoint->machine_name);
      // Delete all of the breakpionts associated with the navbar.
      if ($identifiers[1] === 'module' && $identifiers[2] === 'navbar') {
        breakpoints_breakpoint_delete($breakpoint);
      }
    }
  }
}
