##########################################################################
#
# sasOpenAnnotationBuildproc.pm -- called by sasOpenAnnotationBuidler, per doc
# 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 sasOpenAnnotationBuildproc;

use strict; 
no strict 'refs'; # allow filehandles to be variables and viceversa

use util;

use extrabuildproc;


BEGIN {
    @sasOpenAnnotationBuildproc::ISA = ('extrabuildproc');
}

sub new()
  {
    my $class = shift @_;

    my $self = new extrabuildproc (@_);

    return bless $self, $class;
}


sub textedit {
    my $self = shift (@_);
    my ($doc_obj,$file,$mode) = @_;

    # Code written on the assumption that that sasOpenAnnotation does a replace
    # operation when presented with a docid that already extis.
    # => don't need to do anything special to distinguish between
    #    a mode of "add" and "update"

    # **** !!!! Need to check the above is true!!
    
    my $outhandle = $self->{'outhandle'};

    my $source_dir = $self->{'source_dir'}; # typically the archives dir
    my $build_dir  = $self->{'build_dir'};


    # get doc id
    my $doc_oid = $doc_obj->get_OID();

    # work out if we need to process it
    my $top_section = $doc_obj->get_top_section();
    my $tl_contains_openannotation_json = $doc_obj->get_metadata_element($top_section,"ContainsOpenAnnotationJSON");
	
    if ((defined $tl_contains_openannotation_json) && ($tl_contains_openannotation_json eq "1")) {

	print STDERR "**** Hardwiring port value to: 8383\n";
	my $gs_http_port   = "8383";	
	my $site           = $self->{'site'};    	
	my $collect        = $ENV{'GSDLCOLLECTION'};
	my $gs_library_url = "http://localhost:${gs_http_port}/greenstone3/library";
	    
	my $internal_url_base = "http://localhost:${gs_http_port}/simpleAnnotationStore/annotation";

	my $destroy_delete_url = "${internal_url_base}/destroy";
	my $populate_post_url  = "${internal_url_base}/populate";

	# First remove any previous gv-blocks
	my $gv_block_uris = $doc_obj->get_metadata($top_section,"GoogleVisionBlocks");
	for my $gv_block_uri (@$gv_block_uris) {
	    print $outhandle "  Deleting previous OpenAnnotation GV-block:\n    $gv_block_uri\n";

	    my $cmd = "curl --silent -X DELETE $destroy_delete_url?uri=${gv_block_uri}";
		
	    my $status = system($cmd);
	    if ($status != 0) {
		print STDERR "Error: failed to run:\n  $cmd\n$!\n";
	    }
		
	}
	    	
	# map to assoc dir
	my $assoc_file  = $doc_obj->get_metadata_element ($top_section,"assocfilepath");
	# my $assoc_filename = &util::filename_cat($source_dir,$assoc_file);

	my $tl_root_oa_id = "openannotation-list";	
	my $iuri_prefix = "$gs_library_url/sites/$site/collect/$collect/index/assoc/$assoc_file/";
	
	# For each page:
	#    send the openannotation-list<n>.json to SAS endpoint
	
	my $section = $doc_obj->get_top_section();
	while (defined $section)
	{
	    my $has_openannotation_json = $doc_obj->get_metadata_element($section,"HasOpenAnnotationJSON");
	    if ((!defined $has_openannotation_json) || ($has_openannotation_json eq "0")) {
		$section = $doc_obj->get_next_section($section);
		next;
	    }
		
	    my $sec_oa_id = "${tl_root_oa_id}${section}";

	    my $sec_oa_file = $sec_oa_id;
	    $sec_oa_file =~ s/\./-/g;
	    $sec_oa_file .= ".json";

	    print $outhandle "  Inserting Open Annotation for $doc_oid.$section\n";

	    my $iuri_tail   = $sec_oa_file;
	    my $iuri="${iuri_prefix}${iuri_tail}";

	    my $cmd = "curl --silent -X POST -d \"uri=${iuri}\" \"$populate_post_url\"";
	    
	    my $status = system($cmd);
	    if ($status != 0) {
		print STDERR "Error: failed to run:\n  $cmd\n$!\n";
	    }
	    
	    $section = $doc_obj->get_next_section($section);
	}
    }        
}

sub text {
    my $self = shift (@_);
    my ($doc_obj,$file) = @_;

    $self->textedit($doc_obj,$file,"add");
}

sub textreindex
{
    my $self = shift @_;
    my ($doc_obj,$file) = @_;

    $self->textedit($doc_obj,$file,"update");
}

sub textdelete
{
    my $self = shift @_;

    my ($doc_obj,$file) = @_;

    print STDERR "Warning: sasOpenAnnotation command-line does not currently support delete operation\n";

    # $self->textedit($doc_obj,$file,"delete");
}

1;
