### ----------------------------------------------------
### -- PDF::NameTags
### -- Licenses: Artistic-2.0
### -- Authors: Tom Browder
### -- File: lib/PDF/NameTags.rakumod
### ----------------------------------------------------
### -- Chunk 3 of 3
page-number = "Page 1 of 2";
    }
    else {
        $page-number = "Page 2 of 2";
    }
    $py = 0 + 100;
    $px = $p.page-width - 100;
    $font = %fonts<t>;
    $g.print: $page-number, :kern, :position[$px, $py], :align<right>, :$font,
              :font-size(15);

    $page.text: {
        my $lx = 0.5 * ($p.page-width - $para.content-width);
        .text-position = $lx, $p.page-height * 0.7;
        .print: $para;
    }

    $g.Restore;

} # sub make-printer-test-page

#===== run/help
# printers
our %printers is export = %(
    1 => {
          ofil => "test-toms-hp.pdf",
          name => "Tom's HP",
         },

    2 => {
          ofil => "test-gbumc-color.pdf",
          name => "GBUMC Color",
         },
    3 => {
          ofil => "test-gbz-ups-little-walmart.pdf",
          name => "UPS Store (GBZ, near 'little' Walmart)",
         },
    4 => {
          ofil => "test-gbz-ups-winn-dixie.pdf",
          name => "UPS Store (near Winn-Dixie)",
         },
    5 => {
          ofil => "test-office-depot-pcola-airport.pdf",
          name => "Office Depot (near P'cola Airport)",
         },
);

