package RPS::Import::OutSideMusic;

use strict;

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

use Date::Calc qw(Days_in_Month Decode_Month);

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

# map version num to the field order
my %field_map = (
    1 => {
        service_product_id => 1,
        artist_album       => 3,
        retail_price       => 4,
        units              => 5,
        price              => 6,

    },
);

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 $fileObj  = $args{file};
    my $version  = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();
    my $offsets  = $field_map{$version};
    my ( $dateBegin, $dateEnd );
    my $sheet_count = 0;
    my $linenum     = 0;
    return undef unless ( defined $args{sheets} && defined $version );

    my $sheet_names = $args{sheet_names};

    foreach my $sheet ( @{ $args{sheets} } ) {

        $linenum = 1;
        if ( $sheet_names->[$sheet_count] =~ /detail/i || $sheet_names->[$sheet_count] eq "Sheet1" ) {
            foreach my $line (@$sheet) {
                ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $line->[ $offsets->{units} ] ) if ( $linenum == 1 );
                my ( $artist, $album ) = split( ":", $line->[ $offsets->{artist_album} ] );
                $album =~ s/^\s//;
                my $service_product_id = $line->[ $offsets->{service_product_id} ];
                my $product_type       = RPS::File::Sale::TYPE_ALBUM;
                my $units              = $line->[ $offsets->{units} ];
                my $price              = $line->[ $offsets->{price} ];
                my $retail_price       = $line->[ $offsets->{retail_price} ];
                my $currency           = "USD";
                my $country            = "CA";

                $product_type = RPS::File::Sale::TYPE_CD;

                #$channel = RPS::File::Sale::CHANNEL_RETAIL if($channel =~ /retail/i);
                #$channel = RPS::File::Sale::CHANNEL_MAILORDER if($channel =~ /mail order/i);

                if (   $units ne ""
                    && $units != 0
                    && $service_product_id ne "Vendor Part Number"
                    && $line->[ $offsets->{album_and_artist} ] !~ /Description/
                    && $line->[ $offsets->{album_and_artist} ] ne "" ) {

                    # !!! Let the inherited code do the sanity checks.
                    #                    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($product_type);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->serviceProductID($service_product_id);
                    $sale->artistName($artist);
                    $sale->albumName($album);
                    $sale->retailPrice($retail_price);

                    if ( $units > 0 ) {
                        $sale->sales($units);
                        $sale->salesRevenue($price);
                    } elsif ( $units < 0 ) {
                        my $returnRevenue = $price;
                        $returnRevenue *= -1 if $returnRevenue < 0;
                        $sale->returns( $units * -1 );
                        $sale->returnsRevenue($returnRevenue);
                    }
                    $sale->totalRevenue($price);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->channel(RPS::File::Sale::CHANNEL_RETAIL);
                    $sale->priceLevel(RPS::File::Sale::PLEVEL_FULL);
                    $sale->lineNum($linenum);
                    $self->_insertSale( sale => $sale );
                }

                #else { print STDERR "Skip-> l:".$linenum." u:".$units." p:".$price." ar: ".$artist." al:".$album."<-Skip\n";}
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

#
# Private methods
#

sub __getDates {
    my $date = shift;

    my $beginDate;
    my $endDate;
    if ( $date =~ m/(\d{4})\D(\d+)\D(\d+)/ ) {
        my $year  = $1;
        my $month = $2;
        $beginDate = $year . "-" . $month . "-01";
        $endDate = $year . "-" . $month . "-" . Days_in_Month( $year, $month );
        return ( $beginDate, $endDate );
    } elsif ( $date =~ m/(\d+)\D(\d+)\D(\d{4})/ ) {
        my $year  = $3;
        my $month = $1;
        $beginDate = $year . "-" . $month . "-01";
        $endDate = $year . "-" . $month . "-" . Days_in_Month( $year, $month );
        return ( $beginDate, $endDate );
    }
    return undef;
}

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