#! /usr/bin/perl -w

############################################################
#
# This code is GPL|Artistic dual licensed.
#
# See either Artistic.html or COPYING for the exact conditions.
# Choose among Artistic.html and COPYING the license you prefer.
#
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# (C) 2001 Centre Universitaire d'Informatique, University of Geneva
#
# Author: Wolfgang M&uuml;ller  <Wolfgang.MUELLER@cui.unige.ch>

use XML::Writer;

require Exporter;

use MRML::Client::Base;
use Data::Dumper;
package main;

=pod

=head1 NAME

gift-test-config-test-plugin.pl - a test for illustrating the functions
 of the Ct plugin to the GIFT

=head1 SYNOPSIS

  gift-test-config-test-plugin.pl

=head1 DESCRIPTION

See the article that was written for the ct.

=head1 SEE

 MRML::Client::Base
 Data::Dumper

=cut


use strict;
use vars qw($VERSION);


$VERSION="0.02";


my $lWriter=XML::Writer->new();
my $g_client=new MRML::Client::Base(address=>"localhost:12789");
print "Client instantiiert\n";
my $l_session_1=$g_client->open_session('user-id'=>"tester",
					"session-name"=>"meine-session");
print "Session geöffnet\n";
my $l_session_1_id=$l_session_1->[0]->{attrib}->{"session-id"};
print "SessionID: $l_session_1_id\n";

print "Hole Liste von verfügbaren Algorithmen\n";
my $l_response=$g_client->send_message(tree=>$g_client->generate_mrml(tree=>[
									     {
									      type=>'e',
									      name=>'get-algorithms',
									      content=>[]
									     }
									    ]
								     ));


#print Dumper($l_response);

my($i,$j,$k);
for $i (@{$l_response}){
  if($i->{content}){
    for $j (@{$i->{content}}){
      if(($j->{content})
	 &&
	 ($j->{name} eq "algorithm-list")){
	for $k (@{$j->{content}}){
	  print "Algorithm gefunden, ID/Type: ",$k->{attrib}->{"algorithm-id"},"/",$k->{attrib}->{"algorithm-type"}," Name: ",$k->{attrib}->{"algorithm-name"},"\n";
	}
      }
    }
  }
}

print "Hole Liste von verfügbaren Collections\n";
my $l_response=$g_client->send_message(tree=>$g_client->generate_mrml(tree=>[
									     {
									      type=>'e',
									      name=>'get-collections',
									      content=>[]
									     }
									    ])
				      );


for $i (@{$l_response}){
  if($i->{content}){
    for $j (@{$i->{content}}){
      if(($j->{content})
	 &&
	 ($j->{name} eq "collection-list")){
	for $k (@{$j->{content}}){
	  print "Collection gefunden, ID: ",$k->{attrib}->{"collection-id"}," Name: ", $k->{attrib}->{"collection-name"},"\n";
	}
      }
    }
  }
}


print "Konfiguriere Session $l_session_1_id\n";
 $l_response=$g_client->configure_session( "collection-id"=>"c-ct",
					     "algorithm-id"=> "a-ct",
					     "algorithm-type"=> "a-ct");


print "\n";

&send_two_queries( $l_session_1_id,$g_client);


my $l_session_2=$g_client->open_session('user-id'=>"anderer-tester",
					"session-name"=>"meine-session");
print "Session geöffnet\n";
my $l_session_2_id=$l_session_2->[0]->{attrib}->{"session-id"};
print "SessionID: $l_session_2_id\n";

&send_two_queries( $l_session_2_id,$g_client);


sub send_two_queries( $ $ ){
  my $l_session_id=shift;
  my $l_client=shift;

  $l_client->{'session-id'}=$l_session_id;
  $l_client->send_message(tree=>$l_client->generate_query_step('algorithm-id'=> 'a-ct',
							       'result-size'=> 20,
							       'result-cutoff'=> 0,
							       'cui-comment'=>"Eine Anfrage nach einem Bild fuer GIFT wuerde genau so aussehen",
							       tree=>[{
								     type=>'e',
								     name=>'user-relevance-element-list',
								     attrib=>{},
								     content=>[{
										type=>'e',
										name=>'user-relevance-element',
										attrib=>{
											 'user-relevance'=>"1",
											 'image-location'=>"http://www.benchathlon.net/img/done/128x192/3328_2013/0519/0011.jpeg"
											}
									       }]
								    }])
		       );
$l_client->send_message(tree=>$l_client->generate_query_step('algorithm-id'=> 'a-ct',
							     'result-size'=> 20,
							     'result-cutoff'=> 0,
							     'cui-comment'=>"Eine text query wuerde hingegen so aussehen:",
							     tree=>[{
								     type=>'e',
								     name=>'cui-text-query',
								     attrib=>{'query-string'=>'htdig'},
								     content=>[]
								    }])
		       );
}

print "End\n";