sub help() is export {
    print qq:to/HERE/;
    Usage: {$*PROGRAM.basename} go | <csv file> [...options...]

    Given a list of names, writes them on reversible paper
      for a two-sided name tag on Letter paper.
    The front side of the first two-sided page will contain
      job details (and the back side will be blank).

    Options:
      1|2     - Select option 1 (original method) or 2
                 (XForm object method), default: 1.

      show    - Gives details of the job based on the input name
                list and card dimension parameters, then exits.
                The information is the same as on the printed job
                cover sheet.

      p=N     - For printer N. See list by number, default: 1 (Tom's HP)

      ptest   - Create a printer test page for the selected printer.

      media=X - Where X is Letter or A4, default: Letter

    HERE
    exit
} # sub help() is export

sub run(@args) is export {

    #== from original bin file
    # TODO make more generic before publishing
    my $gbumc-dir = "./examples/GBUMC";
    # TODO create a file name with date and time included
    my $ofile = "Name-tags.pdf";
    # input data file: rose-glass-patterns.dat
    my $gfile = "$gbumc-dir/rose-glass-patterns.dat";
    my @names;
    #my $names-file = "$gbumc-dir/more-names.txt";
    my $names-file = "$gbumc-dir/less-names.txt";
    for $names-file.IO.lines {
        next if $_ ~~ /\h* '#'/;
        my @w = $_.words;
        my $last = @w.pop;
        my $first = @w.shift;
        $first ~= " " ~ @w.pop if @w;
        my $name = "$last $first";
        @names.push: $name;
    }
    @names .= sort;
    #== end chunk from original bin file

    my $show      = 0;
    my $debug     = 0;
    my $landscape = 0;
    my $go        = 0;
    my $clip      = 0;
    my $method    = 1;
    my $printer-num = 1;
    my $ptest     = 0;
    my $media     = "Letter";

    for @args {
        when /^ :i s/  { ++$show  }
        when /^ (1|2)/ { $method = +$0 }
        when /^ :i d/  { ++$debug }
        when /^ :i g/  { ++$go    }
        when /^ :i [m \S*]'=' (L|A) /  {
            $media = ~$0;
        }
        when /^ :i 'p=' (\d) $/ {
            $printer-num = +$0;
            unless %printers{$printer-num}:exists {
                say "WARNING: Unknown printer number $printer-num.";
                say "  Known printers:";
                my @keys = %printers.keys.sort;
                for @keys -> $k {
                    my $v =  %printers{$k}<name>;
                    say "    $k => '$v'";
                }
                exit;
            }
            say "Printer number $printer-num was selected";
        }
        when /^ :i p $/ {
            say "WARNING: No printer was selected. Use 'p=N'.";
            say "  For N of known printers:";
            my @keys = %printers.keys.sort;
            for @keys -> $k {
                my $v =  %printers{$k}<name>;
                say "    $k => '$v'";
            }
            exit;
        }
        when /^ :i [pt|pte|ptes|ptest] $/ {
            $ptest = 1;
        }
        default {
            say "Unknown arg '$_'...exiting.";
            exit;
        }
    }

    if $ptest {
        # get the printer name
        my $name = %printers{$printer-num}<name>;
        my $ofil = %printers{$printer-num}<ofil>;
        say "printer name = $name";

        make-printer-test-doc $ofil, :$name, :$media, :$debug;
        exit;
    }

    if $show {
        # TODO make a two-sided page of this:
        my ($nc, $nr, $hgutter, $vgutter) = show-nums;
        say "Badge width (inches):  {%dims<bwi>}";
        say "Badge height (inches): {%dims<bhi>}";

        say "Showing job details for portrait:";
        say "  number of badge columns: $nc";
        say "  number of badge rows:    $nr";
        say "  horizontal gutter space: $hgutter";
        say "  vertical gutter space:   $vgutter";
        say " Total badges: {$nc*$nr}";

        ($nc, $nr, $hgutter, $vgutter) = show-nums 1;
        say "Showing job details for landscape:";
        say "  number of badge columns: $nc";
        say "  number of badge rows:    $nr";
        say "  horizontal gutter space: $hgutter";
        say "  vertical gutter space:   $vgutter";
        say " Total badges: {$nc*$nr}";
        exit;
    }

    # cols 2, rows 4, total 8, portrait
    my @n = @names; # sample name "Mary Ann Deaver"

    my PDF::Lite $pdf .= new;
    $pdf.media-box = [0, 0, %dims<pw>, %dims<ph>];
    my $page;
    my $page-num = 0;
    while @n.elems {

        # a new page of names <= 8
        my @p = @n.splice(0,8); # weird name

        say @p.raku if 0 and $debug;

        say "Working front page..." if $debug;
        # process the front page
        $page = $pdf.add-page;

        # TODO put first and last name found in top margin
        ++$page-num;
        make-badge-page @p, :side<front>, :$page, :$page-num, :$debug,
        :$printer-num, :project-dir($gbumc-dir), :$method;

        say "Working back page..." if $debug;
        # process the back side of the page
        $page = $pdf.add-page;
        # TODO put first and last name found in top margin
        ++$page-num;
        make-badge-page @p, :side<back>, :$page, :$page-num, :$debug,
        :$printer-num, :project-dir($gbumc-dir), :$method;
    }

    # add page numbers: Page N of M
    # TODO compress to 300 dpi
    $pdf.save-as: $ofile;
    say "See name tags file: $ofile (using \$method $method)";

} # sub run(@args) is export

=finish
# there may be no need for this, see :verbatim and :squish
sub print-lines(
    @text,
    :$x!, $y!,
    :$font!,
    :$font-size!,
    :$align = "left",
    :$debug,
    --> List
    ) is export {

    # get the maximum line width
    my $lw = 0; # linewidth
    for @text {
        my $w = $font.stringwidth: $_, $font-size, :kern;
        $lw = $w if $w > $lw;
    }
    $px -= 0.5 * $lw;
    for $text.lines -> $line {
        $py -= $font-size;
        $g.print: $line, :kern, :position[$px, $py], :align<left>, :$font,
                  :$font-size;
    }

}

sub list-block-width(
    @text,
    :$font!,
    :$font-size!,
    :$debug,
    --> List
    ) is export {
} # list-block-width


