<?php

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

// BLAST Submission Forms
require_once 'includes/blast_ui.form_common.inc';
require_once 'includes/blast_ui.form_advanced_options.inc';
// NOTE: The forms themeselves are included using hook_menu to ensure they
// are only included when needed.

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

// BLAST Link-out functionality.
require_once 'includes/blast_ui.linkouts.inc';

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

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

// Administration
require_once 'includes/blast_ui.admin.inc';

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

  // BLAST DB Node
  $items['node__blastdb'] = array(
    'template' => 'node--blastdb',
    'render element' => 'node',
    'base hook' => 'node',
    'path' => 'theme',
  );

  // Single All-in-One BLAST submission form
  $items['blast'] = array(
    'title' => 'BLAST',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_all_in_one_form'),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_single.inc',
    'type' => MENU_NORMAL_ITEM,
    'expanded' => TRUE,
  );

  // Per Query Type BLAST submission forms
  $items['blast/nucleotide'] = array(
    'title' => 'Nucleotide Query',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_query_type_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_query_type.inc',
    'type' => MENU_NORMAL_ITEM,
    'expanded' => TRUE,
  );

  $items['blast/protein'] = array(
    'title' => 'Protein Query',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_query_type_form', 1),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_query_type.inc',
    'type' => MENU_NORMAL_ITEM,
    'expanded' => TRUE,
  );

  // Per BLAST-program submission forms
  $items['blast/nucleotide/nucleotide'] = array(
    'title' => 'BLASTn',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_blast_program_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_program.inc',
    'type' => MENU_NORMAL_ITEM
  );

  $items['blast/nucleotide/protein'] = array(
    'title' => 'BLASTx',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_blast_program_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_program.inc',
    'type' => MENU_NORMAL_ITEM
  );

  $items['blast/protein/nucleotide'] = array(
    'title' => 'tBLASTn',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_blast_program_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_program.inc',
    'type' => MENU_NORMAL_ITEM
  );

  $items['blast/protein/protein'] = array(
    'title' => 'BLASTp',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_blast_program_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_program.inc',
    '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,
  );

  // BLAST Admin
  $items['admin/tripal/extension/tripal_blast'] = array(
    'title' => 'Tripal BLAST User Interface',
    'description' => 'Provides an interface allowing users to execute their own BLASTs.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_admin_form'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/tripal/extension/tripal_blast/blast_ui'] = array(
    'title' => 'BLAST UI',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  $items['admin/tripal/extension/tripal_blast/help'] = array(
    'title' => 'Help',
    'page callback' => 'theme',
    'page arguments' => array('blast_help'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_LOCAL_TASK,
  );

  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",
  );

  // Lists the recent blast jobs for a given user/session.
  $items['blast_recent_jobs'] = array(
    'template' => 'blast_recent_jobs',
    'variables' => array('programs' => NULL),
    'path' => "$path/theme",
  );

  // Module Help
  $items['blast_help'] = array(
    'template' => 'blast_help',
    'path' => "$path/theme",
  );

  // Menu Information Pages
  // These are only seen if the user has disabled the all-in-one
  // or by query type forms.
  $items['blast_user_menupage'] = array(
    'template' => 'blast_user_menupage',
    'path' => "$path/theme",
  );
  $items['blast_nucleotide_user_menupage'] = array(
    'template' => 'blast_nucleotide_user_menupage',
    'path' => "$path/theme",
  );
  $items['blast_protein_user_menupage'] = array(
    'template' => 'blast_protein_user_menupage',
    'path' => "$path/theme",
  );

  return $items;
}

/**
 * Implements hook_help().
 */
function blast_ui_help($path, $arg) {
  if ($path == 'admin/help#blast_ui') {
    return theme('blast_help');
  }
}

/**
 * Implements hook_libraries_info().
 */
function blast_ui_libraries_info() {

  // Tell the libraries API about CViTjs
  $libraries['cvitjs'] = array(
    'name' => 'CViTjs',
    'vendor url' => 'https://github.com/awilkey/cvitjs',
    'version' => '0.0.1',
    'download url' => 'https://github.com/awilkey/cvitjs/archive/master.zip',
  );

  return $libraries;
}

/**
 * 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_string) {

  // 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
  //decode the job_id
  $job_id = blast_ui_reveal_secret($job_string);
  $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 '';
}

/**
 * Enable web services API
 *
 * @param $owner
 *
 * @param $api
 *
 * @return $result
 *
 */
function blast_ui_ctools_plugin_api($owner, $api) {
  if ($owner == 'services' && $api == 'services') {
    return array(
      'version' => 3,
      'file' => 'includes/blast_ui.services.inc'
    );
  }
}
