#!perl
use strict;
use warnings;

# PODNAME: mcp
# ABSTRACT: pick multiple choice questions randomly

use utf8;
binmode STDOUT, ":utf8";
use YAML::Tiny 'LoadFile';
use HTML::Entities;
use Getopt::Args;
use Data::Dumper;

sub say { print "@_\n" } # say for perl below 5.10

arg quantity => (
    isa => 'Int',
    required => 1,
    comment  => 'Number of questions to pic',
);

arg file => (
    isa      => 'ArrayRef',
    greedy   => 1,
    required => 1,
    comment  => 'Path to YAML file, containg multiple choice questions',
);

my $o = optargs;

my @yaml_documents;
foreach my $file (@{$o->{file}}) {
    my @yaml_file = LoadFile( $file );
    push @yaml_documents, @yaml_file;
}

# document default config
my %c = ( lang => { dunno  => "don't know",
                    answer => 'Answer',
                    claim  => 'Claim',
                    yes    => 'true',
                    no     => 'false',
                  }
        );
my @q; # questions

foreach my $document (@yaml_documents) {

    if ( ref $document eq 'HASH'
             and exists $document->{config}
             and $document->{config} eq 'true') {

        # configuration detected
        my %new_conf = %{$document};

        # overload default config with new, if found
        $c{lang}{dunno}  = $new_conf{lang}{dunno}  if $new_conf{lang}{dunno};
        $c{lang}{answer} = $new_conf{lang}{answer} if $new_conf{lang}{answer};
        $c{lang}{claim}  = $new_conf{lang}{claim}  if $new_conf{lang}{claim};
        $c{lang}{yes}    = $new_conf{lang}{yes}    if $new_conf{lang}{yes};
        $c{lang}{no}     = $new_conf{lang}{no}     if $new_conf{lang}{no};
    }
    elsif ( ref $document eq 'HASH'
                and exists $document->{config}
                and $document->{config} eq 'false') {

        # configuration detected, but disabled
        # nothing the see here, move on
        say   STDERR 'skipping disbaled configuration:';
        print STDERR Dumper $document;
    }
    elsif (ref $document eq 'ARRAY') {
        # no configuration detected
        push @q, @{$document};
    }
    else {
        say   STDERR 'skipping unexpected YAML document format:';
        print STDERR Dumper $document;
    }
}

my $question_count = scalar @q;

# Choose questions randomly

my $index;
my @choosen = ();

for (1..$o->{quantity}) {
    $index = int(rand(scalar @q));
    my $question = $q[$index];

    # if multiple subquestions, choose subquestion and override
    if (ref($question) eq 'ARRAY') {
    my $index = int(rand(scalar @{$question}));
    $question = $question->[$index];
    }

    push @choosen, $question;
    #say $question->{q};

    # delete index from array
    splice(@q, $index, 1);
}

# print choosen questions

say '<!DOCTYPE html>';
say '<html>';
say '<head>';
say '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
say '<style>td { text-align: center; vertical-align: middle; }</style>';
say '<title>mc survey</title>';
say '</head>';
say '<body>';
say '<h1>Questions</h1>';
say $o->{quantity} . " randomly picked questions out of $question_count.";
say '<table border="1">';
say "<tr><th>$c{lang}{answer}</th><th>$c{lang}{claim}</th></tr>";

my $difficulty_sum = 0;
my $difficulty_avg = 0;
my $themen_count = {};

for my $question (@choosen) {

    $difficulty_sum += $question->{difficulty};
    if (exists $themen_count->{$question->{chapter}} ) {
        $themen_count->{$question->{chapter}}++;
    }
    else {
        $themen_count->{$question->{chapter}} = 1;
    }

    say '<tr>';
    say '<td>';
    if (ref($question->{answer}) eq 'HASH') {
        while (my ($answer, $bool) = each(%{$question->{answer}})) {
            say "\x{2610} $answer<br>" if $bool =~ /false/i;
            say "\x{2611} $answer<br>" if $bool =~ /true/i;
        }
    }
    else {
        say "\x{2610} $c{lang}{yes}<br>\x{2611} $c{lang}{no}<br>" if $question->{answer} =~ /false/i;
        say "\x{2611} $c{lang}{yes}<br>\x{2610} $c{lang}{no}<br>" if $question->{answer} =~ /true/i;
    }
    say "\x{2610} $c{lang}{dunno}";
    say '</td>';
    say '<td>';
    say encode_entities($question->{q});
    say '</td>';
    say '</tr>';

}

$difficulty_avg = $difficulty_sum / int scalar @choosen;

say '</table>';

say '<h1>Statistics</h1>';

say '<p>';
say "Average Difficulty $difficulty_avg (1=simplest; 6=most difficult)";
say '</p>';
say '<p>';
say 'Occurence of Chapters<br />';

for my $chap (keys %{$themen_count}) {
    say "Chapter $chap";
    say ':&nbsp;';
    say $themen_count->{$chap};
    say '<br />';
}
say '</p>';

say '</body>';
say '</html>';

__END__

=pod

=encoding UTF-8

=head1 NAME

mcp - pick multiple choice questions randomly

=head1 VERSION

version 0.004

=head1 SYNOPSIS

THIS IS AN EARLY RELEASE. YOU PROBABLY SHOULD NOT USE IT, UNLESS YOU KNOW WHY.

Pick 20 entries from yaml and print them to html:

  mcp 20 myfile.yml > yourfile.html

=head1 AUTHOR

Boris Däppen <bdaeppen.perl@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2019 by Boris Däppen.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
