#---------------------------------------------------------------
# ____                   _ _         ____  _                    
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___ 
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/                            
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::TelusSubscription;

use strict;

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Client;

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

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::MediaType;
use RPS::File::Sale;

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

my %fieldMap = (
        2 => {
            units           => 0,
            isrc            => 1,
            title           => 2,
            label           => 3,
        },
);


#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;

    assert(defined $args{sheets});
    assert(defined $version);

    my $offsets = $fieldMap{$version};
    my $sheet_names = $args{sheet_names};
    my $sheets = $args{sheets};
    my $linenum = 1;
    
    my $country = "CA";
    my $currencyCode = "CAD";
    my $productType = RPS::File::Sale::TYPE_TRACK;
    my $formatType = RPS::File::Sale::FORMAT_VPD;
    my $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;    
    
    
    my $date;
    if ($fileName =~ m/.*_(\w{3})_(\d{2})\.xls/i)
    {
        $date = $1 . " " . $2;   
    }
    
    my ($dateBegin, $dateEnd) = $self->_getDates(date_begin => $date, type => 'text');
    
    unless ($dateBegin && $dateEnd) {
        $self->errstr("Couldn't get dates from file name");       
        print STDERR "couldn't get dates from file name\n";
        return undef;
    }        

    for (my $sheetIndex = 0; $sheetIndex < scalar @{$args{sheets}}; $sheetIndex++)
    {
        my $sheet = $sheets->[$sheetIndex];

        my $foundHeader = 0;
        for (my $lineIndex = 0; $lineIndex < scalar @$sheet; $lineIndex++)
        {
            $linenum = $lineIndex + 1;
            my $line = $sheet->[$lineIndex];


            if (! $foundHeader)
            {
                if ('count' eq $line->[$offsets->{units}])
                {
                    $foundHeader = 1;
                }
                next;
            }

            my $trackTitle;
            my $artist;

            my $titleField = $line->[$offsets->{title}];
            if ($titleField =~ m/^'(.*) - (.*)'$/i)
            {
                $artist = $1;
                $trackTitle = $2;    
            }


            my $isrc                = $line->[$offsets->{isrc}];
            $isrc =~ s/\'//g;
            my $units               = $line->[$offsets->{units}];
            my $label               = $line->[$offsets->{label}];
            $label =~ s/\'//g;

            # Probably wise to allow for blank lines...
            #
            if ($units && $artist && $trackTitle)
            {

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

                $sale->mediaType($mediaType);
                $sale->productType($productType);
                $sale->formatType($formatType);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->labelName($label);
                $sale->trackName($trackTitle);
                $sale->artistName($artist);
                $sale->isrc($isrc);
                $sale->units($units);
                $sale->countryCode($country);
                $sale->currencyCode($currencyCode);
                $sale->lineNum($linenum);

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

	return $linenum;
}



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