package RPS::Import::Omnian::v1;

use strict;
use warnings;

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

use parent 'RPS::Import::ImporterMixed';

sub physical { 2 }

sub _fieldMapExtended {
    {
        mixed => {
            option1 => {
                countryCode      => 'AI',
                dateBegin        => 'O',
                dateEnd          => 'O',
                clientProductID  => 'B',
                upc              => 'F',
                albumName        => 'C',
                units            => 'H',
                currencyCode     => 'P',
                retail           => 'K',
                price            => 'K',
                trackName        => 'D',
                productType      => 'D',
            },
         },
    };
}

sub _unverifiedFieldMapExtended {
    {
        mixed => {
            option1 => {
                _netUnits        => 'H',
            },
        }
    };
}

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

sub serviceName { Client::Service::DSP_OMNIAN }
sub formatType  { RPS::File::Sale::FORMAT_DOWNLOAD }
sub channel     { RPS::File::Sale::CHANNEL_RETAIL }
sub priceLevel  { RPS::File::Sale::PLEVEL_UNKNOWN }

sub countryCode {
    my $self = shift;

    my $name = $self->_getByFieldName('countryCode') || 'US';
    $self->fail( "Unable to determine country from: '$name'." ) unless $name;

    my $oCountry = Common::Country->Get($name);
    if ( my $country = $oCountry->alpha2 ) {
        return $country;
    }

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

sub saleDates {
    my $self = shift;

    # MM/DD/YYYY
    my $dateBegin = $self->_getByFieldName('dateBegin') || '';

    if ( $dateBegin =~ /(\d{2})[\-\/](\d{2})[\-\/](\d{4})/ ) {
        $dateBegin = sprintf "%04d-%02d-%02d", $3, $1, $2;
    } elsif ( $dateBegin =~ /(\d{4})[\-\/](\d{2})[\-\/](\d{2})/ ) {
        $dateBegin = sprintf "%04d-%02d-%02d", $1, $2, $3;
    } else {
        $self->fail( "Unable to determine date from: '$dateBegin'." );
    }

    return $self->_getDates( date_begin => $dateBegin );
}

sub productType {
    my $self = shift;

    # the raw product_type value can contain many slashes or none, let's format the value.
    my $productType = $self->_getByFieldName('productType') || '';
    $productType =~ s{^\s*?\/|\/\s*?$}{}g;
    $productType =~ s{(.*)}{\/ $1 \/};

    # will check the correspondence between the slashes
    # physical part
    if ( $productType =~ m{\/\s+(?:
        CD(?:\sBox\sSet)? | (?:3|4)xCD | EP
    )\s+\/}xi ) {
        return RPS::File::Sale::TYPE_CD;
    } elsif ( $productType =~ m{\/\s+(?:
        2xCS | (?:Another\s\(Demo\)\sOne\s)?Cassette
    )\s+\/}xi ) {
        return RPS::File::Sale::TYPE_CASS;
    } elsif ( $productType =~ m{\/\s+(?:
        (?:7|12)"\sSingle | 7"\s\+\sTape
    )\s+\/}xi ) {
        return RPS::File::Sale::TYPE_LP5;
    } elsif ( $productType =~ m{\/\s+(?:
        (?:7|12)"(?:\sEP)? | 12"\sVinyl\sLP | 2xLP(?:\+7")? | (?:3|6)xLP\sBox\sSet |
        (:?Black|Clear|Gold|White)\sVinyl\sLP | (?:Blue|Red)\sVinyl | Red\sVinyl\sLP\s\+\sTote |
        Limited\sColored\sVinyl(?:\s\+\s7"\s\+\sPoster)? | Limited\sEdition\s(?:12"|LP|Vinyl) |
        LP(?:\+7")? | LP\s(?:Box\sSet|\+\sTape\sBundle|Gold\s\(Ltd\)) | LP\s\+\sT\-Shirt\sBundle\s\((?:L|M|S|XL)\) |
        LP\sBundle\s\((?:L|XL)\) |
        Special\sEdition\s(?:LP|LP\sBox\sSet|Colored\sVinyl\s\+\sPoster) | Vinyl(?:\sBundle)? |
        Default\sTitle | CT10\sEdition\sLP | Gatefold\sLP | Ltd\sEd\sLP\sw\s7" | Deluxe\s3xLP\sSet |
        .*(?:7|12)"
    )\s+\/}xi ) {
        return RPS::File::Sale::TYPE_LP;
    }
    # digital part
    elsif ( $productType =~ m{\/\s+ album \s+\/}xi ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif ( $productType =~ m{\/\s+ track \s+\/}xi ) {
        return RPS::File::Sale::TYPE_TRACK;
    }

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

sub trackName {
    my $self = shift;

    my $trackStr = $self->_getByFieldName('trackName') || '';
    my @items = split '\s+\/\s+', $trackStr;
    my $trackName = $items[0] || '';
    $trackName =~ s/^\s*|\s*$//g;

    return $trackName if $self->productType eq RPS::File::Sale::TYPE_TRACK;
}

# digital
sub price     { abs( shift->_getByFieldName('price') || 0 ) }
sub unitPrice { abs shift->SUPER::unitPrice }
sub units {
    my $units = $_[0]->_getByFieldName('units') || 0;
    my $price = $_[0]->_getByFieldName('price') || 0;
    return $price < 0 ? abs($units) * -1 : $units;
}

sub netUnits           { $_[0]->_getByFieldName('_netUnits') || 0 }
sub sales              { $_[0]->netUnits     > 0 ? $_[0]->netUnits         : 0 }
sub salesRevenue       { $_[0]->totalRevenue > 0 ? $_[0]->totalRevenue     : 0 }
sub returns            { $_[0]->netUnits     < 0 ? abs $_[0]->netUnits     : 0 }
sub returnsRevenue     { $_[0]->totalRevenue < 0 ? abs $_[0]->totalRevenue : 0 }

sub totalRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName('retail') || 0;
    return ( $revenue * $self->netUnits );
}

sub _isValidSaleRecord {
    return ( $_[0]->_getByFieldName('trackName') || $_[0]->_getByFieldName('albumName') )
    && defined $_[0]->netUnits
}


1;