<?php
/**
 * @file video_filter_field.module
 */

/**
 * Implements hook_field_info().
 */
function video_filter_field_field_info() {
  return array(
    'video_filter' => array(
      'label' => t('Video Filter'),
      'description' => t('This field stores URLs and settings to be '
                       . 'handled by the Video Filter module.'),
      'settings' => array(),
      'instance_settings' => array(
        'max_height' => 400,
        'max_width' => 400,
        'autoplay' => 0,
      ),
      'default_widget' => 'video_filter_field_default',
      'default_formatter' => 'video_filter_field_default',
    ),
  );  
}

/**
 * Implements magic callback / psuedo-hook mymodule_field_schema().
 */
function video_filter_field_field_schema($field) {
  if ($field['type'] == 'video_filter') {
    $schema['columns']['url'] = array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
    );
    $schema['columns']['height'] = array(
      'type' => 'int',
      'not null' => FALSE,
    );
    $schema['columns']['width'] = array(
      'type' => 'int',
      'not null' => FALSE,
    );
    $schema['columns']['autoplay'] = array(
      'type' => 'int',
      'not null' => FALSE,
    );

    $schema['indexes'] = array(
      'height' => array('height'),
      'width' => array('width'),
    );

    return $schema;
  }  
}

/**
 * Implements magic callback / psuedo-hook mymodule_field_is_empty().
 */
function video_filter_field_field_is_empty($item, $field) {
  if ($field['type'] == 'video_filter') {
    if (empty($item['url']) && empty($item['height']) 
      && empty($item['width']) && empty($item['autoplay'])) 
    {
      return TRUE; 
    } else {
      return FALSE; 
    }
  }
}

/**
 * Implements magic callback / psuedo-hook mymodule_field_settings_form().
 */
/*
function video_filter_field_settings_form($field, $instance, $has_data) {
  if ($field['type'] == 'video_filter') {
    $settings = $field['setings'];  
    $form = '';
    return $form; 
  }
}
// */

/**
 * Implements hook_field_validate().
 */
function video_filter_field_field_validate($obj_type, $object, $field,
                                           $instance, $langcode, &$items,
                                           &$errors) {
  if ($field['type'] == 'video_filter') {
    foreach ($items as $delta => $item) {
      // Test URL. 
      if (!empty($item['url'])) {
        //$errors[$field['field_name']][$langcode][$delta][] = array(
        //  'error' => 'video_filter_url',
        //  'message'  => t('Video Filter Field: Invalid video URL'),
        //);
        // If URL isn't empty, we need a height.
        if (empty($item['height']) || !is_numeric($item['height'])) {
          $errors[$field['field_name']][$langcode][$delta][] = array( 
            'error' => 'video_filter_height',
            'message' => t('Video Filter Field: Invalid video height'),
          );
        }
        // If URL isn't empty, we need a width.
        if (empty($item['width']) || !is_numeric($item['width'])) {
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => 'video_filter_width',
            'message' => t('Video Filter Field: Invalid video width'),
          );
        }
      } 
      // URL field is empty. Set everything else to empty.
      else {
        unset($items);    
        unset($object->field_media_url);    
      }
    } 
  }  
}


/**
 * Implements hook_field_widget_info().
 */
