package DTA::View::XSLT;
use parent 'Catalyst::View::XSLT';

use utf8;

use warnings;
use strict;

use URI::Escape;
use utf8;

open( my $fh, '<', DTA->config->{dta}->{nrhz_links} ) or die $!;
my @nrhz_chain;
while( my $line = <$fh> ) {
    my @single_chain;
    while( $line =~ /#([\w-]+)/g ) {
        push @single_chain, $1;
    }
    push @nrhz_chain, \@single_chain if @single_chain;
}


__PACKAGE__->config(
    INCLUDE_PATH => [
        DTA->path_to( 'root', 'xslt' ),
    ],
    TEMPLATE_EXTENSION => '.xsl',
    LibXSLT => {
        register_function => [
            {
                uri => 'urn:dta',
                name => 'n',
                subref => \&dta_n,
            },
            {
                uri => 'urn:dta',
                name => 'imglink',
                subref => \&dta_imglink,
            },
            {
                uri => 'urn:dta',
                name => 'is_number',
                subref => \&dta_is_number,
            },
            {
                uri => 'urn:dta',
                name => 'spaces_only',
                subref => sub {
                    my $str = shift || '';
                    return $str =~ /^\s+$/;
                },
            },
            {
                uri => 'urn:dta',
                name => 'fn_notseen',
                subref => \&dta_fn_notseen,
            },
            {
                uri => 'urn:dta',
                name => 'escape',
                subref => \&dta_escape,
            },
            {
                uri => 'urn:dta',
                name => 'urlencode',
                subref => sub {
                    my $str = shift;
                    $str = uri_escape_utf8($str);
                    for ( $str ) {
                        s/&/&amp;/g;
                        s/"/&quot;/g;
                        s/</&lt;/g;
                        s/>/&gt;/g;
                    }
                    return $str;
                },
            },
            {
                uri => 'urn:nrhz',
                name => 'prev_article',
                subref => sub {
                    my $id_wanted = shift;
                    foreach my $row ( @nrhz_chain ) {
                        my $i = 0;
                        foreach my $id ( @$row ) {
                            if ( $id eq $id_wanted ) {
                                return if $i == 0;
                                return nrhz_article_link($row->[$i-1]);
                            }
                            $i++;
                        }
                    }
                    return;
                },
            },
            {
                uri => 'urn:nrhz',
                name => 'next_article',
                subref => sub {
                    my $id_wanted = shift;                 
                    foreach my $row ( @nrhz_chain ) {
                        my $i = 0;
                        foreach my $id ( @$row ) {
                            if ( $id eq $id_wanted ) {
                                return if $i == @$row - 1;
                                return nrhz_article_link($row->[$i+1]);
                            }
                            $i++;
                        }
                    }
                    return;
                },
            },
            {
                uri => 'urn:dtabf',
                name => 'ref',
                subref => sub {
                    my $ref = shift;
                    return $ref if $ref =~ /^http:/;
                    $ref =~ s{(.*?)\.html(?:#(.*))?}{/doku/$1#$2};
                    return $ref;
                },
            },
            {
                uri => 'urn:dtabf',
                name => 'tr',
                subref => sub {
                    my ($type, $str) = @_;
                    return '' unless $str;
                    my %map = (
                        'content' => 'Texterschließung (inhaltlich)',
                        'doc' => 'Dokumentation',
                        'formal' => 'Texterschließung (formal)',
                        'header' => 'TEI-Header',
                        'schema' => 'Schema',
                        'table_header' => 'Übersicht (Header)',
                        'table_text' => 'Übersicht (Textbereich)',
                        'text' => 'Textbereich',
                        'transcrGl' => 'Transkriptionsrichtlinien',
                        'usage' => 'Benutzung',
                    );
                    return $map{$str} || 'XXX';
                },
            },
        ]
    },
#    DUMP_CONFIG => 1,
);

sub dta_n {
    my $str = shift;
    $str =~ s/\A\[(.*)\]\z/$1/; # outer brackets
    $str =~ s/\A([^[]+).*/$1/;
    return $str;
}

sub dta_imglink {
    my $str = shift;
    $str =~ /(\d+)/;
    my $num = $1;
}

sub dta_is_number {
    my $str = shift;
    return unless $str;
    return $str =~ /\A[0-9]+\z/;
}

our %fn_seen = ();
sub dta_fn_notseen {
    my $n = shift;
    return if exists $fn_seen{$n};
    $fn_seen{$n}++;
    return 1;
}

sub dta_escape {
    my $str = shift || '';
    for ( $str ) {
        s/"/&quot;/g;
        s/'/&apos;/g;
    }
    return $str;
}

sub nrhz_article_link {
    my $id = shift;
    my ( $journal ) = $id =~ /ar(\d+(?:[abc]\d?)?(?:-\d+(?:[abc]\d?)?)?)/;
    return sprintf "%s.xml#%s", $journal, $id;
}

1;
__END__

=head1 NAME

DTA::View::XSLT - XSLT View Component

=head1 SYNOPSIS

L<DTAQ>

=head1 DESCRIPTION

Catalyst XSLT View.

=head1 AUTHOR

Frank Wiegand

=head1 LICENSE

This library is free software. You can redistribute it and/or modify it under
the same terms as Perl itself.

=cut
