package RPS::Import::ClearChannel;

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 lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

use constant FIELD_PROD_ID  => 0;
use constant FIELD_ALBUM    => 1;
use constant FIELD_TRACK    => 2;
use constant FIELD_ARTIST   => 3;
use constant FIELD_UPC      => 4;
use constant FIELD_ISRC     => 5;
use constant FIELD_UNITS    => 7;
use constant FIELD_PRICE    => 8;
use constant FIELD_FORMAT   => 9;
use constant FIELD_LABEL    => 10;

#
# public methods
#

sub _importLines
{
    my $self = shift;
    my %args = @_;
    my $lines = $args{lines};
    my $fileObj = $args{file};
    my $fileName = $fileObj->OrigFileName;
    #my $fileName = $args{OrigFileName};

    return undef unless ($lines);

    my ($dateBegin, $dateEnd) = _get_dates($fileName);

    unless ($dateBegin && $dateEnd)
    {
        $self->errstr("couldn't get dates");
        print STDERR "fileName: $fileName\n";
        return undef;
    }

    my $linenum = 1;

    foreach my $line (@$lines)
    {
        my $prodtype  = RPS::File::Sale::TYPE_TRACK;

        my $prod_id = $line->[FIELD_PROD_ID];
        my $upc     = $line->[FIELD_UPC];
        my $isrc    = $line->[FIELD_ISRC];
        my $artist  = $line->[FIELD_ARTIST];
        my $album   = $line->[FIELD_ALBUM];
        my $track   = $line->[FIELD_TRACK];
        my $label   = $line->[FIELD_LABEL];
        my ($units) = $line->[FIELD_UNITS] =~ m/^(\d+)$/;
        my ($price) = $line->[FIELD_PRICE] =~ m/^(\d*\.?\d+)$/;

        my $format;
        my $mediaType;
        if( $line->[FIELD_FORMAT] =~ m/stream/i )
        {
            $format = RPS::File::Sale::FORMAT_STREAM;
            $mediaType = RPS::DB::Item::MediaType::kMediaTypeVideo;
        }

        unless ($format)
        {
            print STDERR "unknown format on $linenum ($line->[FIELD_FORMAT])\n" unless ($linenum == 1);
            $linenum++;
            next;
        }

        if ($upc && $isrc && $track && $units && $price >= 0)
        {
            $price /= $units;
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->mediaType($mediaType);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->serviceProductID($prod_id);
            $sale->upc($upc);
            $sale->isrc($isrc);
            $sale->artistName($artist);
            #$sale->albumName($album);
            $sale->trackName($track);
            $sale->labelName($label);
            $sale->units($units);
            $sale->price($price);
            $sale->lineNum($linenum);

            #$args{dumper}($sale); ## for cmd-line testing
            $self->_insertSale(sale => $sale);
        }
        else { print STDERR "skip:". join(',', @$line) . "\n"; }
        $linenum++;
    }
    return $linenum;
}

#
# Private methods
#

sub _get_dates
{
    my $fName = lc shift;
    my ($month, $day, $year);

    if ($fName =~ m/channel_(\d\d)(\d\d)(\d{4})_\d+_/) # Channel_02012006_02282006_01
    {
        ($month, $day, $year) = ($1, $2, $3);
    }
    elsif ($fName =~ m/_[a-z]+_(\d{4})\D(\d\d)\D(\d\d)\D\d+/) # Report_2006-03-01-2006-03-31.csv
    {
        ($year, $month, $day) = ($1, $2, $3);
    }
    elsif ($fName =~ m/\D(\d{4})[^a-z]+([a-z]+)/) # ClearChannel - 2007 12 SEP
    {
        ($year, $month) = ($1, $2);
        $month = Decode_Month($month);
    }
    else { return undef; }

    return (
        sprintf("%04d-%02d-01", $year, $month), 
        sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month))
    ) if ($month && $year);
}

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