package RPS::Import::ADA::v21;

use strict;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_date normalize_upc);
use Date::Calc qw(Days_in_Month);
use Data::Dumper;

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

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

use base 'RPS::Import::ADA::v14';

sub _headerIdentifier { ( isrc => 'ISRC' ) }

sub _fieldMap {
    {
        label           => 'A',
        serviceID       => 'M',
        countryCode     => 'O',
        
        artistName      => 'E',

        clientProductID => 'C',
        albumName       => 'F',
        trackName       => 'F',

        isrc            => 'I',
        upc             => 'H',

        units           => 'T',
        price           => 'Y',    
        
        productType     => 'G',         
    }
}

sub _unverifiedFieldMap {
    {
        deliveryType    => 'P',
        deliveryFormat  => 'Q',
    }
}


sub currencyCode { 'GBP' }

sub _isValidSaleRecord 
{
    my $self = shift;
    my $units  = $self->units;
    my $price  = $self->price;

    return undef if ( !$units && $price && $price < 0 );
    return $self->RPS::Import::Importer::_isValidSaleRecord();
}

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

    if (!$productType)  
    {
        my $isrc = $self->_getByFieldName('isrc');
        my $upc = $self->_getByFieldName('upc');
        if( $isrc && '' ne $isrc )
        {
            $productType = RPS::File::Sale::TYPE_TRACK;
        }
        elsif( $upc && '' ne $upc )
        {
            $productType = RPS::File::Sale::TYPE_ALBUM;
        }
        else
        {
            return $self->handleError("Product type missing", RPS::DB::Item::SaleImportError::kErrorNonqualifying);
        }
    }
    elsif( $productType =~ /audio clip/i ||
        $productType =~ /mastertone/i ||
        $productType =~ /ringbacktone/i ||
        $productType =~ /ringtone/i ||
        $productType =~ /video/i ||
        $productType =~ /track/i ) 
    {
        $productType = RPS::File::Sale::TYPE_TRACK;
    }
    elsif( $productType =~ /album/i || 
           $productType =~ /single/i ||
           $productType =~ /bundle/i ||
           $productType =~ /boxed set/i ||
           $productType =~ /digital download \((album|single)\)/i ||
           $productType =~ /digital download \((2|3) components\)/i ||
           $productType =~ /ep/i ||
           $productType =~ /full length/i)
    {
        $productType = RPS::File::Sale::TYPE_ALBUM;
    }  
    else 
    {
       return $self->handleError("Unknown product type: $productType", RPS::DB::Item::SaleImportError::kErrorProductType);
    }
}   

sub mediaType {
    my $self = shift;
    my $productType = $self->_getByFieldName( 'productType' );
    
    if ($productType =~ /video/i) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    } else {
        my $deliveryType   = lc $self->_getByFieldName( 'deliveryType' );        
        if (!$deliveryType)  
        {
            return $self->handleError("Delivery type missing", RPS::DB::Item::SaleImportError::kErrorNonqualifying);
        }        
        
        my $deliveryFormat = lc $self->_getByFieldName( 'deliveryFormat' ); 
        if (!$deliveryFormat)  
        {
            return $self->handleError("Delivery format missing", RPS::DB::Item::SaleImportError::kErrorNonqualifying);
        }          

        my $mediaType  = $self->_getMediaType(deliveryFormat => $deliveryFormat, deliveryType => $deliveryType);
        if ($mediaType) {
		    return $mediaType;        
		} else {
            return $self->handleError("Unknown deliveryType($deliveryType) deliveryFormat($deliveryFormat)", RPS::DB::Item::SaleImportError::kErrorMediaType); 
		}        
    }
}

## MapFace Stuff
sub _mapFaceCompatibleVersions {
	return (21,22);
}

sub _mapFaceServiceID {
	return ('serviceID', 'countryCode');
}

sub _mapFaceCountryCode {
	return ('countryCode');
}

sub _mapFaceProductType {
	return ('productType');
}

sub _mapFaceFormatType {
	return ('deliveryType', 'deliveryFormat');
}

sub _mapFaceMediaType {
	return ('productType', 'deliveryType', 'deliveryFormat');
}

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