<?php

/**
 * Implements hook_theme().
 */
function quicktabs_tabstyles_theme() {
  return array(
    'quicktabs_style_options' => array(
      'render element' => 'quicktabs_tabstyle',
    ),
  );
}

/**
 * Implements hook_menu().
 */
function quicktabs_tabstyles_menu() {
  $items['admin/structure/quicktabs/styles'] = array(
    'title' => 'Styles',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('quicktabs_tabstyles_styles'),
    'access arguments' => array('administer quicktabs'),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Callback function for admin/structure/quicktabs/styles. The style chooser form.
 */
function quicktabs_tabstyles_styles() {
  $options = array();
  $styles = module_invoke_all('quicktabs_tabstyles');
  // The keys used for options must be valid html id-s.
  // Removing the css file path, because that can't be used.
  foreach ($styles as $style) {
    $options[$style] = $style;
  }
  ksort($options);

  $form['quicktabs_tabstyle'] = array(
    '#type' => 'radios',
    '#title' => t('Quicktab styles'),
    '#options' => array('nostyle' => t('No style')) + $options,
    '#default_value' => variable_get('quicktabs_tabstyle', 'nostyle'),
    '#description' => t('Select the default style for quicktabs.'),
    '#attributes' => array('class' => array('quicktabs-tabstyles', 'clear-block')),
    '#theme' => 'quicktabs_style_options',
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

/**
 * Submit handler for QuickTabs styles.
 */
function quicktabs_tabstyles_styles_submit($form, &$form_state) {
  variable_set('quicktabs_tabstyle', $form_state['values']['quicktabs_tabstyle']);
  drupal_set_message(t('The default quicktab style has been saved.'));
}

/**
 * Theme function for quicktabs style radio options.
 *
 * @ingroup themeable
 */
function theme_quicktabs_style_options($variables) {
  $style_element = $variables['quicktabs_tabstyle'];
  $markup = '';

  $tabs = array(
    array('title' => t('One'), 'contents' => array('#markup' => t('First tab')), 'weight' => 0),
    array('title' => t('Two'), 'contents' => array('#markup' => t('Second tab')), 'weight' => 1),
    array('title' => t('Three'), 'contents' => array('#markup' => t('Third tab')), 'weight' => 2)
  );

  $options = array('renderer' => 'quicktabs', 'hide_empty_tabs' => 0, 'ajax' => 0);
  // Preview for each style.
  foreach (element_children($style_element) as $style) {
    $element = $style_element[$style];
    $options['style'] = $style;
    $quicktabs = quicktabs_build_quicktabs(drupal_strtolower($style), $options, $tabs);
    $preview = '<div class="quicktabs-preview">'. drupal_render($quicktabs['content']) .'</div>';
    $element['#description'] = t('%style preview', array('%style' => $element['#title'])) .':<br />'. $preview;
    $markup .= drupal_render($element);
  }
  $build = array(
    'style' => array('#markup' => $markup),
    '#attached' => array('css' => array(drupal_get_path('module', 'quicktabs_tabstyles') . '/css/quicktabs-tabstyles-admin.css')),
  );
  return drupal_render($build);
}


/**
 * Implements hook_quicktabs_tabstyles().
 */
function quicktabs_tabstyles_quicktabs_tabstyles() {
  $tabstyles_directory = drupal_get_path('module', 'quicktabs_tabstyles') . '/tabstyles';
  $files = file_scan_directory($tabstyles_directory, '/\.css$/');
  $tabstyles = array();
  foreach ($files as $file) {
    // Skip RTL files.
    if (!strpos($file->name, '-rtl')) {
      $tabstyles[$file->uri] = drupal_ucfirst($file->name);
    }
  }
  return $tabstyles;
}