######################################################################
#
# GoogleVisionImagePlugin.pm -- plugin that extends the capability of 
# ImagePlugin to use Google Vision API allowing for: metadata labelling
# of objects within a scene; OCR text recognition.
#
# 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.
#
###########################################################################

package GoogleVisionImagePlugin;

use strict;
no  strict 'refs'; # allow filehandles to be variables and viceversa
no  strict 'subs';

use utf8;
use JSON qw( from_json );
use Digest::MD5 qw( md5_hex );

use gsprintf;
use FileUtils;

use ImagePlugin;
use GoogleVisionAPIConverter;


sub BEGIN {
    @GoogleVisionImagePlugin::ISA = ('ImagePlugin', 'GoogleVisionAPIConverter');
}

my $arguments = [];


my $options = { 'name'     => "GoogleVisionImagePlugin",
		'desc'     => "{GoogleVisionImagePlugin.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 ImagePlugin($pluginlist, $inputargs, $hashArgOptLists);

    return bless $self, $class;
}


# do plugin specific processing of doc_obj
sub process {
    my $self = shift (@_);
    # options??
    my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;

    my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
    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->{'gv-dococr-json-filename-recs'} = [];

    $self->run_gv_convert($filename_full_path,$url_encoded_filename,$doc_obj);

    $self->SUPER::process(@_);
}

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;











