package RPS::Import::Qobuz;

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    => 'N',
            serviceID    => 'B',
            countryCode  => 'AB',
            dateBegin    => 'G',
            productType  => 'D',
            formatType   => 'C',
            artistName   => 'K',
            upc          => 'H',
            albumName    => 'J',
            isrc         => 'O',
            trackName    => 'L',
            units        => 'W',
            currencyCode => 'Y',
            price        => 'X',
        },
        2 => {
            labelName    => 'M',
            serviceID    => 'B',
            countryCode  => 'Y',
            dateBegin    => 'F',
            productType  => 'D',
            formatType   => 'C',
            artistName   => 'J',
            upc          => 'G',
            albumName    => 'I',
            isrc         => 'N',
            trackName    => 'K',
            units        => 'V',
            currencyCode => 'X',
            price        => 'W',
        },
        3 => {
            serviceID       => 'B',
            countryCode     => 'AB',
            dateBegin       => 'A',
            dateEnd         => 'A',
            productType     => 'D',
            formatType      => 'C',
            clientProductID => 'J',
            artistName      => 'L',
            upc             => 'I',
            albumName       => 'K',
            isrc            => 'P',
            trackNum        => 'R',
            trackName       => 'M',
            units           => 'Y',
            currencyCode    => 'AA',
            price           => 'Z',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            altDateBegin => 'A',
        },
        2 => {
            altDateBegin => 'A',
        },
    );

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

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

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);

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

sub productType {
    my $self = shift;

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

    if ( lc($productType) eq 'track' ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( lc($productType) eq 'album' ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } else {
        $self->fail("Unrecognized product type: $productType");
    }
}

sub formatType {
    my $self = shift;

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

    if ( lc($formatType) eq 'streaming' ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( lc($formatType) eq 'download' ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } else {
        $self->fail("Unrecognized format type: $formatType");
    }
}

sub _getDateBegin {

    my $self      = shift;
    my $dateBegin = lc( $self->_getByFieldName('dateBegin') );

    if ( !$dateBegin ) {
        $dateBegin = lc( $self->_getByFieldName('altDateBegin') );
    }

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $dateBegin =~ /^\d{5}$/ and $dateBegin > 38716 ) {
        $dateBegin = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $dateBegin );
    } elsif ( $dateBegin =~ /^(\d{5})\./ and $dateBegin > 38716 ) {
        $dateBegin = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $1 );
    }

    return $dateBegin;
}

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