#!/usr/bin/perl

use strict;
#use Data::Dumper;


my $filein 		= $ARGV[0];
my $inhalt;

my $maxEntrys 	= $ARGV[1]; # how much entries does the orignal CSV file have?

warn "I DIED: perl $0 infile.csv MaxCounts_in_Decimal\n";

open(C,"<$filein") or die;
my @content	= <C>;
my $header = @content[0];
close C;
#print Dumper @content; exit;

my %hash = ();
my $hash = {};

my $result_codes;
my %result_codes = (

"NS01" => "NS01",
"NS02" => "NS02",
"NS03" => "NS03",
"NS04" => "NS04",
"NS05" => "NS05",
"NS06" => "NS06",
"NS07" => "NS07",
"NS08" => "NS08",
"NS99" => "NS99",
"NE01" => "NE01",
"NE02" => "NE02",
"NE03" => "NE03",
"NE04" => "NE04",
"NE05" => "NE05",
"NE06" => "NE06",
"NE08" => "NE08",
"NE99" => "NE99",
"AC01" => "AC01",
"AC02" => "AC02",
"AC03" => "AC03",
"AC09" => "AC09",
"AC10" => "AC10",
"AC11" => "AC11",
"AC12" => "AC12",
"AC13" => "AC13",
"AC15" => "AC15",
"AC16" => "AC16",
"AC17" => "AC17",
"AC18" => "AC18",
"AC19" => "AC19",
"AC20" => "AC20",
"AE01" => "AE01",
"AE02" => "AE02",
"AE03" => "AE03",
"AE05" => "AE05",
"AE08" => "AE08",
"AE09" => "AE09",
"AE10" => "AE10",
"AE11" => "AE11",
"AE12" => "AE12",
"AE13" => "AE13",
"AE14" => "AE14",
"AE17" => "AE17",
"AV11" => "AV11",
"AV12" => "AV12",
"AV13" => "AV13",
"AV14" => "AV14",
"AV15" => "AV15",
"AV21" => "AV21",
"AV22" => "AV22" ,
"AV23" => "AV23",
"AV24" => "AV24",
"AV25" => "AV25",
"GE01" => "GE01",
"GE02" => "GE02",
"GS01" => "GS01",
"GS02" => "GS02",
"GS03" => "GS03",
"GS04" => "GS04",
"GS05" => "GS05",
"GS06" => "GS06",
"MS01" => "MS01",
"MS02" => "MS02",
"MS03" => "MS03"
);

my %hashCounter = ();
my $hashCounter = {};
my $count = 0;
foreach my $entry (@content){
    chomp($entry);
	if ($count == 0){
		$count++;
		next;
	} else {
		my @content 		= split(";", $entry);
		my $new_entry_line 	= "";
		my $finalPositionForResultCode = 0;
				
		for (my $i=0;$i<=scalar(@content);$i++){
			#if (@content[$i] =~ /[\w{2}+\d{2}]\,[\w{2}+\d{2}]/g ||exists($result_codes{@content[$i]})){
			if (@content[$i] =~ /[A-Z]{2}[0-9]{2}\,[A-Z]{2}[0-9]{2}/g){
				#print "Wert von I: $i --->>> Inhalt Result codes: @content[$i]\n";
				my @CommaContent = split(",", @content[$i]);
				for (my $k=0;$k<=scalar(@CommaContent);$k++){
					if (exists($result_codes{@CommaContent[$k]})){
						#print "Result Code found: @CommaContent[$k]\n";
						$hashCounter{@CommaContent[$k]}++;
					}
				}
				last;
			} elsif ( length(@content[$i]) == 4 && exists($result_codes{@content[$i]})){
				#print "Single line Result Code: @content[$i]\n";
				$hashCounter{@content[$i]}++;
			}
		}
	}
	$count++;
}

open(W,">$filein-RESULTCODES.txt");

foreach my $name (sort keys %hashCounter) {
   
	printf W "Result Code: %s -> Hit Counts: %s -> Percentage: %s \% -> All Result Count: %s\n", $name, $hashCounter{$name}, sprintf("%.4f", (($hashCounter{$name}/$maxEntrys)*100)), $maxEntrys;
	print W "\n";
	
	printf "Result Code: %s -> Hit Counts: %s -> Percentage: %s \% -> All Result Count: %s\n", $name, $hashCounter{$name}, sprintf("%.4f", (($hashCounter{$name}/$maxEntrys)*100)), $maxEntrys;
	print "\n";
}
print "$count entries parsed\n";
#
#while (my ($key, $value) = each %hashCounter) {
#	print "Result Code $key --> Count: $value\n";
#	print W "$key --> Count: $value\n";
#}
close W;

sub trim { my $s = shift; $s =~ s/^\s+//;$s =~ s/\s+$//;$s =~ s/^\s+|\s+$//g; return $s };