#!/usr/bin/perl
#-*-cperl-*-

use strict;

my @track_format = ( [ 1, 0 ], [ 1, 0 ], [ 1, 2, 3, 4, 0 ], [ 1, 2, 3, 4, 0 ], [ 1, 2, 3, 4, 0 ] );

foreach my $isrc ( @{ $track_format[0] } ) {
    foreach my $upc ( @{ $track_format[1] } ) {
        foreach my $artist ( @{ $track_format[2] } ) {
            foreach my $album ( @{ $track_format[3] } ) {
                foreach my $track ( @{ $track_format[4] } ) {
                    my $level = 100;

                    my @mod_points;

                    my $string = join( '', $isrc, $upc, $artist, $album, $track ), "\n";

                    foreach my $val ( $artist, $album, $track ) {
                        if ( $val == 0 ) {
                            push @mod_points, -5;
                        } else {
                            my $mod = 1 - $val;
                            push( @mod_points, $mod ) if $mod < 0;
                        }
                    }

                    if ( all_equal( 0, $isrc, $track ) ) {
                        push @mod_points, -25;
                    } elsif ( $isrc == 1 && $track > 0 ) {
                        push @mod_points, ( 16 - $track );
                    } elsif ( $isrc == 1 ) {
                        push @mod_points, 5;
                    }

                    if ( any_equal( 0, $isrc, $track ) ) {
                        push @mod_points, -5;
                    }

                    if ( all_equal( 0, $upc, $album ) ) {
                        push @mod_points, -5;
                    } elsif ( $upc == 1 && $album > 0 ) {
                        push @mod_points, ( 8 - $album );
                    } elsif ( $upc == 1 ) {
                        push @mod_points, 1;
                    }

                    if ( any_equal( 0, $upc, $album ) ) {
                        push @mod_points, -1;
                    }

                    foreach my $mod (@mod_points) {
                        $level += $mod;
                    }

                    # if we have the sensible bare minimum req'd,
                    # raise the level to our min threshold if necessary
                    if ( $level < 90 && ( ( $track && ( $album || $upc || $isrc ) ) ) ) {
                        my $mod = 90 - $level;
                        $level += $mod;
                        push @mod_points, $mod;
                    }

                    my $mod_points_joined = join( ',', @mod_points );
                    print join( "\t", 'T', $string, $level, '\N', $mod_points_joined ), "\n";

                }
            }
        }
    }
}

my @album_format = ( [ 1, 0 ], [ 1, 2, 3, 4, 0 ], [ 1, 2, 3, 4, 0 ] );

foreach my $upc ( @{ $album_format[0] } ) {
    foreach my $artist ( @{ $album_format[1] } ) {
        foreach my $album ( @{ $album_format[2] } ) {
            my $level = 100;

            my @mod_points;

            my $string = join( '', $upc, $artist, $album ), "\n";

            if ($upc) {
                push @mod_points, 15;
            } else {
                push @mod_points, -15;
            }

            if ($artist) {
                my $mod = 6 - $artist;
                push @mod_points, $mod;
            } else {
                push @mod_points, -5;
            }

            if ($album) {
                my $mod = 11 - $album;
                push @mod_points, $mod;
            } else {
                push @mod_points, -10;
            }

            # synergy points
            if ( $upc && $artist && $album ) {
                my $mod = 17 - ( $artist + $album );
                push @mod_points, $mod;
            } elsif ( $artist && $album ) {
                my $mod = 12 - ( $artist + $album );
                push @mod_points, $mod;
            } elsif ( $upc && ( $artist || $album ) ) {
                my $mod = 7 - ( $artist + $album );
                push @mod_points, $mod;
            }

            # penalty points for zeroes
            my $mod = 5 * num_equal( 0, $upc, $artist, $album );
            if ($mod) {
                push @mod_points, -$mod;
            }

            foreach my $mod (@mod_points) {
                $level += $mod;
            }

            # if we have the sensible bare minimum req'd,
            # raise the level to our min threshold if necessary
            if ( $level < 90 && ( $upc || ( $artist && $album ) ) ) {
                my $mod = 90 - $level;
                $level += $mod;
                push @mod_points, $mod;
            }

            my $mod_points_joined = join( ',', @mod_points );
            print join( "\t", 'A', $string, $level, '\N', $mod_points_joined ), "\n";
        }
    }
}

sub any_equal {
    my $value = shift;
    my @items = @_;

    foreach my $item (@items) {
        return 1 if $item eq $value;
    }

    return undef;
}

sub all_equal {
    my $value = shift;
    my @items = @_;

    foreach my $item (@items) {
        return undef if $item ne $value;
    }

    return 1;
}

sub num_equal {
    my $value = shift;
    my @items = @_;

    my $num_equal = 0;
    foreach my $item (@items) {
        $num_equal++ if $item eq $value;
    }

    return $num_equal;
}

