package RPS::Import::Hostess::v1;

use strict;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_upc normalize_isrc);

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

sub _headerIdentifier { (upc => 'UPC') }

sub _fieldMap {
    my $self = shift;
    my %field_map = (
        1 => {
            labelName        => 'M',
            serviceID        => 'S',
            dateBegin        => 'A',
            dateEnd          => 'B',
            formatType       => 'J',
            artistName       => 'K',
            upc              => 'C',
            albumName        => 'L',
            trackName        => 'L',
            isrc             => 'D',
            units            => 'F',
            price            => 'H',
        },
        2 => {
            labelName        => 'M',
            serviceID        => 'R',
            dateBegin        => 'A',
            dateEnd          => 'B',
            formatType       => 'J',
            artistName       => 'K',
            upc              => 'C',
            albumName        => 'L',
            trackName        => 'L',
            isrc             => 'D',
            units            => 'F',
            price            => 'H',
        },
    );
    return $field_map{$self->version};
}

sub countryCode { 'JP' }

sub currencyCode { 'JPY' }

sub serviceID {
    my $self = shift;
    my $service = $self->_getByFieldName('serviceID');
    my $serviceID = $self->getServiceID(lc ($service)) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };
    return $serviceID if( $serviceID );
    return Client::Service::DSP_HOSTESS if( '' eq $service );

    $self->fail("Unknown service '$service'");
}

sub saleDates {
    my $self = shift;
    my $dateBegin = $self->_getByFieldName('dateBegin');
    my $dateEnd = $self->_getByFieldName('dateEnd');
    $dateBegin =~ s/\./\//g;
    $dateEnd =~ s/\./\//g;
    return $self->_getDates( date_begin => $dateBegin, date_end => $dateEnd, type => 'eur' );
}

sub productType {
    my $self = shift;
    my $isrc = $self->_getByFieldName('isrc');
    return (defined $isrc && '' ne $isrc) ? RPS::File::Sale::TYPE_TRACK : RPS::File::Sale::TYPE_ALBUM;
}

sub formatType {
    my $self = shift;
    my $type = $self->_getByFieldName('formatType');

    if( '' eq $type || $type =~ /^ftd$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif( $type =~ /^stream$/i || $type =~ /^linear radio$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    $self->fail("Could not determine format type from '$type'");
}


sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio; }


sub trackName {
    my $self = shift;
    return $self->productType eq RPS::File::Sale::TYPE_ALBUM ? undef : $self->SUPER::trackName;
}

sub albumName {
    my $self = shift;
    return $self->productType eq RPS::File::Sale::TYPE_TRACK ? undef : $self->SUPER::albumName;
}

sub isrc {
    my $self = shift;
    my $isrc = $self->_getByFieldName('isrc');
    return normalize_isrc($isrc);
}

sub upc {
    my $self = shift;
    my $upc = $self->_getByFieldName('upc');
    return normalize_upc($upc);
}

sub _isValidSaleRecord {
    my $self = shift;
    return ( defined($self->units) && $self->units != 0 && ($self->trackName || $self->albumName) );
}

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