###########################################################################
#
# PharosImagePlugin - an ImagePlugin that can also do pharos related tasks.
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2008 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################
package PharosImagePlugin;

use ImagePlugin;
use PharosImageIndexer;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa

use gsprintf 'gsprintf';

BEGIN {
    @PharosImagePlugin::ISA = ('ImagePlugin', 'PharosImageIndexer');
}

my $index_on_list =
    [ { 'name' => "original",
        'desc' => "the original full sized file (will convert to jpg if necessary)" },
      { 'name' => "screenview",
        'desc' => "The screenview sized image (jpg screenview images will be created)" }, 
      { 'name' => "thumbnail",
        'desc' => "The thumbnail image (jpg thumbnails will be created)" } ];


my $arguments = [
		 { 'name' => "disable_pharos_imageis",
		   'desc' => "Switch off the image similarity indexing",
		   'type' => "flag",
		   'reqd' => "no" },
		 { 'name' => "pharos_imageis_index",
		   'desc' => "Which size image files to index",
		   'type' => "enum",
		   'deft' => "thumbnail",
		   'list' => $index_on_list,
		   'reqd' => "no"}
		 ];

my $options = { 'name' => "PharosImagePlugin",
		'desc' => "Image Plugin that also does Pharos Image Similarity Indexing",
		'abstract' => "no",
		'inherits' => "yes",
		'args' => $arguments };

sub new {
    my ($class) = shift (@_);
    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
    push(@$pluginlist, $class);

    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
    push(@{$hashArgOptLists->{"OptList"}},$options);

    new PharosImageIndexer($pluginlist, $inputargs, $hashArgOptLists);
    my $self = new ImagePlugin($pluginlist, $inputargs, $hashArgOptLists);

    if ($self->{'disable_pharos_imageis'}) {
	# pharos disabled, don't care about args
	return bless $self, $class;
    }
    
    # we want imageconverter to store the paths to thumbnails etc so we can use them later.
    $self->{'store_file_paths'} = 1;
    # check args
    if ($self->{'pharos_imageis_index'} eq "thumbnail") {
	# thumbnails
	if ($self->{'create_thumbnail'} eq "false") {
	    print STDERR "Pharos Indexing set to index thumbnails, switching on -create_thumbnail\n";
	    $self->{'create_thumbnail'} = "true";
	}
	if ($self->{'thumbnailtype'} !~ /^jpe?g$/i) {
	    print STDERR "Pharos Indexing set to index thumbnails, setting thumbnailtype to jpg\n";
	    $self->{'thumbnailtype'} = "jpg";
	}
    } elsif ($self->{'pharos_imageis_index'} eq "screenview") {
	# screenview
	if ($self->{'create_screenview'} eq "false") {
	    print STDERR "Pharos Indexing set to index screenview, switching on -create_screenview\n";
	    $self->{'create_screenview'} = "true";
	}
	if ($self->{'screenviewtype'} !~ /^jpe?g$/i) {
	    print STDERR "Pharos Indexing set to index screenview, setting screenviewtype to jpg\n";
	    $self->{'screenviewtype'} = "jpg";
	}
	

    } else {
	# originals
	if (!$self->{'converttotype'} || $self->{'converttotype'}!~ /^jpe?g$/i) {
	    print STDERR "Pharos Indexing set to index original images, setting  -converttotype to jpg\n";
	    $self->{'converttotype'} = "jpg";
	}
	
    }
    return bless $self, $class;

}

# needs to be called after BasePlugin init, so that outhandle is set up.
sub init {
    my $self = shift(@_);
    my ($verbosity, $outhandle, $failhandle) = @_;

    $self->SUPER::init(@_);
    $self->PharosImageIndexer::init() unless $self->{'disable_pharos_imageis'};

    if (!$self->{'pharos_available'}) {
	$self->{'disable_pharos_imageis'} = 1; # set this to one so we don't call the functions all the time and get lots of warnings
    }
}

sub remove_all {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
    
    $self->SUPER::remove_all(@_);
    $self->PharosImageIndexer::remove_all(@_) unless $self->{'disable_pharos_imageis'};
   
}

sub remove_one {
    my $self = shift (@_);
    my ($file, $oids, $archivedir) = @_;
    return undef if (!$self->can_process_this_file($file));
    
    $self->PharosImageIndexer::remove_one(@_) unless $self->{'disable_pharos_imageis'};
    return $self->SUPER::remove_one(@_);
    
    
}

sub post_process_doc_obj {
    my $self = shift (@_);
    # options??
    my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
    return if $self->{'disable_pharos_imageis'};
    if ($self->{'pharos_imageis_index'} eq "thumbnail" && -e $self->{'thumb_file'} ) {
	print STDERR "indexing thumbnail $self->{'thumb_file'}\n";
	$self->pharos_index_image($self->{'thumb_file'}, $doc_obj);
    } elsif ($self->{'pharos_imageis_index'} eq "screenview" && -e $self->{'screen_file'}) {
	print STDERR "indexing screenview $self->{'screen_file'}\n";
	$self->pharos_index_image($self->{'screen_file'}, $doc_obj);
    }
    elsif (defined $self->{'orig_file'} && -e $self->{'orig_file'}) {
	print STDERR "indexing converted original $self->{'orig_file'}\n";
	$self->pharos_index_image($self->{'orig_file'}, $doc_obj);
    }
    else {
	# fall back option, index the original
	print STDERR "indexing original &util::filename_cat($base_dir, $file)\n";
	$self->pharos_index_image(&util::filename_cat($base_dir, $file), $doc_obj);
    }
}



1;	
