###########################################################################
#
# GoogleVisionPagedImagePlugin.pm -- plugin that adds Google Vision API
# capabilities on top of PageImagePlugin
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 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.
#
###########################################################################


# See PagedImagePlugin for more details

package GoogleVisionPagedImagePlugin;

use strict;
no  strict 'refs'; # allow filehandles to be variables and viceversa

use Digest::MD5 qw( md5_hex );

use GoogleVisionAPIConverter;
use PagedImagePlugin;


sub BEGIN {
    @GoogleVisionPagedImagePlugin::ISA = ('PagedImagePlugin', 'GoogleVisionAPIConverter');
}


my $arguments = [];

my $options = { 'name'     => "GoogleVisionPagedImagePlugin",
		'desc'     => "{GoogleVisionPagedImagePlugin.desc}",
		'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 GoogleVisionAPIConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
    my $self = new PagedImagePlugin($pluginlist, $inputargs, $hashArgOptLists);

    return bless $self, $class;
}


#sub init {
#    my $self = shift (@_);
#    my ($verbosity, $outhandle, $failhandle) = @_;
#
#    $self->SUPER::init(@_);
#    $self->GoogleVisionAPIConverter::init();
#}

sub begin {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;

    $self->SUPER::begin(@_);
    $self->GoogleVisionAPIConverter::begin(@_);
}


sub init_new_doc_item
{    
    my $self = shift (@_);
    my ($filename_full_path, $processor, $metadata) = @_;

    my $doc_obj = $self->SUPER::init_new_doc_item(@_);

    $self->{'gv-dococr-json-filename-recs'} = [];
    
    return $doc_obj;    
}


sub process_image {
    my $self = shift(@_);
    my ($filename_full_path, $filename_no_path, $doc_obj, $section, $rotate) = @_;

    my $utf8_filename_no_path = $self->filepath_to_utf8($filename_no_path);
    my $url_encoded_filename = &util::rename_file($utf8_filename_no_path, $self->{'file_rename_method'});
    
    $self->run_gv_convert($filename_full_path,$url_encoded_filename,$doc_obj,$section);
    
    return $self->SUPER::process_image(@_);
}

sub post_process_doc_obj {
    my $self = shift (@_);  
    my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;

    my $ret_val_ok = $self->SUPER::post_process_doc_obj(@_);

    if ($ret_val_ok) {
	$ret_val_ok = $self->opt_run_gen_openannotation($doc_obj);
	#$ret_val_ok = $self->opt_run_gen_webannotation($doc_obj);
    }

    # Compute SimpleAnnotationServer short_id (md5_hex hash on OID)
    # and store as metadata for later use
    my $OID= $doc_obj->get_OID();

    # my $sas_manifest_url = "http://ld.greenstone.org/intermuse/programmes-and-performers/$OID/manifest";
    my $sas_manifest_url = $self->get_openannotation_ld_prefix("$OID/manifest");
    
    my $sas_short_id = md5_hex($sas_manifest_url);

    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(),"SASShortID",$sas_short_id);

    return $ret_val_ok;
}


1;
