package RPS::Import::Vevo;

use strict;

use Data::Dumper;
use lib '/app/tools/common/lib';
use Common::Util qw(normalize_upc normalize_isrc);
use Date::Calc qw(Days_in_Month);

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

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

sub _headerIdentifier { ( isrc => 'Isrc' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            labelName   => 'E',
            countryCode => 'F',
            artistName  => 'D',
            isrc        => 'B',
            trackName   => 'C',
            units       => 'G',
            price       => 'I',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub mediaType    { RPS::DB::Item::MediaType::kMediaTypeVideo }
sub formatType   { RPS::File::Sale::FORMAT_STREAM }
sub currencyCode { 'USD' }

sub saleDates {
    my $self = shift;
    return ( $self->{_dateBegin}, $self->{_dateEnd} );
}

sub _preProcess {

    my $self = shift;

    my %args     = @_;
    my $fileName = $args{file}->OrigFileName();

    # Get date information from the filename
    #
    if ( $fileName =~ /(\d{4}) (\d{2})/ )    # YYYY MM
    {
        my ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $1 . $2, type => 'numerical_year_first' );
        $self->{_dateBegin} = $dateBegin;
        $self->{_dateEnd}   = $dateEnd;
    } else {
        $self->fail("Couldn't get dates from filename '$fileName'");
    }

}

sub productType {
    my $self = shift;

    my $isrc = $self->_getByFieldName('isrc') || return;

    if ($isrc) {
        return RPS::File::Sale::TYPE_TRACK;
    }

    #else
    #{
    #    return RPS::File::Sale::TYPE_ALBUM;
    #}
    else {
        $self->fail("Unrecognized product type");
    }
}

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