package view;

#
# BUILD CONSTRAINT:  all views must return $content, $extension.
# additional return values (as seen below) are optional.  However,
# careful use of symlinks and dependency management in path.pm can
# resolve most issues with this constraint.
#

use strict;
use warnings;
use Dotiac::DTL qw/Template/;
use Dotiac::DTL::Addon::markup;
use ASF::Util qw/read_text_file/;

push @Dotiac::DTL::TEMPLATE_DIRS, "templates";

# This is most widely used view.  It takes a
# 'template' argument and a 'path' argument.
# Assuming the path ends in foo.mdtext, any files
# like foo.page/bar.mdtext will be parsed and
# passed to the template in the "bar" (hash)
# variable.
sub normal_page {
    my %args = @_;
    my $file = "content$args{path}";
    $args{path} =~ s/\.mdtext$/\.html/;

    my $template = $file;
    if($args{template}) {
       $template = $args{template};
    }

    read_text_file $file, \%args;
    $args{breadcrumbs} = breadcrumbs($args{path}, $args{headers});

    if($args{path} eq "sitemap.html" || $args{path} eq "/sitemap.html") {
      sitemap($file, \%args);
    }    

    my $page_path = $file;
    $page_path =~ s/\.[^.]+$/.page/;
    if (-d $page_path) {
        for my $f (grep -f, glob "$page_path/*.mdtext") {
            $f =~ m!/([^/]+)\.mdtext$! or die "Bad filename: $f\n";
            $args{$1} = {};
            read_text_file $f, $args{$1};
        }
    }

    return Dotiac::DTL::Template($template)->render(\%args), html => \%args;
}

# Generates cwiki-style breadcrumbs
sub breadcrumbs {
    my ($fullpath, $headerref) = @_;
    my @titles = split m!/!, $fullpath;
    my @paths = split m!/!, $fullpath;

    if($paths[scalar@paths-1] =~ /^index/) { 
      pop @titles; 
      pop @paths; 
    } else {
      if($headerref && $headerref->{title}) {
         $titles[scalar@titles-1] = $headerref->{title};
      }
    }
    $titles[0] = "Home";

    my @rv;
    my $relpath = "/";
    for(my $i=0; $i<scalar @paths; $i++) {
        if($i == (scalar @paths)-1 && $headerref->{breadcrumb}) {
           my $bc = $headerref->{breadcrumb};
           $bc =~ s/^\s+//;
           $bc =~ s/\s+$//;
           if($bc =~ /\:/) {
              $bc =~ /^(.*?)\:(.*?)$/;
              push @rv, qq(<a href="$relpath$1.html">$2</a>)
           } else {
              push @rv, qq(<a href="$relpath$bc">$bc</a>)
           }
        }

        my $title = $titles[$i];
        $relpath .= $paths[$i];
        push @rv, qq(<a href="$relpath">\u$title</a>);
        unless($relpath eq "/") { $relpath .= "/"; }
    }

    return join "&nbsp;&raquo&nbsp;", @rv;
}

# Generate a CWiki style sitemap
sub sitemap {
   my ($file, $argsref) = @_;

   # Find the list of files
   my ($dir) = ($file =~ /^(.*)\/.*?/);
   my $entries = {};
   sitemapFind($dir, $entries);

   my $sitemap = "<ul>\n";
   $sitemap = sitemapRender($sitemap, $entries, "");
   $sitemap .= "</ul>\n";
   $argsref->{'sitemap'} = $sitemap;
}
sub sitemapFind {
   my ($dir, $entries) = @_;
   $entries->{"title"} = "";
   $entries->{"entries"} = {};
   my %entries = ( "title"=>"", "entries"=>{} );

   foreach my $item (<$dir/*>) {
      my ($rel) = ($item =~ /^.*\/(.*?)$/);

      if(-d $item) {
         $entries->{"entries"}->{$rel} = {};
         sitemapFind($item, $entries->{"entries"}->{$rel});
      } else {
         # Grab the title
         my $title = $rel;
         if($rel =~ /\.mdtext$/) {
             my %args;
             read_text_file $item, \%args;
             $title = $args{"headers"}->{"title"};
         } elsif ($rel =~ /\.png$/ || $rel =~ /\.jpg$/) {
            next;
         } else {
             open F, "<$item";
             my $file = "";
             while(my $line = <F>) {
                $file .= $line;
             }
             close F;

             if($file =~ /block\s+title\s*\%\}(.*?)\{/) {
                $title = $1;
             } elsif($file =~ /title\>(.*?)\</) {
                $title = $1;
             }
         }

         # Process
         if($rel =~ /^index\.(html|mdtext)$/) {
            $entries->{"title"} = $title;
         } else {
            $entries->{entries}->{$rel}->{title} = $title;
         }
      }
   }
   return %entries;
}
sub sitemapRender {
   my ($sitemap, $dir, $path) = @_;
   my %entries = %{$dir->{"entries"}};

   foreach my $e (sort keys %entries) {
      my $fn = $e;
      $fn =~ s/\.mdtext/.html/;
      if($entries{$e}->{entries}) {
         $fn .= "/";
      }
      if($fn eq "images/" or $fn eq "resources/") {
         next;
      }

      my $title = $entries{$e}->{title};
      unless($title) {
         $title = $e;
      }

      $sitemap .= "<li><a href=\"$path/$fn\">".$title."</a>";
      if($entries{$e}->{entries}) {
         $sitemap .= "<ul>\n";
         $sitemap = sitemapRender($sitemap, $entries{$e}, "$path/$e");
         $sitemap .= "</ul>\n";
      }
      $sitemap .= "</li>\n";
   }
   return $sitemap;
}

1;

=head1 LICENSE

           Licensed to the Apache Software Foundation (ASF) under one
           or more contributor license agreements.  See the NOTICE file
           distributed with this work for additional information
           regarding copyright ownership.  The ASF licenses this file
           to you under the Apache License, Version 2.0 (the
           "License"); you may not use this file except in compliance
           with the License.  You may obtain a copy of the License at

             http://www.apache.org/licenses/LICENSE-2.0

           Unless required by applicable law or agreed to in writing,
           software distributed under the License is distributed on an
           "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
           KIND, either express or implied.  See the License for the
           specific language governing permissions and limitations
           under the License.

