<?php

/**
 * @file
 * Theming functions for the imagetarget question type.
 */

/**
 * Preprocess the alternative answers to the creation form
 * @param $form
 */
function theme_imagetarget_creation_form(&$form) {
  $path = drupal_get_path('module', 'imagetarget');
  jquery_ui_add(array('ui.draggable', 'ui.droppable', 'ui.sortable'));
  _imagetarget_add_js('imagetarget-creation');

  $output = '';

  $output .= drupal_render($form['upload']);
  $output .= drupal_render($form['upload_now']);
  $output .= '<div style="clear: both; padding-bottom: 20px;"></div>';
  $output .= drupal_render($form['imagetarget_image']);

  if (isset($form['targets'])) {
    $rows = array();
    foreach ($form['targets'] as $identifier => $target) {
      if (intval($identifier) > 0) {
        $row = array();
        $row[] = drupal_render($target['selected_region']) . drupal_render($target['region']);
        $row[] = drupal_render($target['targettext']);
        $row[] = drupal_render($target['score_if_correct']);
        $row[] = drupal_render($target['score_if_not_correct']);
        $rows[] = $row;
      }
    }
    $header[] = array('data' => t('Region'));
    $header[] = array('data' => t('Description'));
    $header[] = array('data' => t('Score if correct'));
    $header[] = array('data' => t('Score if not correct'));
    $output .= theme('table', $header, $rows, array(), NULL, TRUE);
  }

  $output .= drupal_render($form['feedback']);

  // TODO : Add alternatives functionality
  //$output .= drupal_render($form['imagetarget_add_alternative']);

  return $output;
}

/**
 * Theme the answer part of the node view
 *
 * @param array $alternatives
 *  Array of alternatives. Each alternative is also an array with all the
 *  data for each alternative.
 * @param boolean $show_correct
 *  True if the user is allowed to view the solution
 */
function theme_imagetarget_answer_node_view($alternatives, $show_correct, $fid) {
  $header = array('');
  $rows = array();
  $answers = array();

  if ($show_correct) {
    foreach ($alternatives as $i => $short) {
      
      if ($show_correct) {
        $answers[$i] = array(
          'region' => $short,
        );
      }   

      $rows[] = array(
        $i . '. ' . check_plain($short['targettext']),
      );
    }
  }

  $output = theme('imagetarget_image', $fid, $answers, $show_correct);
  $output .= theme('table', $header, $rows);
  return $output;
}

/**
 * Theme function for the imagetarget report.
 * This appears at the end of a quiz and shows the user what they got right and wrong
 *
 * @param int $fid
 *   The file fid of the imagetarget image
 */
function theme_imagetarget_response($fid, array $answers = array(), $feedback = '', $showcorrect = FALSE, $showfeedback = FALSE) {
  $rows = array();
  $path = drupal_get_path('module', 'imagetarget') . '/theme/images';

  foreach ($answers as $identifier => $answer) {
    $cols = array();

    $cols[] = array(
      'data' => "<span>{$identifier}. </span>",
      'width' => 35,
    );

    if ($showcorrect) {
      $info = $answer['correct']
        ? theme('image', "{$path}/correct.png", t('Correct'), t('This answer is correct'), array('class' => 'feedback-icon'))
        : theme('image', "{$path}/wrong.png",  t('Wrong'), t('This answer is wrong'), array('class' => 'feedback-icon'));

      $cols[] = array(
        'data' => $info,
        'width' => 35,
        'rowspan' => 1,
        'class' => 'selector-td',
      );
    }

    $cols[] = array(
      'data' => $answer['targettext'],
    );

    $rows[] = $cols;
  }

  if ($showfeedback) {
    $rows[] = array( array( 'colspan' => 3, 'data' => '<strong>' . t('Feedback:') . '</strong><br />' . $feedback ));
  }

  $table = theme('table', NULL, $rows);
  $img = is_numeric($fid) ? theme('imagetarget_image', $fid, $answers, $showcorrect, $showfeedback) : '';
  return $img . $table;
}

