<?php

/**
 * @file
 * The main file for the blast UI module.
 */

// Type-specific BLAST functionality
require_once 'includes/blast_ui.blastn.inc';
require_once 'includes/blast_ui.blastp.inc';

// BLAST DB Node functionality
require_once 'includes/blast_ui.node.inc';

// Functions specific to themeing (ie: preprocess)
require_once 'theme/blast_ui.theme.inc';

// Application Programmers Interface
require_once 'api/blast_ui.api.inc';

/**
 * Implements hook_menu().
 */
function blast_ui_menu() {

  // Parent menu-item for BLAST submission
  $items['blast'] = array(
    'title' => 'BLAST',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_nucleotide_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
    'expanded' => TRUE,
  );

  // Nucleotide BLAST submission form
  $items['blast/blastn'] = array(
    'title' => 'Nucleotide BLAST',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_nucleotide_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM
  );

  // Protein  BLAST submission form
  $items['blast/blastp'] = array(
    'title' => 'Protein BLAST',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_protein_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM
  );

  // BLAST Results page
  $items['blast/report/%'] = array(
    'title' => 'BLAST Results',
    'page callback' => 'show_blast_output',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implements hook_theme().
 */
function blast_ui_theme() {

  $items = array();
  $path = drupal_get_path('module', 'blast_ui');

  // Displays the BLAST results for each job
  $items['show_blast_report'] = array(
    'template' => 'blast_report',
    'path' => "$path/theme",
  );

  // Displays the BLAST results for each job
  $items['blast_report_pending'] = array(
    'template' => 'blast_report_pending',
    'path' => "$path/theme",
  );

  // Themes the alignments in a BLAST result display
  $items['blast_report_alignment_row'] = array(
    'template' => 'blast_report_alignment_row',
    'variables' => array('hsps' => NULL),
    'path' => "$path/theme",
  );

  return $items;
}

/**
 * Facilitate presenting the result of the blast search
 *
 * @param $job_id
 *  The tripal job_id of the BLAST job previously submitted
 *
 * @return $result
 *  Return HTML output of the BLAST results to be displayed to the user
 *
 */
function show_blast_output($job_id) {

  // BLASTs are run as a Tripal job. As such we need to determine whether the current
  // BLAST is in the queue, running or complete in order to determine what to show the user
  $job = tripal_get_job($job_id);

  // 1) Job is in the Queue
  if ($job->start_time === NULL AND $job->end_time == NULL) {

    return theme('blast_report_pending', array('status_code' => 0, 'status' => 'Pending'));
  }
  // 2) Job has been Cancelled
  elseif ($job->status == 'Cancelled') {

    return theme('blast_report_pending', array('status_code' => 999, 'status' => 'Cancelled'));
  }
  // 3) Job is Complete
  elseif ($job->end_time !== NULL) {

    // Return the Results :)
    return theme('show_blast_report', array('job_id' => $job_id));

  }
  // 4) Job is in Progress
  else {

    return theme('blast_report_pending', array('status_code' => 1, 'status' => 'Running'));
  }


  return '';
}