package RPS::Import::VeniceMusic::v1;

use strict;
use warnings;

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

use Client::Service;
use RPS::Import::ServiceMap;

use base qw/RPS::Import::MapFaceBase RPS::Import::Importer/;

sub _fieldMap {
    {
        labelName    => 'G',
        serviceID    => 'B',
        countryCode  => 'C',
        dateBegin    => 'A',
        dateEnd      => 'A',
        productType  => 'J',
        formatType   => 'J',
        artistName   => 'H',
        upc          => 'E',
        isrc         => 'F',
        units        => 'K',
        currencyCode => 'Z',
        price        => 'Z',
        mediaType    => 'J',
    };
}

sub _unverifiedFieldMap {
    {
        _trackTitle => 'I',
    };
}

sub _headerIdentifier { upc => 'upc' }

sub currencyCode { 'USD' }

sub serviceID {
    my $self = shift;

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

    $self->handleError( "Unknown service: '$service'", RPS::DB::Item::SaleImportError::kErrorService );
}

sub countryCode {
    my $self = shift;

    my $name = $self->_getByFieldName('countryCode') || '';
    my $country = Common::Country->Get($name);
    return $country->alpha2() if $country;

    $self->fail("Unable to determine country code from: '$name'");
}

sub dateBegin {
    my $self = shift;

    # MM/DD/YYYY
    my $date = $self->_getByFieldName('dateBegin') || '';
    if ( $date =~ /^(\d{2})\/(\d{2})\/(\d{4})$/ ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }

    $self->fail("Unable to determine date begin from: '$date'");
}

sub dateEnd {
    my $self = shift;

    #MM/DD/YYYY
    my $date = $self->_getByFieldName('dateEnd') || '';
    if ( $date =~ /^(\d{2})\/(\d{2})\/(\d{4})$/ ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }

    $self->fail("Unable to determine date end from: '$date'");
}

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';
    my $trackTitle = $self->_getByFieldName('_trackTitle') || '';

    if ( $type =~ /^(?:Stream|TrackDownload|VideoCreation|VideoDownload)$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( $type =~ /^AlbumDownload$/i ) {
        return RPS::File::Sale::TYPE_TRACK if $trackTitle;
        $self->fail("Found album download with no track title.");
    }


    $self->fail("Unable to determine product type from: '$type'");
}

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || '';
    if ( $type =~ /^(?:Stream|VideoCreation)$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $type =~ /^(?:Album|Track|Video)Download$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }


    $self->fail("Unable to determine format type from: '$type'");
}

sub albumName {
    my $self = shift;

    if ( $self->productType eq RPS::File::Sale::TYPE_ALBUM ) {
        return $self->_getByFieldName('_trackTitle') || '';
    }

    return;
}

sub trackName {
    my $self = shift;

    if ( $self->productType eq RPS::File::Sale::TYPE_TRACK ) {
        return $self->_getByFieldName('_trackTitle') || '';
    }

    return;
}

sub price {
    my $self = shift;

    my $price = $self->_getByFieldName('price') || 0;
    $price =~ s/\$//g;

    return abs($price);
}

sub unitPrice { abs shift->SUPER::unitPrice }
sub units {
    my $self = shift;

    my $units = $self->_getByFieldName('units') || 0;
    my $price = $self->_getByFieldName('price') || 0;
    $price =~ s/\$//g;

    if ( $price > 0 ) {
        $units = $units ? abs($units) : 1;
    } elsif ( $price < 0 ) {
       $units = $units ? -1 * abs($units) : -1;
    }

    return $units;
}

sub mediaType {
    my $self = shift;

    my $type = $self->_getByFieldName('mediaType') || '';

    if ( $type =~ /^(?:Stream|AlbumDownload|TrackDownload)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    } elsif ( $type =~ /^(?:VideoCreation|VideoDownload)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }

    $self->fail("Unable to determine media type from: '$type'");
}
sub _isValidSaleRecord {
    my $self = shift;

    my $service = $self->_getByFieldName('serviceID') || '';

    return if $service =~ /^Stripe payment processing fee$/i;

    return $self->SUPER::_isValidSaleRecord();
}

sub _mapFaceProductType { }
sub _mapFaceServiceID   { 'serviceID' }
sub _mapFaceCountryCode { }
sub _mapFaceFormatType  { }
sub _mapFaceMediaType   { }

1;
