<?php

include_once('debut_media.features.inc');

/**
 * Implements hook_menu_alter().
 *
 * Convert the 'file/add' item to a local action.
 */
function debut_media_menu_alter(&$items) {
  if (isset($items['file/add'])) {
    $items['file/add']['type'] = MENU_LOCAL_ACTION;
  }
}

/**
 * Implements hook_menu_link_alter().
 *
 * Set the file/% link to hidden to avoid navigation menu error,
 * see http://drupal.org/node/1018614.
 *
 * @todo: remove when http://drupal.org/node/1018614 is fixed.
 */
function debut_media_menu_link_alter(&$item) {
  if ($item['link_path'] == 'file/%') {
    $item['hidden'] = TRUE;
  }
}

/**
 * Implements hook_entity_info_alter().
 *
 * Enable custom settings for the 'full' file view mode.
 * See http://drupal.org/node/1291428.
 */
function debut_media_entity_info_alter(&$entity_info) {
  if (isset($entity_info['file']['view modes']['full'])) {
    $entity_info['file']['view modes']['full']['custom settings'] = TRUE;
  }
}

/**
 * Implements hook_image_styles_alter().
 *
 * Restore previous dimensions of media_thumbnail (was small_thumbnail).
 */
function debut_media_image_styles_alter(&$styles) {
  // Check that we only affect a default style.
  if (isset($styles['media_thumbnail']['storage']) && $styles['media_thumbnail']['storage'] == IMAGE_STORAGE_DEFAULT) {
    $styles['media_thumbnail']['label'] = t('Media thumbnail (180x180)');
    foreach($styles['media_thumbnail']['effects'] as $key => $effect) {
      if ($effect['name'] == 'image_scale_and_crop') {
        $styles['media_thumbnail']['effects'][$key]['data'] = array('width' => 180, 'height' => 180);
      }
    }
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function debut_media_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'entity_info_alter') {
    // Move debut_media_entity_info_alter() to the end of the list.
    // module_implements() iterates through $implementations with a foreach
    // loop which PHP iterates in the order that the items were added, so to
    // move an item to the end of the array, we remove it and then add it.
    $group = $implementations['debut_media'];
    unset($implementations['debut_media']);
    $implementations['debut_media'] = $group;
  }
}

/**
 * Implements hook_jcarousel_options_alter(().
 *
 * Swap in setup and reload callbacks to make the media carousel responsive.
 */
function debut_media_jcarousel_options_alter(&$options, $class_name) {
  static $added = FALSE;

  if (!empty($options['view_options']['view_name']) && $options['view_options']['view_name'] == 'media') {
    foreach (array('setupCallback', 'reloadCallback') as $callback) {
      if (!isset($options[$callback])) {
        $options[$callback] = 'Drupal.debutMedia.' . $callback;
        if (!$added) {
          drupal_add_css(drupal_get_path('module', 'debut_media') . '/debut_media.css', array('weight' => 10));
          drupal_add_js(drupal_get_path('module', 'debut_media') . '/debut_media.js');
          $added = TRUE;
        }
      }
    }
  }
}

/**
 * Implementation of Styles module hook_styles_default_styles().
 */
function debut_media_styles_default_styles() {
  return array(
    'file' => array(
      'styles' => array(
        'medium_large' => array(
          'label' => t('Medium large'),
          'description' => t('A medium large format of the media.'),
        ),
        'small_square_thumbnail' => array(
          'label' => t('Small square thumbnail'),
          'description' => t('A small, square format of the media.'),
        ),
      ),
    ),
  );
}

/**
 * Implementation of Styles module hook_styles_default_presets_alter().
 */
function debut_media_styles_default_presets_alter(&$presets) {
  foreach (array_keys(debut_media_image_default_styles()) as $image_style) {
    // Cover the containers defined by file_styles and media_youtube.
    foreach (array('application', 'image', 'audio', 'video', 'default', 'media_youtube', 'media_vimeo') as $type) {
      // Ensure another module hasn't removed the container.
      if (isset($presets['file']['containers'][$type])) {
        $presets['file']['containers'][$type]['styles'][$image_style] = array(
          'default preset' => 'medium_large',
        );
        $presets['file']['containers'][$type]['presets'][$image_style] = array(
          array(
            'name' => 'image_style',
            'settings' => array(
              'image_style' => $image_style,
            ),
          ),
          array(
            'name' => 'thumbnail',
            'settings' => array(),
          ),
        );
      }
    }
  }
}

/**
 * Implements hook_file_default_displays_alter().
 *
 * Clone settings for 'media_large' to new 'full' view mode and fix weighting
 * of video preview images.
 *
 * @todo: remove 'media_large' to 'full' cloning once media provider modules
 * catch up. See http://drupal.org/node/1291428.
 */
function debut_media_file_default_displays_alter(&$items) {
  // Clone settings for 'media_large' to new 'full' view mode.
  $info = entity_get_info('file');
  $bundles = array_keys($info['bundles']);
  $view_modes = array_keys($info['view modes']);

  foreach ($items as $key => $data) {
    list($file_type, $view_mode, $formatter_name) = explode('__', $key);
    if ($view_mode == 'media_large') {
      $display_name = implode('__', array($file_type, 'full', $formatter_name));
      if (!isset($items[$display_name])) {
        $items[$display_name] = clone($data);
        $items[$display_name]->name = $display_name;
      }
    }
  }
  // Fix weighting of video preview images.
  foreach (array('video__preview__media_vimeo_image', 'video__preview__media_youtube_image') as $key) {
    if (isset($items[$key])) {
      $items[$key]->weight = -10;
    }
  }
}
