## DESCRIPTION
## Match each of the following biofuel types with their corresponding descriptions.
## ENDDESCRIPTION
## KEYWORDS('biofuel types','descriptions')
## DBsubject('Biotechnology')
## DBchapter('')
## DBsection('')
## Date('2026-11-18')
## Author('Neil R. Voss')
## Institution('Roosevelt University')

DOCUMENT();

loadMacros(
    'PGstandard.pl',
    'PGML.pl',
    'PGchoicemacros.pl',
    'parserPopUp.pl',
    'parserUtils.pl',
    'PGgraders.pl',
    'PGcourse.pl'
);
our @ALPHABET = ('A' .. 'Z');

# ================================
# Full matching data
# ================================
%match_data = (
  'Starch-based Biofuels' => [
    'Corn kernels used to make ethanol',
    'Ethanol made from sugars released from corn grain',
    'Fuel made by breaking down grain into sugars and fermenting them',
    'Using edible grain to make ethanol competes with using it for food',
    'Making fuel from corn grain competes with food production',
    'Using grain for fuel leaves less of that grain available for food',
  ],
  'Cellulose-based Biofuels' => [
    'Wood, grasses, or crop stalks used to make ethanol',
    'Ethanol made from sugars released from wood and crop stalks',
    'Fuel made from the tough cell walls of grasses and other plants',
    'Releasing sugars from tough plant cell walls takes extra processing',
    'Tough plant cell walls must be broken down before their sugars can be fermented',
    'Getting usable sugars out of wood and crop stalks requires extra processing',
  ],
  'Conventional Biodiesel' => [
    'Vegetable oils, animal fats, or used cooking oil',
    'Fuel made by chemically processing vegetable oils or animal fats',
    'Used frying oil processed into fuel for diesel engines',
    'Water and free fatty acids in used cooking oil complicate processing',
    'Used frying oil may need treatment to remove water before making fuel',
    'Free fatty acids in used cooking oil can interfere with making the fuel',
  ],
  'Algae-based Biodiesel' => [
    'Oils from microscopic photosynthetic cells grown in ponds or tanks',
    'Fuel made from oils collected from tiny photosynthetic cells grown in water',
    'Oils produced by microscopic pond-grown cells and processed into diesel fuel',
    'Extracting oil from microscopic cells requires separating it from other cell material',
    'Getting oil out of microscopic cells and away from the cell debris is difficult',
    'Oil from microscopic photosynthetic cells must be separated from the rest of the cell contents',
  ],
);

# -------------------------------
# Select N random keys
# -------------------------------
my $n = 4;
@all_keys = PGsort(sub { $_[0] lt $_[1] }, keys %match_data);
my $local_seed = (defined($problemSeed) && $problemSeed ne '') ? $problemSeed : 1;
my $local_random = new PGrandom($local_seed);

my @indices = (0 .. $#all_keys);
my @shuffled = ();
while (@indices) {
  my $pick = $local_random->random(0, $#indices, 1);
  push @shuffled, splice(@indices, $pick, 1);
}
my @selected_keys = @all_keys[@shuffled[0..$n-1]];

# -------------------------------
# Build question/answer pairs
# -------------------------------
# Each entry: [prompt, choice]
@q_and_a = ();
foreach my $key (@selected_keys) {
  my $values_ref = $match_data{$key};
  my $i = $local_random->random(0, $#$values_ref, 1);
  my $value = $values_ref->[$i];
  push @q_and_a, [ $value, $key ];
}

# -------------------------------
# Randomize the questions
# -------------------------------
my @q_indices = (0 .. $#q_and_a);
my @q_shuffled = ();
while (@q_indices) {
  my $pick = $local_random->random(0, $#q_indices, 1);
  push @q_shuffled, $q_and_a[splice(@q_indices, $pick, 1)];
}
@q_and_a = @q_shuffled;

# -------------------------------
# Sort the choices alphabetically
# -------------------------------
@answers = ();
push(@answers, (map { $_->[1] } @q_and_a));
@answers_sorted = PGsort(sub { $_[0] lt $_[1] }, @answers);

# -------------------------------
# HTML-safe answer labels
# -------------------------------
%answer_html = (
  'Starch-based Biofuels' => '<span style="color: #b74300; font-weight:700;">Starch-based Biofuels</span>',
  'Cellulose-based Biofuels' => '<span style="color: #3b7600; font-weight:700;">Cellulose-based Biofuels</span>',
  'Conventional Biodiesel' => '<span style="color: #007576; font-weight:700;">Conventional Biodiesel</span>',
  'Algae-based Biodiesel' => '<span style="color: #0067cc; font-weight:700;">Algae-based Biodiesel</span>',
);

@answers_sorted_html = map { $answer_html{$_} || $_ } @answers_sorted;

# -------------------------------
# Create answer index lookup
# -------------------------------
our %answer_index;
for (my $i = 0; $i <= $#answers_sorted; $i++) {
  $answer_index{$answers_sorted[$i]} = $i;
}

# -------------------------------
# PopUp/DropDown compatibility
# -------------------------------
sub make_popup {
  return defined &DropDown ? DropDown(@_) : PopUp(@_);
}

# -------------------------------
# Create popup objects (blank default)
# -------------------------------
my @answer_letters = @ALPHABET[0 .. $#answers_sorted];
my @answer_letters_with_blank = ('', @answer_letters);
@answer_dropdowns =
  map { make_popup([ @answer_letters_with_blank ], $answer_index{$q_and_a[$_][1]} + 1 ) }
  0 .. $#q_and_a;

# -------------------------------
# Render the question
# -------------------------------
HEADER_TEXT(<<END_STYLE);
<style>
.pgml-bold { font-weight: 700; }
.two-column {
    display: flex;
	flex-wrap: wrap;
	gap: 2rem;
	align-items: center;
	justify-content: space-evenly;
}
</style>
END_STYLE

$question_text = 'Match each biofuel type with its description.';
$note_text = 'Note: Each choice will be used exactly once.';

BEGIN_PGML
[$question_text]*
[$note_text]*

[@ MODES(HTML => '<div class="two-column"><div>') @]*
[@ join(
    "\n\n",
    map {
        '[_]{$answer_dropdowns[' . $_ . ']} '
            . '*' . ($_ + 1) . '.* '
            . '[$q_and_a[' . $_ . '][0]]*'
    } 0 .. $#q_and_a
) @]**
[@ MODES(HTML => '</div><div class="right-col">') @]*
[@ join(
    "\n\n",
    map {
        chr(65 + $_) . '\\.' . ' ' . '[$answers_sorted_html[' . $_ . ']]*'
    } 0 .. $#answers_sorted
) @]**
[@ MODES(HTML => '</div></div>') @]*
END_PGML

# -------------------------------
# Dynamic Partial Credit Based on $n
# -------------------------------
$showPartialCorrectAnswers = 0;
my @thresholds;
my @scores;
for (my $i = 1; $i <= $n; $i++) {
  push @thresholds, $i;
  push @scores, sprintf("%.2f", $i / $n);
}

install_problem_grader(~~&custom_problem_grader_fluid);
$ENV{grader_numright} = [@thresholds];
$ENV{grader_scores}   = [@scores];
$ENV{grader_message} = 'You can earn partial credit.';

# -------------------------------
# Solution
# -------------------------------
$answer_list = join(', ', map { ($_ + 1) . '-' . $ALPHABET[$answer_index{$q_and_a[$_][1]}] } 0 .. $#q_and_a);
BEGIN_PGML_SOLUTION
The correct answers are: [$answer_list].
END_PGML_SOLUTION

ENDDOCUMENT();

