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

package RPS::Import::TurnTableLab;

use strict;

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;
use Date::Calc qw(Days_in_Month Decode_Month);

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

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


# map version num to the field order
my %fieldMap =
(
	1 =>
	{
        isrc                => 1,
        upc                 => 2,
        artist              => 3,
        album               => 4,
        track               => 5,
        label               => 7,
        track_product_id    => 8,
        album_product_id    => 9,
        product_type        => 11,
        units               => 13,
        price               => 15,
        currency            => 16,
        #country             => 17,
        format_type         => 19,
        date                => 20,
        country             => 24,
	},
    2 => {
        isrc                => 1,
        upc                 => 2,
        artist              => 16,
        album               => 15,
        track               => 14,
        label               => 24,
        track_product_id    => 3,
        album_product_id    => 3,
        product_type        => 7,
        units               => 9,
        price               => 11,
        currency            => 12,
        #country             => 13,
        format_type         => 18,
        date                => 19,
        country             => 23,
    },
    3 => {
        isrc                => 0,
        upc                 => 1,
        artist              => 15,
        album               => 14,
        track               => 13,
        label               => 23,
        track_product_id    => 2,
        album_product_id    => 2,
        product_type        => 6,
        units               => 8,
        price               => 10,
        currency            => 11,
        #country             => 12,
        format_type         => 17,
        date                => 18,
        country             => 22,
    },
    4 => {
        isrc                => 1,
        upc                 => 2,
        artist              => 17,
        album               => 16,
        track               => 15,
        label               => 25,
        track_product_id    => 3,
        album_product_id    => 3,
        product_type        => 7,
        units               => 9,
        price               => 11,
        currency            => 13,
        #country             => 14,
        format_type         => 19,
        date                => 20,
        country             => 24,
    },
    5 => {
        isrc                => 1,
        upc                 => 2,
        artist              => 16,
        album               => 15,
        track               => 4,
        label               => 24,
        track_product_id    => 3,
        album_product_id    => 3,
        product_type        => 8,
        units               => 10,
        price               => 12,
        currency            => 13,
        #country             => 14,
        format_type         => 18,
        date                => 19,
        country             => 23,
    },
);

#public methods

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

	my $lines = $args{lines};
	my $fileObj = $args{file};
	my $version = $fileObj->VersionNum;

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

	my $offsets = $fieldMap{$version};

        my $linenum = 1;
        foreach my $line (@$lines)
        {
            my $isrc                = $line->[$offsets->{isrc}];
            my $upc                 = $line->[$offsets->{upc}];
            # stripping out all non-numeric characters
            $upc                    =~ s/\D//g;
            my $artist              = $line->[$offsets->{artist}];
            my $album               = $line->[$offsets->{album}];
            my $track               = $line->[$offsets->{track}];
            my $label               = $line->[$offsets->{label}];
            my $track_product_id    = $line->[$offsets->{track_product_id}];
            my $album_product_id    = $line->[$offsets->{album_product_id}];
            my $product_type        = uc $line->[$offsets->{product_type}];
            my $units               = $line->[$offsets->{units}];
            my $price               = $line->[$offsets->{price}];
            my $currency            = $line->[$offsets->{currency}];
            my $country             = $line->[$offsets->{country}];
            my $format_type         = uc $line->[$offsets->{format_type}];
            my $date                = $line->[$offsets->{date}];
            my ($dateBegin,$dateEnd) = _getDates($date);
            $price                  /= $units if($units != 0);

        if ($product_type && $units && $price && $artist && $artist !~ /Artist Name/ && ($album || $track))
        {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($product_type);
            $sale->formatType($format_type);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->serviceProductID($album_product_id) if($product_type eq RPS::File::Sale::TYPE_ALBUM);
            $sale->serviceProductID($track_product_id) if($product_type eq RPS::File::Sale::TYPE_TRACK);
            $sale->upc($upc) if($upc ne "");
            $sale->artistName($artist);
            $sale->albumName($album) if($album ne "");
            $sale->labelName($label);
            if ($product_type eq RPS::File::Sale::TYPE_TRACK) {
                $sale->trackName($track);
                $sale->isrc($isrc);
            }
            $sale->units($units);
            $sale->price($price);
            $sale->countryCode($country);
            $sale->currencyCode($currency);
            $sale->lineNum($linenum);
            $self->_insertSale(sale => $sale);
        }
        #else { print STDERR "Skip-> l:".$linenum." u:".$units." p:".$price." ar: ".$artist." al: ".$album."<-Skip\n";}
        $linenum++;
    }
	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);
    }
    return undef;
}

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