#!/usr/bin/perl

use CGI;
use RDF::TrineShortcuts ':all';
use URI::Escape;

my $cgi   = CGI->new;
my $node  = $cgi->param('Node') || 'http://buzzword.org.uk/2010/game/test-nodes/london';
my $model = rdf_parse($node);

# Add vocab itself!
{
   local $/ = undef;
   rdf_parse(<DATA>, type=>'Turtle', model=>$model);
}

# We need this for the first SPARQL query...
$model->add_statement(rdf_statement("<$node> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/NET/game#Node> ."));

print $cgi->header('text/html');

print "<!doctype html>\n";
print "<html>\n";
print "\t<head>\n";
print "\t\t<title property=\"http://purl.org/dc/terms/title\">game client - $node</title>\n";
print "\t</head>\n";
print "\t<body about=\"$node\">\n";

my ($data) = flatten_iterator(rdf_query(<<SPARQL, $model));

PREFIX game: <http://purl.org/NET/game#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc:   <http://purl.org/dc/terms/>
PREFIX elem: <http://purl.org/dc/elements/1.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?title ?desc ?img
WHERE {
   <$node> a game:Node .
   OPTIONAL { <$node> foaf:name ?title . }
   OPTIONAL { <$node> dc:title ?title . }
   OPTIONAL { <$node> elem:title ?title . }
   OPTIONAL { <$node> rdfs:label ?title . }
   OPTIONAL { <$node> dc:description ?desc . }
   OPTIONAL { <$node> elem:description ?desc . }
   OPTIONAL { <$node> rdfs:comment ?desc . }
   OPTIONAL { <$node> foaf:depiction ?img . }
   OPTIONAL { ?img foaf:depicts <$node> . }
}

SPARQL

printf("\t\t<h1 property=\"http://xmlns.com/foaf/0.1/name\">%s</h1>\n",
   $data->{title} || $node);

printf("\t\t<span rel=\"http://xmlns.com/foaf/0.1/depiction\"><img style=\"float:left;border:1px solid;margin:0 1em 0.67em 0;\" alt=\"\" src=\"%s\" /></span>\n",
   $data->{img})
   if defined $data->{img};
   
printf("\t\t<pre property=\"http://purl.org/dc/terms/description\">%s</pre>\n",
   $data->{desc})
   if defined $data->{desc};

my $links = flatten_iterator(rdf_query(<<SPARQL, $model));

PREFIX game: <http://purl.org/NET/game#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?node ?nodelabel ?predicate ?predicatelabel
WHERE {
   <$node> ?predicate ?node .
   ?predicate rdfs:subPropertyOf game:exit .
   OPTIONAL { ?node foaf:name ?nodelabel . }
   OPTIONAL { ?node rdfs:label ?nodelabel . }
   OPTIONAL { ?predicate rdfs:label ?predicatelabel . }
}

SPARQL

print "\t\t<ul style=\"clear:left;\">\n";
foreach my $link (@$links)
{
   printf("\t\t\t<li><strong>%s:</strong> <a rel=\"%s\" href=\"?Node=%s\" resource=\"%s\">%s</a></li>\n",
      $link->{predicatelabel} || $link->{predicate},
      $link->{predicate},
      uri_escape($link->{node}),
      $link->{node},
      $link->{nodelabel} || $link->{node},
      );
}
print "\t\t</ul>\n";
print "\t</body>\n";
print "</html>\n";

__DATA__
@prefix game: <http://purl.org/NET/game#> .

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:  <http://www.w3.org/2002/07/owl#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix dc:   <http://purl.org/dc/terms/> .

game:Node
  a owl:Class ;
  rdfs:isDefinedBy game: ;
  rdfs:label "Node"@en .

game:exit
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:label "exit"@en ;
  rdfs:comment "Don't use this property directly."@en .

game:north
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "north"@en .

game:north-east
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "north-east"@en .

game:east
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "east"@en .

game:south-east
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "south-east"@en .

game:south
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "south"@en .

game:south-west
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "south-west"@en .

game:west
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "west"@en .

game:north-west
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "north-west"@en .

game:up
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "up"@en .

game:down
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "down"@en .

game:inside
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "inside"@en .

game:outside
  a owl:ObjectProperty ;
  rdfs:isDefinedBy game: ;
  rdfs:subPropertyOf game:exit ;
  rdfs:label "outside"@en .