<?php

// API.
require_once 'includes/tripal_jbrowse_page.api.inc';

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

  // Listing Page.
  $items['jbrowse'] = [
    'title' => 'JBrowse',
    'description' => 'A listing of available JBrowse instances.',
    'page callback' => 'tripal_jbrowse_page_listing_page',
    'access arguments' => ['access content'],
    'file' => 'includes/tripal_jbrowse_page.listing.inc',
    'type' => MENU_NORMAL_ITEM,
  ];

  // Pages for each JBrowse Instance.
  $instances = tripal_jbrowse_mgmt_get_instances();
  foreach ($instances as $instance) {

    if (tripal_jbrowse_page_is_instance_public($instance->id)) {

      // Create the menu item.
      $path = 'jbrowse/'.$instance->organism->genus . '-' . $instance->organism->species;
      $items[$path] = [
        'title' => $instance->title,
        'description' => $instance->description,
        'page callback' => 'tripal_jbrowse_page_page',
        'page arguments' => [1],
        'access arguments' => ['access content'],
        'file' => 'includes/tripal_jbrowse_page.page.inc',
        'type' => MENU_SUGGESTED_ITEM,
      ];
    }
  }

  // Administration.
  $admin_path = 'admin/tripal/extension/tripal_jbrowse/management';
  $items[$admin_path.'/page-integration'] = [
    'title' => 'Page Integration',
    'description' => 'A listing of available JBrowse instances.',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['tripal_jbrowse_page_settings_form'],
    'access arguments' => ['access content'],
    'file' => 'includes/tripal_jbrowse_page.admin.inc',
    'type' => MENU_LOCAL_TASK,
  ];

  return $items;
}

/**
 * Implements hook_theme().
 */
function tripal_jbrowse_page_theme($existing, $type, $theme, $path) {

  $items = array(
    'jbrowse_instance_public_listing' => array(
      // Don't specify the path in the template name.
      // Unless you have your template inside a directory within this module.
      'template' =>  'theme/jbrowse-instance--public-listing',
      'variables' => array('instances' => []),
    ),
    'jbrowse_instance_embedded_page' => array(
      // Don't specify the path in the template name.
      // Unless you have your template inside a directory within this module.
      'template' =>  'theme/jbrowse-instance--embedded',
      'variables' => array('url' => ''),
    ),
  );

  return $items;
}
