#!/usr/bin/perl

use strict;
use warnings;

# --- Configuration: Full List of 62 Special Bonuses ---
my @SPECIAL_BONUSES = (
    "Ambush", "Assassin", "Backfire", "Bloodlust", "Brawn", "Burn", "Coupe de Grace", 
    "Cripple", "Deadeye", "Demoralize", "Disarm", "Disrupt", "Echo", "Eviscerate", 
    "Execute", "Expose", "Feint", "Fester", "Freeze", "Fury", "Lacerate", "Last stand", 
    "Motivate", "Parry", "Plunder", "Poison", "Precision", "Quicken", "Ransack", 
    "Revitalize", "Smurf", "Specialist", "Speed", "Suppress", "Sunder", "Toxin", 
    "Valor", "Venom", "Vulnerability", "Warlord", "Weaken", "Wind-up", 
    "Sure shot", "Rage", "Take down", "Sever", "Overload", "Life steal", 
    "Flurry", "Empower", "Concentration", "Conserve", "Assassinate", 
    "Unload", "Shred", "Stricken", "Bleed", "Blunt", "Penetrate", "Shredder", 
    "Smash" 
);

# --- Function Definitions (Same Logic) ---

sub simplify_bonus {
    my ($bonus_str) = @_;
    return '-' unless defined $bonus_str && length $bonus_str > 0;

    if ( $bonus_str =~ /^([A-Za-z\s]+)\s*\((.*?)\)$/ ) {
        my ($name, $description) = ($1, $2);

        if ( $description =~ /([\d\.]+%)/ ) {
            my $value = $1;
            return "$name ($value)";
        }
        
        return "$name ($description)";
    }
    
    return $bonus_str;
}