function video_filter_field_field_widget_info() {
  return array(
    'video_filter' => array(
      'label' => t('Video Filter'),
      'description' => t('Enable user to determine height, width, and autoplay settings'),
      'field types' => array('video_filter'),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function video_filter_field_field_widget_form(&$form, &$form_state, $field,
                                        $instance, $langcode, $items,
                                        $delta, $element) {
  $base = $element;   
  //dsm($element);
  //dsm($instance,' instance');
  
  if ($instance['widget']['type'] == 'video_filter') {
    $element['url'] = array(
      '#type' => 'textfield',
      '#title' => t('Video URL'),
      '#default_value' => isset($items[$delta]['url']) ? 
                          $items[$delta]['url'] : NULL,
      '#weight' => 0,
      ) + $base;
    $element['height'] = array(
      '#type' => 'textfield',
      '#title' => t('Height'),
      '#default_value' => isset($items[$delta]['height']) ? 
                          $items[$delta]['height'] : NULL,
      '#weight' => 1,
      ) + $base;
    $element['width'] = array(
      '#type' => 'textfield',
      '#title' => t('Width'),
      '#default_value' => isset($items[$delta]['width']) ? 
                          $items[$delta]['width'] : NULL,
      '#weight' => 2,
    ) + $base;
    $element['autoplay'] = array(
      '#type' => 'checkbox',
      '#title' => t('Autoplay'),
      '#default_value' => isset($items[$delta]['autoplay']) ? 
                          $items[$delta]['autoplay'] : FALSE,
      '#weight' => 3,
    ) + $base;
  }
  return $element;
}

/**
 * Implements hook_field_formatter_info().
 */
function video_filter_field_field_formatter_info() { 
  return array(
    'video_filter_field_default' => array(
      'label' => t('Video Filter'),
      'field types' => array('text', 'video_filter'),
    ),
  );
} 

/**
 * Implements hook_field_formatter_view().
 */
function video_filter_field_field_formatter_view($obj_type, $object, $field, 
                                           $instance, $langcode, $items, 
                                           $display) { 

/*
dsm($obj_type, 'obj_type');
dsm($object, 'object');
dsm($field, 'field');
dsm($instance,'instance');
dsm($langcode,'langcode');
dsm($display,'display');
dsm($items,'items');
// */

  $elements = array();
  foreach ($items as $delta => $item) {
    $url = url($item['url']);
    $text = "[video:$url]";

    $elements[$delta] = array(
      '#theme' => 'video_filter_field_default_formatter',
      '#item' => video_filter_field_process($text, $item),
    );
  }

  return $elements;
}

/**
 * Implements hook_theme().
 */
function video_filter_field_theme() {
  return array(
    'video_filter_field_default_formatter' => array(
      'variables' => array('item' => NULL),
    ),
  );
  
}

/**
 * Returns HTML for a video_filter_field_formatter
 */
function theme_video_filter_field_default_formatter($variables) {
  $output = '<div class="video-filter-field">' . $variables['item'] . '</div>';

  return $output;
}

/**
 * Implement video_filter processing from video_filter module.
 * 
 * @see _video_filter_process()
 */
function video_filter_field_process($text, $video) {
  $filter = new stdClass(); 
  //$filter->format = ''; 
  //$filter->module = 'video_filter';
  //$filter->name = 'video_filter'; 
  //$filter->weight = -1;
  $filter->status = 1;
  $filter->settings = array(
    'video_filter_width' => 400,
    'video_filter_height' => 400,
    'video_filter_autoplay' => 0,
    'video_filter_related' => 1,
  );

    $text = (string)$text; // Make sure $text is a string. 
    if (preg_match_all('/\[video(\:(.+))?( .+)?\]/isU', $text, $matches_code)) {
    foreach ($matches_code[0] as $ci => $code) {
      $video = (array)$video + array(
        'source' => $matches_code[2][$ci],
        'autoplay' => $filter->settings['video_filter_autoplay'],
        'related' => $filter->settings['video_filter_related'],
      );

      // Pick random out of multiple sources separated by ','
      if (strstr($video['source'], ',')) {
        $sources          = explode(',', $video['source']);
        $random           = array_rand($sources, 1);
        $video['source']  = $sources[$random];
      }

      // Load all codecs
      $codecs = module_invoke_all('codec_info');

      // Find codec
      foreach ($codecs AS $codec_name => $codec) {
        if (!is_array($codec['regexp'])) {
          $codec['regexp'] = array($codec['regexp']);
        }

        // Try different regular expressions
        foreach ($codec['regexp'] as $delta => $regexp) {
          if (preg_match($regexp, $video['source'], $matches)) {
            $video['codec'] = $codec;
            $video['codec']['delta'] = $delta;
            $video['codec']['matches'] = $matches;
            $video['codec']['codec_name'] = $codec_name; // used in theme function
            $video['codec']['control_bar_height'] = 0; // default
            break 2;
          }
        }
      }

      // Codec found
      if (isset($video['codec'])) {
        // Override default attributes
        if ($matches_code[3][$ci] && preg_match_all('/\s+([a-zA-Z_]+)\:(\s+)?([0-9a-zA-Z\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
          foreach ($matches_attributes[0] AS $ai => $attribute) {
            $video[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
          }
        }

        // Use configured ratio if present, use that from the codec otherwise
        $ratio = 1;
        if (isset($video['ratio']) && preg_match('/(\d+)\/(\d+)/', $video['ratio'], $tratio) ) {
          //validate given ratio parameter
          $ratio = $tratio[1] / $tratio[2];
        }
        else {
          $ratio = $video['codec']['ratio'];
        }

        // Sets video width & height after any user input has been parsed.
        // First, check if user has set a width.
        if (isset($video['width']) && !isset($video['height'])) {
          $video['height'] = variable_get('video_filter_height_'.$format, 400);
        }
        // Else, if user has set height.
        elseif (isset($video['height']) && !isset($video['width'])) {
          $video['width'] = $video['height'] * $ratio;
        }
        // Maybe both?
        elseif (isset($video['height']) && isset($video['width'])) {
          $video['width'] = $video['width'];
          $video['height'] = $video['height'];
        }
        // Fall back to defaults.
        elseif (!isset($video['height']) && !isset($video['width'])) {
          $video['width'] = $filter->settings['video_filter_width'];
          $video['height'] = $filter->settings['video_filter_height'];
        }

        // Default value for control bar height
        $control_bar_height = 0;
        if (isset($video['control_bar_height'])) {
          // respect control_bar_height option if present
          $control_bar_height = $video['control_bar_height'];
        }
        elseif (isset($video['codec']['control_bar_height'])) {
          // respect setting provided by codec otherwise
          $control_bar_height = $video['codec']['control_bar_height'];
        }

        // Resize to fit within width and height repecting aspect ratio
        if ($ratio) {
          $scale_factor = min(array(($video['height']-$control_bar_height), $video['width']/($ratio)));
          $video['height'] = round($scale_factor + $control_bar_height);
          $video['width'] = round($scale_factor * $ratio);
        }

        $video['autoplay'] = (bool) $video['autoplay'];
        $video['align'] = (isset($video['align']) && in_array($video['align'], array('left', 'right', 'center'))) ? $video['align'] : NULL;

        if (is_callable($video['codec']['callback'], FALSE)) {
          $replacement = call_user_func($video['codec']['callback'], $video);
        }
        else {
          // Invalid callback
          $replacement = '<!-- VIDEO FILTER - INVALID CALLBACK IN: ' . $pattern . ' -->';
        }
      // Invalid format
      }
      else {
        $replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
      }

      $text = str_replace($code, $replacement, $text);
    }
  }

  return $text;
}

/**
 * Test support for URL.
 *
 * @todo Implement hook_codec_info() to...
 * - add support for whatever media module uses as an alternative to video_filter module.
 * - add support for http://embed.ly/.
 * 
 * @param $url
 *  string, URL which may or may not include media of some kind.
 * 
 * @return
 *  TRUE, video_filter recognizes and supports media at this URL
 *  FALSE, video_filter doesn't recognize it. This could be either because
 *    the URL doesn't actually provide media or because we just doen't have
 *    the codec for it.
 */
function video_filter_field_url_is_supported($url) {
  // Load all codecs
  module_load_include('inc', 'video_filter', 'video_filter.codecs');
  $codecs = module_invoke_all('codec_info');

  // Find codec
  foreach ($codecs AS $codec_name => $codec) {
    if (!is_array($codec['regexp'])) {
      $codec['regexp'] = array($codec['regexp']);
    }

    // Try different regular expressions
    $video = array();
    foreach ($codec['regexp'] as $delta => $regexp) {
      if (preg_match($regexp, $url)) {
        $video['codec'] = TRUE;
      }
      break;
    }
    break;
  }
  // If we found a codec, this URL is supported. 
  $is_supported = (isset($video['codec'])) ? TRUE : FALSE;

  return $is_supported;
}
