#!/usr/bin/env raku
use v6.d;
use Markup::Calendar;

my %*SUB-MAIN-OPTS = :named-anywhere;

#| Displays a calendar in HTML format.
sub MAIN(
        Str:D :y(:$year) = '',            #= Year spec; if an empty string only the months spec is used.
        Str:D :m(:$months) = 'Whatever',  #= Months spec, comma separated list of integers or month names.
        Str:D :h(:$highlight) = '',       #= Highlight spec that is a comma separated list of days.
        Str:D :t(:$tooltip) = '',         #= Tooltip spec.
        Str:D :l(:$hyperlink) = '',       #= Hyperlink spec (per calendar day.)
        Str:D :s(:$highlight-style) = 'color:orange; font-size:12pt', #= Highlight style (HTML style.)
        UInt:D :$per-row = 3,             #= Number of months per row.
        Bool:D :doc(:$document) = False   #= Should a complete HTML document be made or not?
         ) {

    # Process year
    my $yearLocal = $year.lc ∈ <whatever auto automatic> ?? Whatever !! $year.Int;

    # Process months
    my $monthsLocal = $months.lc ∈ <whatever auto automatic> ?? Whatever !! $months.split(/<[,;]>/, :skip-empty).Array;

    if $yearLocal && $monthsLocal.isa(Whatever) {
        $monthsLocal = 1 .. 12
    } elsif !$monthsLocal.isa(Whatever) {
        $monthsLocal = $monthsLocal.map({ my $i = try $_.Int; $! ?? $_ !! $i }).Array
    }

    # Process highlight
    my @highlightLocal;
    if $highlight { @highlightLocal = |$highlight.split(/<[,;]>/, :skip-empty) }

    my @tooltipLocal;
    if $tooltip { @tooltipLocal = |$tooltip.split(/<[,;]>/, :skip-empty) }

    my @hyperlinkLocal;
    if $hyperlink { @hyperlinkLocal = |$hyperlink.split(/<[,;]>/, :skip-empty) }

    my %args =
            highlight => @highlightLocal,
            tooltip => @tooltipLocal,
            hyperlink => @hyperlinkLocal,
            :$highlight-style,
            :$per-row,
            :$document;

    if $year {
        say Markup::Calendar::calendar-html($yearLocal, $monthsLocal, |%args)
    } else {
        say Markup::Calendar::calendar-html($monthsLocal, |%args)
    }
}
