<?php

/**
 * @file
 * Create and manage JBrowse instances.
 */

require_once 'includes/tripal_jbrowse_mgmt.api.inc';
require_once 'includes/tripal_jbrowse_mgmt.jobs.inc';
require_once 'includes/tripal_jbrowse_mgmt_commands.inc';

/**
 * Implements hook_menu().
 */
function tripal_jbrowse_mgmt_menu() {
  $items = [];

  // Admin forms.
  $items['admin/tripal/extension/tripal_jbrowse/management'] = [
    'title' => 'Tripal JBrowse Management',
    'description' => 'List JBrowse settings',
    'page callback' => 'tripal_jbrowse_mgmt_instances_page',
    'page arguments' => ['tripal_jbrowse_mgmt_configure_form'],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_list.page.inc',
    'type' => MENU_NORMAL_ITEM,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/instances'] = [
    'title' => 'List Instances',
    'description' => 'List JBrowse settings',
    'page callback' => 'tripal_jbrowse_mgmt_instances_page',
    'page arguments' => ['tripal_jbrowse_mgmt_configure_form'],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_list.page.inc',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/configure'] = [
    'title' => 'Settings',
    'description' => 'List and create JBrowse instances.',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_mgmt_configure_form'],
    'access arguments' => ['configure tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_configure.form.inc',
    'type' => MENU_LOCAL_TASK,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/instances/register'] = [
    'title' => 'Register Existing Instance',
    'description' => 'Adds an existing JBrowse to the list of instances managed within Tripal.',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_mgmt_register_form'],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_register.form.inc',
    'type' => MENU_LOCAL_ACTION,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/instances/add'] = [
    'title' => 'Create New Instance',
    'description' => 'List and create JBrowse instances.',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_mgmt_add_form'],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_add.form.inc',
    'type' => MENU_LOCAL_ACTION,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/instances/%'] = [
    'title' => 'Manage Instance',
    'description' => 'View an instance and manage its tracks.',
    'page callback' => 'tripal_jbrowse_mgmt_instance_page',
    'page arguments' => [6],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_instance.page.inc',
    'type' => MENU_CALLBACK,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/instances/%/edit'] = [
    'title' => 'Edit Instance',
    'description' => 'Edit metadata for existing JBrowse instances.',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_mgmt_edit_form', 6],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_edit.form.inc',
    'type' => MENU_LOCAL_ACTION,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/instances/%/delete'] = [
    'title' => 'Delete Instance',
    'description' => 'Confirm deleting an instance.',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_mgmt_delete_instance_form', 6],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_delete_instance.form.inc',
    'type' => MENU_LOCAL_ACTION,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/instances/%/add_track'] = [
    'title' => 'Add New Track',
    'description' => 'Add new track to a jbrowse instance.',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_mgmt_add_track_form', 6],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_tracks.form.inc',
    'type' => MENU_LOCAL_ACTION,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/tracks/%'] = [
    'title' => 'Edit Track',
    'description' => 'Edit tracks.',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_mgmt_json_editor_form', 6],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_json_editor.form.inc',
    'type' => MENU_CALLBACK,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/tracks/%/delete'] = [
    'title' => 'Delete Track',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_mgmt_delete_track_form', 6],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_tracks.form.inc',
    'type' => MENU_LOCAL_ACTION,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/tracks/%/edit_json'] = [
    'title' => 'Edit Json',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_mgmt_json_editor_advance_form', 6],
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'file' => 'includes/tripal_jbrowse_mgmt_tracks.form.inc',
    'type' => MENU_LOCAL_ACTION,
  ];

  $items['admin/tripal/extension/tripal_jbrowse/management/instances/analysis/autocomplete'] = [
    'title' => 'autocomplete for JBrowse instance analysis',
    'page callback' => '_mgmt_add_analysis_autocomplete',
    'access arguments' => ['administer tripal_jbrowse_mgmt'],
    'type' => MENU_CALLBACK,
  ];

  return $items;
}

/**
 * Implements hook_permission().
 */
function tripal_jbrowse_mgmt_permission() {
  $items = [];

  $items['configure tripal_jbrowse_mgmt'] = [
    'title' => t('Configure Tripal JBrowse Management'),
  ];

  $items['administer tripal_jbrowse_mgmt'] = [
    'title' => t('Create, edit and delete JBrowse instances'),
  ];

  return $items;
}

/**
 * autocomplete helper
 * $string = string for autocomplete search
 * to provide analysis names for form in tripal_jbrowse_mgmt_add.form.inc
 */
 function _mgmt_add_analysis_autocomplete($string){
   $analysis_4match =array();
   $analysis = db_select('chado.analysis', 'CA')
     ->fields('CA', ['analysis_id', 'name'])
     ->condition('name','%'.db_like($string).'%','LIKE')
     ->execute();

    foreach($analysis as $one_analysis){
      $key = $one_analysis->name . ' (' . $one_analysis->analysis_id . ')';
      $analysis_4match[$key] = $one_analysis->name;
    }
    drupal_json_output($analysis_4match);
 }
