<?php

/**
 * @file
 * Adds includes to Beautytips settings page and provides some built in functionality.
 * TODO: hook_theme invoke - maybe
 * Invoked include hooks: admin_info, menu_change, theme_change, form_change, menu_items
 * ex.  beautytips_textinput_info
 */

function beautytips_ui_admin_settings(&$form) {
  $items = beautytips_ui_include_invoke('beautytips', 'admin_info', $args = array());
  if (is_array($items)) {
    foreach ($items as $form_item) {
      if (count($form_item)) {
        $keys = array_keys($form_item);
        if (count($keys)) {
          foreach ($keys as $menu_item) {
            $form[$menu_item] = $form_item[$menu_item];
          }
        }
      }
    }
  }
}

/**
 *  Clear menu cache and theme registry in case beautytips help tips turned on or off
 */
function beautytips_ui_admin_submit($form, $form_state) {
  module_invoke('menu', 'rebuild');
  cache_clear_all('*', 'cache_menu', TRUE);
  drupal_theme_rebuild();
}

/**
 * Implementation of hook_menu.
 */
function beautytips_ui_menu() {
  $items = array(); 

  $menu_items = beautytips_ui_include_invoke('beautytips', 'menu_items', $args = array());
  if (is_array($menu_items)) {
    foreach ($menu_items as $menu_item) {
      if (is_array($menu_item)) {
        $path = key($menu_item);
        $items[$path] = $menu_item[$path]; 
      }
    }
  } 
  return $items;
}

/**
 * Determine whether an include implements a hook, cf. module_hook.
 *
 * @param $tooltip
 *   The name of the tooltip file (without the .inc extension), such as 'youtube' or 'google'.
 * @param $hook
 *   The name of the hook (e.g. "thumbnail", "settings", etc.).
 * @return
 *   TRUE if the tooltip is loaded and the hook is implemented.
 */
function beautytips_ui_include_hook($module, $tooltip, $hook) {
  return function_exists($module .'_'. $tooltip .'_'. $hook);
}

/**
 * Invoke hook in a particular include.
 *
 * @param $module
 *  the helper module
 * @param $tooltip
 *   The name of the tooltip (without the .inc extension).
 * @param $hook
 *   The name of the hook (e.g. "menu_change", "form_change", etc.).
 * @param ...
 *   Arguments to pass to the hook implementation.
 * @return
 *   The return value of the hook implementation.
 */
function beautytips_ui_include_invoke($module, $hook, $args) {
  $includes = beautytips_ui_include_list();
  foreach ($includes as $tooltip) {
    $function = $module .'_'. $tooltip .'_'. $hook;
    $params[] = beautytips_ui_include_hook($module, $tooltip, $hook) ? call_user_func_array($function, $args) : NULL;
  }
  return $params;
}

/**
 * Maintains a list of all loaded include files.
 *
 * @param $file
 *   -------------------------
 * @return
 *   An array of all loaded includes. (w/o appended .inc)
 */
function beautytips_ui_include_list() {
  $files = array(
    'drupal_help.inc' => 'drupal_help',
    'textinput.inc' => 'textinput',
  );
  foreach ($files as $file => $name) {
    module_load_include('inc', 'beautytips', 'includes/'. $name);
  }

  return $files;
}

/**
 * Implementation of hook_form_alter.
 * Adds beautytips for textareas and textfields
 */
function beautytips_ui_form_alter(&$form, $form_state, $form_id) {
  beautytips_ui_include_invoke('beautytips', 'form_change', 
    array(
      'form' => &$form,
      'form_state' => $form_state,
      'form_id' => $form_id,
    )
  );   
}

/**
 * Implementation of hook_menu_alter().
 */
function beautytips_ui_menu_alter(&$items) {
  beautytips_ui_include_invoke('beautytips', 'menu_change', array('items' => &$items));
}

/**
 * Implementation of hook_theme_registry_alter().
 */
function beautytips_ui_theme_registry_alter(&$theme_registry) {
  beautytips_ui_include_invoke('beautytips', 'theme_change', array('theme_registry' => &$theme_registry));
}
