#!/usr/bin/perl

use strict;
use vars qw/$DB $URL/;
use lib '/usr/local/gramene/lib/perl/';

use Ace 1.51;
use CGI 2.42 qw/:standard :html3 escape/;
use CGI::Carp qw/fatalsToBrowser/;
use Ace::Browser::AceSubs qw(:DEFAULT ResolveUrl DoRedirect AceInit AceHeader);
use Ace::Browser::SearchSubs;
use GrameneSubs qw(:swish AceSearchMenuBar);
use GramenePage;

# zero globals in utilities
AceInit();
my $pattern        = param('query');
my $search_type    = param('type');
my $offset         = AceSearchOffset();

$URL = url();
$URL=~s!^http://[^/]+!!;

# fetch database handle
$DB = OpenDatabase() || AceError("Couldn't open database.");

my ($objs,$count);
($objs,$count) = do_search($pattern,$offset,$search_type) if $pattern;


# possibly search Rice genetics newsletter with SWISH-E
my $static_files;
if (param('RGN') && $pattern) {
  $static_files = Search_Swish($pattern);
}

DoRedirect(@$objs) if $count==1 && !$static_files;

AceHeader();
my $page = GramenePage->new(Apache->request) || AceError("Can't open Gramene page configurator");

print start_html(-Title=>'Gramene Text Search',
		 -Style  => {-src => $page->stylesheet},
		 -Target=>'_top',
		 -Class=>'search');
print $page->start_body;

print
  AceSearchMenuBar('nohome'),
  h1(img({-src=>SEARCH_ICON,-align=>'CENTER'}),'Legacy Text Search');

display_search_form();
display_search($objs,$count,$offset,$pattern) if $pattern;
Display_Swish_Hits($static_files,$pattern) if param('RGN');

print  $page->end_body;

exit 0;

sub display_search_form {
  print p({-class=>'small'},
	  "Type in text or keywords to search for.",
	  "The * and ? wildcard characters are allowed.");
  my $name = Configuration()->Name || get_symbolic();
  AceSearchTable("\u$name Text Search",
		 table({-width=>'100%'},
		       TR(
			  td({-align=>'LEFT'},"Search text: "),
                       ),TR(
			  td(textfield(-name=>'query',-size=>40),
			  submit(-label=>'Search'))
                       ),
		       TR(
			  td({-colspan=>3},
			     radio_group(-name=>'type',
					 -value=>[qw/short long/],
					 -labels=>{'short'=>'Fast search',
						   'long' =>'In-depth search'}
					),
			     checkbox(-name=>'RGN',-label=>'Include Rice Genetics Newsletter',-checked=>1)
			    ))
                       ));
}

sub do_search {
  my ($pattern,$offset,$type) = @_;
  my $count;
  $pattern =~ s/\"//g;
  my (@objs) = $DB->grep(-pattern=> $pattern,
			 -count  => MAXOBJECTS,
			 -offset => $offset,
			 -total => \$count,
			 -long  => $type eq 'long',
			 );
  return ([],0) unless @objs;
  return (\@objs,$count);
}

sub display_search {
    my ($objs,$count,$offset,$pattern) = @_;
    my $title = $count > 0 ? 
       p(strong($count),"objects contain the keywords \"$pattern\"")
     : p({-class=>'error'},'No matching database objects found');

    my @objects = map { font({-color=>'red'},$_->class) . ":&nbsp;".a({-href=>Object2URL($_)},$_) } 
      sort { $a->class cmp $b->class } @$objs;
    print a({-name=>'searchagain'},'&nbsp;');
    AceResultsTable(\@objects,$count,$offset,$title);
}

