#!/usr/bin/perl -w

# This software is being released under the GNU General Public License v3
# See https://www.gnu.org/licenses/gpl.txt

use strict;

use Getopt::Long;

my $help;
my $file  = '';
my $v_rhp = 2000;
my $v_lhp =  500;
my $split = 0.01;

my $ret = GetOptions( 'file=s'     => \$file
                    , 'left=f'     => \$v_lhp
                    , 'right=f'    => \$v_rhp
                    , 'split=f'    => \$split
                    , 'help'       => \$help
                    );

if ($help) {
    print STDERR "platoon.pl\n";
    print STDERR "Read through platoon data and look for reverse splits\n";
    print STDERR "\n";
    print STDERR "-f  --file     CSV file to read.\n";
    print STDERR "-h  --help     This help message.\n";
    print STDERR "-l  --left     Minimum PA vs LHP for inclusion. (default = $v_lhp)\n";
    print STDERR "-r  --right    Minimum PA vs RHP for inclusion. (default = $v_rhp)\n";
    print STDERR "-s  --split    Minimum reverse split. (default = $split)\n";
    exit 0;
}

my $years;

open FILE, "<$file";

my $header = <FILE>;
chomp $header;
my @cols   = split ',', $header;

sub woba {
    my $r = $_[0];
    my $num = 0.7 *($r->{BB} - $r->{IBB} + $r->{HBP}) + 0.9 * ($r->{'1B'} + $r->{ROE}) + 1.25 * $r->{'2B'} + 1.6 * $r->{'3B'} + 2 * $r->{HR} + .25 * $r->{SB} - .5 * $r->{CS};
    my $den = $r->{PA} - $r->{IBB} - $r->{SH};

    return $den ? $num / $den : 0;
}

my @sum_stats = ('1B', '2B', '3B', 'HR', 'BB', 'IBB', 'HBP', 'ROE', 'SB', 'CS', 'PA', 'SH');
my $map = { 'vs LHP as RHB' => 'L'
          , 'vs RHP as RHB' => 'R' };

while (<FILE>) {
    chomp;
    my @c = split ',', $_;
    my $ii = 0;
    my $r = { };
    for my $c (@c) {
        $r->{$cols[$ii++]} = $c;
    }
    $r->{'1B'} = $r->{H} - $r->{'2B'} - $r->{'3B'} - $r->{'HR'};
    $r->{wOBA} = woba($r);

    $years->{$r->{result_batter}}{$r->{year_game}}{$r->{split_name}} = $r;
}

my $before;
print "                             Before                                   After\n";
print "                        vs LHP     vs RHP                      vs LHP       vs RHP\n";
print "Player     years     wOBA    PA  wOBA   PA  Split   years    wOBA   PA    wOBA   PA  Split\n";
my ($bsum, $asum) = ({ }, { });
for my $p (keys %{$years}) { # Players
    my $b = { };
    my $a = { };
    my $t = $b;
    for my $y (sort keys %{$years->{$p}}) { # Years
        for my $hand (keys %{$years->{$p}{$y}}) { # Handedness
            my $h = $map->{$hand};
            next if (!$h);
            for my $s (@sum_stats) {
                $t->{$h}{$s}    += $years->{$p}{$y}{$hand}{$s};
            }
            $t->{syear} = $y if (!$t->{syear});
            $t->{eyear} = $y;
        }
        if ($t->{L} && $t->{L}{PA} >= $v_lhp &&
            $t->{R} && $t->{R}{PA} >= $v_rhp) {
            $t = $a;
        }
    }
    if ($t eq $a && scalar keys %{$a}) {
        $b->{L}{wOBA} = woba($b->{L});
        $b->{R}{wOBA} = woba($b->{R});
        $b->{PA} = $b->{R}{PA} + $b->{L}{PA};
        my $bsplit = $b->{R}{wOBA} - $b->{L}{wOBA};
        if ($bsplit > $split) {
            $a->{PA} = $a->{R}{PA} + $a->{L}{PA};
            $a->{L}{wOBA} = woba($a->{L});
            $a->{R}{wOBA} = woba($a->{R});
            my $asplit = $a->{R}{wOBA} - $a->{L}{wOBA};
            printf "%-9s %4d-%4d %.3f  %4d %.3f  %4d %.3f %4d-%4d %.3f  %4d %.3f  %4d %6.3f\n", $p, $b->{syear}, $b->{eyear}, $b->{L}{wOBA}, $b->{L}{PA}, $b->{R}{wOBA}, $b->{R}{PA}, $bsplit, $a->{syear}, $a->{eyear}, $a->{L}{wOBA}, $a->{L}{PA}, $a->{R}{wOBA}, $a->{R}{PA}, $asplit;
            map { $bsum->{L}{$_} += $b->{L}{$_};
                  $bsum->{R}{$_} += $b->{R}{$_};
                  $asum->{L}{$_} += $a->{L}{$_};
                  $asum->{R}{$_} += $a->{R}{$_};
            } @sum_stats;
        }
    }
}

if ($bsum->{L}{PA} && $bsum->{R}{PA} && $asum->{L}{PA} && $asum->{R}{PA}) {
    $bsum->{L}{wOBA} = woba($bsum->{L});
    $bsum->{R}{wOBA} = woba($bsum->{R});
    my $bsplit = $bsum->{R}{wOBA} - $bsum->{L}{wOBA};
    $asum->{L}{wOBA} = woba($asum->{L});
    $asum->{R}{wOBA} = woba($asum->{R});
    my $asplit = $asum->{R}{wOBA} - $asum->{L}{wOBA};

    printf "Total               %.3f %5d %.3f %5d %.3f           %.3f %5d %.3f %5d %6.3f\n", $bsum->{L}{wOBA}, $bsum->{L}{PA}, $bsum->{R}{wOBA}, $bsum->{R}{PA}, $bsplit, $asum->{L}{wOBA}, $asum->{L}{PA}, $asum->{R}{wOBA}, $asum->{R}{PA}, $asplit;
}
