package RPS::Import::NineSquared;

use strict;

use Date::Calc qw(Days_in_Month);
use Spreadsheet::ParseExcel;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_date);
use Common::Consts;

use lib '/app/tools/data_classes/lib';

#private constants
use constant FIELD_STARTDATE  => 0;
use constant FIELD_VENDORID   => 3;
use constant FIELD_ARTIST     => 1;
use constant FIELD_TITLE      => 5;
use constant FIELD_UNITS      => 11;
use constant FIELD_TOTALPRICE => 14;

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

#public methods

sub _importLines {
    my $self = shift;
    my %args = @_;

    #
    # The NineSquared report comes in 3 sheets of data.
    # Each sheet is for a particular month in a quarter.
    # So, get our data from the 'sheets' param, not the
    # 'lines' param.
    #
    my $retval      = undef;
    my $sheets      = $args{sheets};
    my $sheet_names = $args{sheet_names};
    my $linenum;
    my $sheet_count = 0;

    if ($sheets) {
        foreach my $sheet (@$sheets) {
            my $dateBegin;
            my $dateEnd;
            if ( $sheet_names->[$sheet_count] !~ /summary/ ) {
                $linenum = 1;
                foreach my $line (@$sheet) {
                    my ( $dateBegin, $dateEnd ) = _getDates( $line->[FIELD_STARTDATE] );
                    my $prodtype = RPS::File::Sale::TYPE_TRACK;
                    my $format   = RPS::File::Sale::FORMAT_RINGTONE;

                    my $vendorID    = $line->[FIELD_VENDORID];
                    my $artist      = $line->[FIELD_ARTIST];
                    my $title       = $line->[FIELD_TITLE];
                    my $track       = $title;
                    my $units       = $line->[FIELD_UNITS];
                    my $total_price = $line->[FIELD_TOTALPRICE];
                    my $price       = $total_price / $units if ( $units > 0 );

                    if ( $units && $price && $artist && $title ) {
                        unless ( $dateBegin && $dateEnd ) {
                            print STDERR "Could not get dates from l:" . $linenum . " d:" . $line->[FIELD_STARTDATE] . "\n";
                            $self->errstr( "couldn't get dates from l:" . $linenum . " d:" . $line->[FIELD_STARTDATE] );
                            return undef;
                        }
                        my $sale = RPS::Import::SaleRec->new();

                        $sale->productType($prodtype);
                        $sale->formatType($format);
                        $sale->dateBegin($dateBegin);
                        $sale->dateEnd($dateEnd);

                        # $sale->serviceProductID($vendorID);
                        # $sale->clientProductID($clientID);
                        # $sale->upc($upc);
                        # $sale->isrc($isrc);
                        $sale->artistName($artist);

                        # $sale->albumName($album);
                        $sale->trackName($track);

                        # $sale->labelName($label);
                        $sale->units($units);
                        $sale->price($price);
                        $sale->lineNum($linenum);
                        $sale->countryCode('US');

                        $self->_insertSale( sale => $sale );
                    } else {
                        print STDERR "skip l:" . $linenum . " u:" . $units . " p:" . $price . " a:" . $artist . " t:" . $title . "\n";
                    }
                    $linenum++;
                }
            }    # foreach line
            $sheet_count++;
        }    # foreach sheet

    }

    return $linenum;
}

#
# Private methods
#

sub matchHeader {
    my $line = shift;

    # verify the column headings are as we expect them
    my $artist = $line->[FIELD_ARTIST];
    my $track  = $line->[FIELD_TITLE];
    my $units  = $line->[FIELD_UNITS];
    my $price  = $line->[FIELD_TOTALPRICE];

    if (
           $artist =~ /author/i
        && $track =~ /product/i
        && $units =~ /totalsent/i
        &&    # not a typo
        $price =~ /payment/i
      ) {
        return 1;
    } else {
        return 0;
    }
}

sub _getDates {
    my ( $month, $year, $day );
    my $passed_date = shift;
    if ( $passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/ ) {
        $month = $1;
        $year  = $3;
    } elsif ( $passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/ ) {
        $year  = $1;
        $month = $2;
    }
    if ( $month && $year ) {
        return ( $year . "-" . $month . "-01", $year . "-" . $month . "-" . Days_in_Month( $year, $month ) );
    }
    return undef;
}

###
1;    # Play nicely.
###
