package RPS::Import::Fina;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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

use lib '/app/tools/data_classes/lib';
use Client::Service;

my %field_map = (
    1 => {
        'album_id' => 0,
        'artist'   => 1,
        'units'    => 2,
        'price'    => 3,
        'date'     => 5,
        'country'  => 6,
    },
    2 => {
        'album_id' => 0,
        'artist'   => 1,
        'album'    => 2,
        'units'    => 3,
        'price'    => 4,
        'date'     => 6,
        'country'  => 7,
    },
);

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 $fileName = $fileObj->OrigFileName;
    my $version  = $fileObj->VersionNum;

    return undef unless ( $lines && $fileName && $version );

    my $offsets = $field_map{$version};

    my %errors  = ();
    my $linenum = 1;

    foreach my $line (@$lines) {

        if ( $linenum < 2 ) {
            $linenum++;
            next;
        }

        my $format    = RPS::File::Sale::FORMAT_DOWNLOAD;
        my $prodtype  = RPS::File::Sale::TYPE_ALBUM;
        my $serviceID = $line->[ $offsets->{'album_id'} ];
        my $artist    = $line->[ $offsets->{'artist'} ];
        my $units     = $line->[ $offsets->{'units'} ];

        my $countryCode = $line->[ $offsets->{'country'} ];
        if ( $countryCode eq 'European customer' ) {
            $countryCode = 'EU';
        } else {
            $countryCode = 'US';
        }
        my $currencyCode = 'USD';
        my $price        = $line->[ $offsets->{'price'} ];
        if ($price) {
            $price = sprintf( "%.8f", $price );
        }

        my $album;

        if ( defined $offsets->{album} ) {
            $album = $line->[ $offsets->{album} ];
        }

        if ( $units && $serviceID ) {
            my ( $dateBegin, $dateEnd ) = _getDates( $line->[ $offsets->{date} ] );
            unless ( $dateBegin && $dateEnd ) {
                print STDERR "Couldn't get dates from " . $line->[ $offsets->{date} ] . "\n";
                $self->errstr( "couldn't get dates from " . $line->[ $offsets->{date} ] );
                return undef;
            }

            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->units($units);
            $sale->currencyCode($currencyCode);
            $sale->countryCode($countryCode);
            $sale->price($price);
            $sale->serviceProductID($serviceID);
            $sale->lineNum($linenum);

            $self->_insertSale( sale => $sale );
        }
        $linenum++;
    }

    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my ( $syear, $eyear, $smonth, $emonth, $passed_date, $dateBegin, $dateEnd );
    $passed_date = shift;
    print "$passed_date\n";
    if ( $passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/ ) {
        return ( $3 . "-" . sprintf( "%02d", $1 ) . "-01", $3 . "-" . sprintf( "%02d", $1 ) . "-" . Days_in_Month( $3, $1 ) );
    } elsif ( $passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/ ) {
        return ( $1 . "-" . sprintf( "%02d", $2 ) . "-01", $1 . "-" . sprintf( "%02d", $2 ) . "-" . Days_in_Month( $1, $2 ) );
    } elsif ( $passed_date =~ m/(\d{4})(\d{2})(\d{2})/ ) {
        return ( $1 . "-" . $2 . "-01", $1 . "-" . $2 . "-" . Days_in_Month( $1, $2 ) );
    }
    return undef;
}

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