/**
 * Theme the img tag for the imagetarget image
 * @param int $fid
 *    The fid of the actual uploaded image. If imagecache is setup, the img returned will be the image cached one
 * @param array $answers
 *    One for each answer with the following keys
 *      'region' - An answer region on the image: ARRAY [INT 'leftpx', INT 'width', INT 'top', INT 'height']
 *      'target' (optional) - Where to place the target marker: ARRAY [INT 'top', INT 'left'] relative to the top left of the image
 * @param boolean $showpoints
 *    TRUE to display the answers (regions)
 * @param boolean $showfeedback
 *    TRUE to display the feedback (note this is not implemented, only relevant when there is separate feedback per answer)
 * @return string
 *    HTML displaying the imagetarget image and (optionally) answer regions and user targets
 */
function theme_imagetarget_image($fid, array $answers = array(), $showpoints = FALSE, $showfeedback = FALSE) {
  if (is_numeric($fid) && $fid > 0) {
    $file = imagetarget_file_load($fid);
    $url = imagetarget_get_imagecache_preset() ? imagecache_create_url(imagetarget_get_imagecache_preset()->presetname, $file->filepath) : url($file->filepath);

    $output = '<div id="imagetarget_image" style="position: relative; height: ' . imagetarget_get_image_height($fid) . 'px;">';  
    $output .= '<img src="' . $url . '" style="position: absolute;" />';

    foreach ($answers as $identifier => $answer) {
      // Region
      if ($showpoints) {
        $region = $answer['region'];
        $output .= theme('imagetarget_region', $region['top'], $region['leftpx'], $region['width'], $region['height'], $identifier);
      }

      // target
      if (isset($answer['target'])) {
        $target = $answer['target'];
        $output .= theme('imagetarget_target', $target['top'], $target['leftpx'], $identifier);
      }
    }
    $output .= '</div>';
    return $output;
  }
  return '';
}

/**
 * Theme template preprocess for the targets that get dragged onto the form
 * Override this function in your template if you want to change the targets
 * Be sure to offset the top and left co-ordinates by half the width or height
 * of the target image so it gets centered on the area the drag onto
 * e.g. The default target used by the module is 32px x 32px so top and left
 * co-ordinates are reduced by 16
 */
function template_preprocess_imagetarget_target(&$vars) {
  $path = drupal_get_path('module', 'imagetarget') . '/theme/images';
  $vars['top'] = $vars['top'] - 16;
  $vars['left'] = $vars['left'] - 16;
  $vars['width'] = 32;
  $vars['height'] = 32;
  $vars['imgurl'] = url($path . '/target.png');
}

/**
 * Theme function - describe output of the answering page for the imagetarget
 */
function theme_imagetarget_answering_form($form) {
  $path = drupal_get_path('module', 'imagetarget');
  jquery_ui_add(array('ui.draggable', 'ui.droppable', 'ui.sortable'));
  _imagetarget_add_js('imagetarget-answer');

  $header = array(
    array('data' => t('Target Number')),
    array('data' => t('Drag Target')),
    array('data' => t('Description')),
  );
  $rows = array();

  foreach ($form['targets'] as $identifier => $target) {
    if (intval($identifier) > 0) {
      $row = array();
      $row[] = array( 'data' => "<span>{$identifier}. </span>", 'width' => 35, );
      $row[] = array( 'class' => 'drag-' . $identifier, 'data' => '<div style="position:relative;"><div class="imagetarget-target" id="imagetarget-target-' . $identifier . '" style="position: absolute; width: 32px; height: 32px; top: -14px; background: url(' . url($path . '/theme/images/target.png') . ') no-repeat scroll 0 0 transparent;">' . $identifier . '</div></div>', 'width' => 35, );
      $row[] = drupal_render($target['targettext']);
      $rows[] = $row;
    }
  }

  $output = drupal_render($form['question']);
  $output .= drupal_render($form['imagetarget_image']);
  $output .= drupal_render($form['tries[answer]']);
  $output .= theme('table', $header, $rows, array(), NULL, TRUE);
  $output .= drupal_render($form['navigation']);

  return $output;
}
