package RPS::Import::Nokia::BaseNokia;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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

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

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

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

use Data::Dumper;

my %delivery_formats = (
    'DOWNLOAD'              => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Stream'                => RPS::File::Sale::FORMAT_STREAM,
    'Limited Download'      => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Over the Air Download' => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Ringtone'              => RPS::File::Sale::FORMAT_RINGTONE,
    'Ringback'              => RPS::File::Sale::FORMAT_RINGBACK,
    'Over the Air Download' => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Burn'                  => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Other'                 => RPS::File::Sale::FORMAT_STREAM,
    'Play'                  => RPS::File::Sale::FORMAT_STREAM,
    'Realtone'              => RPS::File::Sale::FORMAT_RINGTONE,
    'D'                     => RPS::File::Sale::FORMAT_TETHERED,
    'S'                     => RPS::File::Sale::FORMAT_STREAM,
    'C'                     => RPS::File::Sale::FORMAT_TETHERED,
);

my %gCountryCodeToCurrencyMap = (
    'US' => 'USD',
    'JP' => 'JPY',
    'NZ' => 'NZD',
    'IS' => 'ISD',
    'JM' => 'JMD',
    'EU' => 'EUR',
    'GB' => 'GBP',
    'CA' => 'CAD',
    'AU' => 'AUD',
    'DE' => 'EUR',
    'FR' => 'EUR',
    'BE' => 'EUR',
    'IT' => 'EUR',
    'CH' => 'CHF',
    'PL' => 'PLN',
    'LV' => 'LVL',
    'LT' => 'LTL',
    'KR' => 'KRW',
    'ZA' => 'ZAR',
    'GR' => 'EUR',
    'NL' => 'EUR',
);

my $currencyCode;

#
# public methods
#

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

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

sub serviceID {
    my $self    = shift;
    my $service = $self->_getByFieldName('serviceID');

    my $serviceID;
    if ($service) {
        $serviceID = $self->getServiceID($service) || $self->_getServiceID($service);
    }

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

    return $serviceID;
}

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

sub saleDates {
    my $self     = shift;
    my $fileName = $self->{filename};
    my ( $year, $month ) = $fileName =~ /[^\d](\d{4})(\d{2})[^\d]/;

    # Not the best place for this, but saleDates was already overloaded this class.
    # The better fix would be to remove this overload and implement date selection
    # like we do in bookpub importers.
    return $self->SUPER::saleDates(@_) if ( $self->_getDateBegin() );

    my ( $dateBegin, $dateEnd );

    ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $fileName, type => 'text' );

    unless ( $dateBegin && $dateEnd ) {
        ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $fileName, type => 'iso' );
    }

    unless ( $dateBegin && $dateEnd ) {
        ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $fileName, type => 'us' );
    }

    unless ( $dateBegin && $dateEnd ) {
        ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => "$month-$year", type => 'numerical' );
    }

    unless ( $dateBegin && $dateEnd ) {
        ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $fileName, type => 'numerical' );
    }

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

}

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

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

#
# Private methods
#

#
# _getCurrencyCode - use this method if the currency code is determined
# from the client's default country code
#
sub _getCurrencyCode {
    my $self = shift;

    my $clientID = $self->{clientID};

    my $client = RPS::DB::Item::Client->Lookup( client_id => $clientID );
    my $country_code = $client->country_code;
    $currencyCode = $gCountryCodeToCurrencyMap{$country_code};

    if ( !$currencyCode ) {
        $self->errstr("couldn't get base currency from country of origin");
        print STDERR "couldn't get base currency from country of origin\n";
        return undef;
    }
    return $currencyCode;
}

sub _getServiceID {
    my $self    = shift;
    my $service = shift;

    if ( $service =~ /^ovi music/i ) {
        return Client::Service::DSP_OVI_MUSIC;
    }

    return undef;
}

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