package RPS::Import::SpiralFrog;

use strict;

use Date::Calc qw(Days_in_Month Add_Delta_Days Decode_Month);

use lib '/app/tools/common/lib';
use Common::Consts qw(%COUNTRY_TO_CODE);

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

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

my %fieldMap = (
    1 => {
        product    => 2,
        isrc       => 4,
        artist     => 6,
        album      => 9,
        track      => 7,
        date_start => 10,
        date_end   => 11,
        format     => 12,
        units      => 13,
    },
);

#public methods

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

    my $fileObj     = $args{file};
    my $version     = $args{file}->VersionNum();
    my $fileName    = $args{file}->OrigFileName();
    my $serviceID   = $args{file}->ServiceID;
    my $sheet_count = 0;

    return undef unless ( defined $args{sheets} && defined $version );

    my $offsets     = $fieldMap{$version};
    my $sheet_names = $args{sheet_names};
    my $linenum     = 1;
    my ( $dateBegin, $dateEnd );
    foreach my $sheet ( @{ $args{sheets} } ) {
        $linenum = 1;
        if ( $sheet_names->[$sheet_count] !~ /summary/i ) {
            my $lines = $sheet;
            foreach my $line (@$sheet) {
                my $dateBegin = _getDates( $line->[ $offsets->{date_start} ] );
                my $dateEnd   = _getDates( $line->[ $offsets->{date_end} ] );
                my $artist    = $line->[ $offsets->{artist} ];
                my $track     = $line->[ $offsets->{track} ];
                my $album     = $line->[ $offsets->{album} ];
                my $format    = $line->[ $offsets->{format} ];
                my $prodtype  = $line->[ $offsets->{product} ];
                my $units     = $line->[ $offsets->{units} ];
                my $isrc      = $line->[ $offsets->{isrc} ];
                my $country   = "US";
                my $currency  = "USD";
                my $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;

                if ( $format =~ /^v$/i ) {
                    $mediaType = RPS::DB::Item::MediaType::kMediaTypeVideo;
                }

                if ( $artist && $units && $track && $format && $prodtype ) {

                    unless ( $dateBegin ne "" && $dateEnd ne "" ) {
                        print STDERR "Couldn't get dates from: "
                          . $line->[ $offsets->{date_start} ] . " "
                          . $line->[ $offsets->{date_end} ]
                          . " line: "
                          . $linenum . "\n";
                        $self->errstr( "couldn't get dates from: "
                              . $line->[ $offsets->{date_start} ] . " "
                              . $line->[ $offsets->{date_end} ]
                              . " line: "
                              . $linenum );
                        return undef;
                    }
                    unless ( $format ne "" ) {
                        print STDERR "Unknown format: " . $line->[ $offsets->{format} ] . "\n";
                        $self->errstr( "Unknown format: " . $line->[ $offsets->{format} ] );
                    }
                    unless ( $prodtype ne "" ) {
                        print STDERR "Unknown product type: " . $line->[ $offsets->{product} ] . "\n";
                        $self->errstr( "Unknown product type: " . $line->[ $offsets->{product} ] );
                    }

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

                    $sale->mediaType($mediaType);

                    $sale->productType($prodtype);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->artistName($artist);
                    $sale->albumName($album) if ( $album ne "" );
                    $sale->trackName($track) if ( $track ne "" && $prodtype eq RPS::File::Sale::TYPE_TRACK );
                    $sale->isrc( ($isrc) ) if ( $isrc ne "" );
                    $sale->formatType(RPS::File::Sale::FORMAT_VPD);
                    $sale->units($units);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

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

#else { print STDERR "skip sh: ".$sheet_names->[$sheet_count] . ": l: " . $linenum . " - u:".$units." a:".$artist." t:".$track." a:".$album." f:".$format."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

#
# Private methods
#

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

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