<?php

/**
 * @file media_vimeo/media_vimeo.module
 *
 * Media: Vimeo provides a stream wrapper and formatters for videos provided
 * by Vimeo, available at http://vimeo.com/.
 *
 * @TODO:
 * Tie in Vimeo API.
 * Allow editors to search for videos to display on the browser.
 * Allow editors to put in a vimeo username to display on the browser.
 * Allow editors to log in w/ their credentials.
 * Allow editors to upload videos to Vimeo.
 */

// A registry of variable_get defaults.
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'media_vimeo') . '/includes/media_vimeo.variables.inc';

// Hooks and callbacks for integrating with File Entity module for display.
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'media_vimeo') . '/includes/media_vimeo.formatters.inc';

/**
 * Implements hook_media_internet_providers().
 */
function media_vimeo_media_internet_providers() {
  $path = drupal_get_path('module', 'media_vimeo');
  return array(
    'MediaInternetVimeoHandler' => array(
      'title' => t('Vimeo'),
      'image' => $path . '/images/stream-vimeo.png'
    ),
  );
}

/**
 * Implements hook_stream_wrappers().
 */
function media_vimeo_stream_wrappers() {
  return array(
    'vimeo' => array(
      'name' => t('Vimeo videos'),
      'class' => 'MediaVimeoStreamWrapper',
      'description' => t('Videos provided by Vimeo.'),
      'type' => STREAM_WRAPPERS_READ_VISIBLE,
    ),
  );
}

/**
 * Implements hook_theme().
 */
function media_vimeo_theme($existing, $type, $theme, $path) {
  return array(
    'media_vimeo_video' => array(
      'variables' => array('uri' => NULL, 'options' => array()),
      'file' => 'media_vimeo.theme.inc',
      'path' => $path . '/includes/themes',
      'template' => 'media-vimeo-video',
    ),
  );
}

/**
 * Implements hook_media_parse().
 *
 * @todo This hook should be deprecated. Refactor Media module to not call it
 *   any more, since media_internet should be able to automatically route to the
 *   appropriate handler.
 */
function media_vimeo_media_parse($embed_code) {
  $handler = new MediaInternetVimeoHandler($embed_code);
  return $handler->parse($embed_code);
}

/**
 * Implements hook_media_format_form_prepare_alter().
 */
function media_vimeo_media_format_form_prepare_alter(&$form, &$form_state, $media) {
  if ($form['#media']->filemime == 'video/vimeo') {
    $settings = array('autosubmit' => TRUE);
    drupal_add_js(array('media_format_form' => $settings), 'setting');
  }
}

 /**
 * Implements hook_file_mimetype_mapping_alter().
 */
function media_vimeo_file_mimetype_mapping_alter(&$mapping) {
  $mapping['mimetypes'][] = 'video/vimeo';
}

/**
 * Implements hook_ctools_plugin_api().
 */
function media_vimeo_ctools_plugin_api($owner, $api) {
  static $api_versions = array(
    'file_entity' => array(
      'file_default_displays' => 1,
    ),
  );
  if (isset($api_versions[$owner][$api])) {
    return array('version' => $api_versions[$owner][$api]);
  }
}
