package RPS::Import::RealStream;

use strict;

use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Util;

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

#private constants
use constant FIELD_LABEL_ALT => 0;
use constant FIELD_LABEL     => 1;
use constant FIELD_ARTIST    => 2;
use constant FIELD_ALBUM     => 3;
use constant FIELD_TRACK     => 4;
use constant FIELD_UPC       => 5;
use constant FIELD_TRACKNUM  => 7;
use constant FIELD_ISRC      => 8;
use constant FIELD_UNITS     => 9;
use constant FIELD_ALBUMID   => 10;
use constant FIELD_TRACKID   => 11;
use constant FIELD_CATALOGID => 12;
use constant FIELD_DATEBEGIN => 13;
use constant FIELD_DATEEND   => 14;
use constant FIELD_FREE      => 16;

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 = @_;

    my $retval  = undef;
    my $lines   = $args{lines};
    my $fileObj = $args{file};
    my $version = $fileObj->VersionNum;

    if ( $lines && $version ) {
        my $dateBegin;
        my $dateEnd;

        #		($dateBegin, $dateEnd) = $self->_getDates($lines);

        #		if ($dateBegin && $dateEnd)
        #		{
        my $linenum = 1;
        foreach my $line (@$lines) {
            if ( $linenum > 1 ) {
                my $prodtype   = RPS::File::Sale::TYPE_TRACK;
                my $clientID   = $line->[FIELD_TRACKID];
                my $upc        = Common::Util::normalize_upc( $line->[FIELD_UPC] );
                my $isrc       = Common::Util::normalize_isrc( $line->[FIELD_ISRC] );
                my $artist     = $line->[FIELD_ARTIST];
                my $album      = $line->[FIELD_ALBUM];
                my $track      = $line->[FIELD_TRACK];
                my $label      = $line->[FIELD_LABEL] || $line->[FIELD_LABEL_ALT];
                my ($trackNum) = $line->[FIELD_TRACKNUM];
                my $format     = RPS::File::Sale::FORMAT_STREAM;
                my ($units)    = $line->[FIELD_UNITS];

                ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $line->[FIELD_DATEBEGIN], date_end => $line->[FIELD_DATEEND] );

                my $countryCode = 'US';

                my $free = 0;
                if ( $version == 3 ) {

                    #sometimes there's extra baggage after the 'Y', so we let that slide
                    if ( $line->[FIELD_FREE] =~ m/^y/i ) {
                        $free = 1;
                    }
                }

                if ( $prodtype && $units && ( $album || $track ) ) {
                    my $sale = RPS::Import::SaleRec->new();

                    $sale->productType($prodtype);
                    $sale->formatType($format);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->clientProductID($clientID);
                    $sale->upc($upc);
                    $sale->isrc($isrc);
                    $sale->artistName($artist);
                    $sale->albumName($album);
                    $sale->trackName($track);
                    $sale->labelName($label);
                    $sale->trackNum($trackNum);
                    $sale->units($units);
                    $sale->lineNum($linenum);
                    $sale->free($free);
                    $sale->countryCode($countryCode);

                    $self->_insertSale( sale => $sale );
                } else {
                    print STDERR "missing input\n";
                }
            }
            $linenum++;
        }
        $retval = $linenum;

        #		}
    }

    return $retval;
}

#
# Private methods
#

sub __getDates {
    my $self  = shift;
    my $lines = shift;

    my $dateBegin;
    my $dateEnd;

    foreach my $line (@$lines) {
        my $date = Common::Util::normalize_date( $line->[FIELD_DATEBEGIN] );

        if ($date) {
            if ( $date < $dateBegin || !$dateBegin ) {
                $dateBegin = $date;
            }
        }

        $date = Common::Util::normalize_date( $line->[FIELD_DATEEND] );

        if ($date) {
            if ( $date > $dateEnd || !$dateEnd ) {
                $dateEnd = $date;
            }
        }
    }

    if ($dateBegin) {
        substr( $dateBegin, 8 ) = "01";
    }

    if ($dateEnd) {
        substr( $dateEnd, -2 ) = Days_in_Month( substr( $dateEnd, 0, 4 ), substr( $dateEnd, 5, 2 ) );
    }

    return ( $dateBegin, $dateEnd );
}

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