#!/usr/bin/env perl

# Copyright (c) 2015 Christian Jaeger, copying@christianjaeger.ch
# This is free software. See the file COPYING.md that came bundled
# with this file.

use strict;
use warnings;
use warnings FATAL => 'uninitialized';

use Cwd 'abs_path';
our ($mydir, $myname);

BEGIN {
    my $location = (-l $0) ? abs_path($0) : $0;
    $location =~ /(.*?)([^\/]+?)_?\z/s or die "?";
    ($mydir, $myname) = ($1, $2);
}
use lib "$mydir/../lib";

use PXML::XHTML ':all';
use FP::Lazy;
use FP::List;
use PXML::Serialize;

use utf8;

$| = 1;

sub countdown {
    my ($i) = @_;
    lazyLight {

        #sleep 1;
        if ($i >= 0) {
            cons(P($i), countdown($i - 1));
        } else {
            null

                # XX should test undef here, too
        }
    }
}

sub page {
    my ($title, $mtime, $main) = @_;
    HTML(
        HEAD(TITLE($title)),
        BODY(
            $main,
            HR(),
            P(
                "By ",
                A({ href => "http://christianjaeger.ch" }, "Christian Jaeger"),
                ", last modified at ",
                gmtime($mtime) . "",
                " (or something)."
            )
        )
    )
}

our $numbers = { 1 => "one", 2 => "two", 3 => "three" };

sub examplepage {
    my ($title) = @_;
    page(
        "example page - $title",
        $ENV{T} // time,
        [
            H1($title),
            P(
                "Garçon méchanique, \"1 < 2\" is true. ",
                A({ href => "\"1 < 2\"" }, "this will be 404")
            ),
            TABLE(
                { border => 1 },
                map { TR(TD($_), TD($$numbers{$_})) } (1 .. 3)
            ),
            countdown($ENV{N} || 1e9),
        ]
    )
}

pxml_xhtml_print examplepage("Hello World"), *STDOUT{IO}, "en";