sub filter_and_split_bonuses {
    my ($bonus_string) = @_;
    my @special_bonuses_found;

    my @raw_bonuses = split(/^-/, $bonus_string);
    
    foreach my $raw_bonus (@raw_bonuses) {
        $raw_bonus =~ s/^\s*[-*]\s*//;
        $raw_bonus =~ s/\s*\n\s*//g;
        $raw_bonus =~ s/\|//g;

        next unless length $raw_bonus > 0;
        
        my $clean_bonus = $raw_bonus;
        $clean_bonus =~ s/^\s+|\s+$//g;
        
        my $name_check = $clean_bonus;
        $name_check =~ s/\s*\(.*//;
        $name_check =~ s/[^\w\s]//g;
        $name_check =~ s/^\s+|\s+$//g;
        
        if (grep { $_ eq $name_check } @SPECIAL_BONUSES) {
            if ($clean_bonus =~ /^([A-Za-z\s]+)\s*\((.*?)\)$/) {
                 my ($name, $description) = ($1, $2);
                 push @special_bonuses_found, simplify_bonus("$name ($description)");
            } else {
                 push @special_bonuses_found, simplify_bonus($clean_bonus);
            }
        }
    }

    while (scalar @special_bonuses_found < 2) {
        push @special_bonuses_found, "-";
    }
        
    return @special_bonuses_found[0..1];
}


# --- Main Logic (Refactored for Stability) ---

# Read all data from standard input (STDIN).
my $raw_data = do { local $/; <STDIN> };

# 1. Remove source tags and pipe characters (uses simple delimiters)
#$raw_data =~ s/\//g;
$raw_data =~ s/\|\s*/ /g;

# 2. Normalize HOU5E's armour block (uses a safe capture and replace)
# Captures everything between "Armour:" and the "Boots" keyword, replacing it with "Full Set: Vanguard"
# The 's' modifier allows '.' to match newlines.
$raw_data =~ s/(Armour:\s*.*?Boots)/Full Set: Vanguard/s;

# 3. Normalize multi-line entries into a single line (Loop-based for safety)
# This finds a newline followed by three or more spaces (or a tab), and replaces the newline with a single space.
# This prevents premature termination errors common with complex multi-line regexes.
my $counter = 0;
do {
    # Replace newline followed by whitespace/dash with a single space (flattens lines)
    $counter = $raw_data =~ s/\n(\s+[-A-Za-z0-9])/\s$1/g;
} while ($counter > 0);


# Split the data into blocks per player, using 'Name:' as the separator
my @player_blocks = split(/\nName: /, "\n" . $raw_data);
shift @player_blocks;

# Prepare output array and header
my @output;
push @output, join("\t", 'Player Name', 'Full Set/Armour', 'Temp Item', 
                        'Primary Weapon', 'P_Bonus 1', 'P_Bonus 2', 
                        'Secondary Weapon', 'S_Bonus 1', 'S_Bonus 2', 
                        'Melee Weapon', 'M_Bonus 1', 'M_Bonus 2');

# Process each player block (same robust logic as before)
foreach my $block (@player_blocks) {
    my %data;
    $data{'Player Name'} = ($block =~ /^([A-Za-z0-9]+)/m) ? $1 : '-';
    
    $data{'Full Set/Armour'} = ($block =~ /Full Set:\s*([A-Za-z\s]+)/) ? $1 : 
                               ( $block =~ /helmet:|chest:|gloves:|pants:|boots:/i ? 'Mixed' : '-' );

    $data{'Temp Item'} = ($block =~ /Temp:\s*([A-Za-z\s]+)/) ? $1 : '-';

    # --- Primary Weapon ---
    my $primary_match = $block =~ /Primary:\s*(.*?), damage/ ? $1 :
                        $block =~ /Stoner 96:/ ? 'Stoner 96' : '-';
    $data{'Primary Weapon'} = $primary_match;
    
    my $p_bonuses_raw = ($block =~ /Primary:.*?Bonuses:\s*(.*?)(?=\n\n|\nSecondary:|\nMelee:|$)/s) ? $1 : 
                        ($block =~ /Stoner 96:.*?Bonus:\s*(.*?)(?=BT MP9:)/s) ? $1 : '';
    my @p_bonuses = filter_and_split_bonuses($p_bonuses_raw);
    $data{'P_Bonus 1'} = $p_bonuses[0];
    $data{'P_Bonus 2'} = $p_bonuses[1];

    # --- Secondary Weapon ---
    my $secondary_match = $block =~ /Secondary:\s*(.*?), damage/ ? $1 :
                          $block =~ /BT MP9:/ ? 'BT MP9' : '-';
    $data{'Secondary Weapon'} = $secondary_match;
    
    my $s_bonuses_raw = ($block =~ /Secondary:.*?Bonuses:\s*(.*?)(?=\n\n|\nMelee:|\nTemp:|$)/s) ? $1 : 
                        ($block =~ /BT MP9:.*?Bonus:\s*(.*?)(?=Kodachi:)/s) ? $1 : '';
    my @s_bonuses = filter_and_split_bonuses($s_bonuses_raw);
    $data{'S_Bonus 1'} = $s_bonuses[0];
    $data{'S_Bonus 2'} = $s_bonuses[1];

    # --- Melee Weapon ---
    my $melee_match = $block =~ /Melee:\s*(.*?), damage/ ? $1 :
                      $block =~ /Kodachi:/ ? 'Kodachi' : '-';
    $data{'Melee Weapon'} = $melee_match;
    
    my $m_bonuses_raw = ($block =~ /Melee:.*?Bonuses:\s*(.*?)(?=\n\n|\nTemp:|$)/s) ? $1 :
                        ($block =~ /Kodachi:.*?Bonus:\s*(.*?)(?=Smoke Grenade)/s) ? $1 : '';
    my @m_bonuses = filter_and_split_bonuses($m_bonuses_raw);
    $data{'M_Bonus 1'} = $m_bonuses[0];
    $data{'M_Bonus 2'} = $m_bonuses[1];

    # Final output line
    push @output, join("\t", 
        $data{'Player Name'}, $data{'Full Set/Armour'}, $data{'Temp Item'}, 
        $data{'Primary Weapon'}, $data{'P_Bonus 1'}, $data{'P_Bonus 2'}, 
        $data{'Secondary Weapon'}, $data{'S_Bonus 1'}, $data{'S_Bonus 2'}, 
        $data{'Melee Weapon'}, $data{'M_Bonus 1'}, $data{'M_Bonus 2'}
    );
}

# Print the final output (tab-separated)
print join("\n", @output), "\n";

