package RPS::Import::Bandcamp::v34;

use strict;
use warnings;

use parent 'RPS::Import::Bandcamp::v25';

sub _fieldMapExtended {
    {
        mixed => {
            option1 => {
                countryCode     => 'AK',
                dateBegin       => 'A',
                dateEnd         => 'A',
                formatType      => 'AA',
                clientProductID => 'AE',
                artistName      => 'E',
                upc             => 'AF',
                albumName       => 'D',
                isrc            => 'AG',
                trackName       => 'D',
                units           => 'H',
                currencyCode    => 'F',
                retailPrice     => 'G',
                totalRevenue    => 'Z',
                price           => 'Z',
                mediaType       => 'C',
            },
        },
    };
}

sub _unverifiedFieldMapExtended {
    {
        mixed => {
            option1 => {
                _productTypePhys => 'AA',
                _productTypeDig  => 'C',
                _netUnits        => 'H',
                _ship_from       => 'N',
            },
        }
    };
}

sub _headerIdentifierExtended {
    {
        mixed => {
            option1 => {
                upc => 'upc',
            },
        },
    };
}

sub productType {
    my $self = shift;

    my $shipFrom = $self->_getByFieldName('_ship_from')       || '';
    my $typeDig  = $self->_getByFieldName('_productTypeDig')  || '';
    my $typePhys = $self->_getByFieldName('_productTypePhys') || '';

    if ( !$shipFrom ) {
        # digital
        if ( $typeDig =~ /^track$/i ) {
            return RPS::File::Sale::TYPE_TRACK;
        }
        elsif ( $typeDig =~ /^album|refund|bundle$/i ) {
            return RPS::File::Sale::TYPE_ALBUM;
        }
        elsif ( $typeDig =~ /^reversal$/i ) {
            return $self->_getByFieldName('isrc')
                ? RPS::File::Sale::TYPE_TRACK
                : RPS::File::Sale::TYPE_ALBUM;
        }
    } else {
        # physical
        if ( $typePhys =~ /(?:12'' )?Vinyl [EL]P|\(?Vinyl\)?|\(?LP\)?|7''|10''|Image 7./i ) {
            return RPS::File::Sale::TYPE_LP;
        } elsif ( $typePhys =~ /Limited Edition (?:Magenta|on) Color (?:Vinyl|variant)/i) {
            return RPS::File::Sale::TYPE_LP;
        } elsif ( $typePhys =~ /^(?:Super Limited .?Deluxe.? Package|Limited Edition Semi-Pro Flexi Disc)$/i) {
            return RPS::File::Sale::TYPE_LP;
        } elsif ( $typePhys =~ /Ignorance & HIITISLATS double lyric book./i) {
            return RPS::File::Sale::TYPE_LP;
        } elsif ( $typePhys =~ /(?:Compact Disc|CD|Audio CD|\(CD\)|Boxset)/i || !$typePhys) {
            return RPS::File::Sale::TYPE_CD;
        } elsif ( $typePhys =~ /Cassette/i ) {
            return RPS::File::Sale::TYPE_CASS;
        } elsif ( $typePhys =~ /(?:7|12)\s*["”']{1,2}/i ) {
            return RPS::File::Sale::TYPE_LP;
        } elsif ( $typePhys =~ /tote\s*bag/i ) {
            return RPS::File::Sale::TYPE_LP;
        }
    }

    $self->fail("Unable to determine product_type from '$typeDig' or '$typePhys'.");
}

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || '';
    return RPS::File::Sale::FORMAT_DOWNLOAD if $type =~ /^(?:digital (?:download|bundle)|Happysad)$/i;

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

sub _isValidSaleRecord {
    my $self = shift;

    my $revenue     = $self->_getByFieldName('totalRevenue')    || 0;
    my $prodTypeDig = $self->_getByFieldName('_productTypeDig') || '';

    return if $prodTypeDig =~ /^payout$/i;
    return if !$revenue && $prodTypeDig =~ /^(?:cancelled|pending) reversal$/i;

    return ( $self->_getByFieldName('trackName') || $self->_getByFieldName('albumName') )
       && defined $self->netUnits;

    return 1;
}


1;
