#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2013 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::Sony::v13;

use strict;

use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::UTF8;
use Common::Country;

use lib '/app/tools/rps/lib';
use base 'RPS::Import::MapFaceBase';
use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        # detail
        #
        dateBegin   => 'J',
        artistName  => 'B',
        countryCode => 'H',
        units       => 'Z',
        price       => 'AC',
        priceLevel  => 'L',
        retailPrice => 'M',
        productType => 'I',    # Config
        formatType  => 'K',    # Distribution channel
    };
}

sub _unverifiedFieldMap {
    {
        productNumber => 'G',
        productTitle  => 'C',
    };
}

sub _headerIdentifier {
    ( 'productNumber' => 'Product number' );
}

sub currencyCode { 'USD' }

sub saleDates {
    my $self      = shift;
    my $startDate = $self->_getByFieldName('dateBegin');

    my $dateBegin;
    my $dateEnd;
    if ( $startDate =~ /(\d{4}) H(\d)/ ) {
        if ( $2 == "1" ) {
            $dateBegin = "$1-01-01";
            $dateEnd   = "$1-06-30";
        } elsif ( $2 == "2" ) {
            $dateBegin = "$1-07-01";
            $dateEnd   = "$1-12-31";
        }
    }

    return ( $dateBegin, $dateEnd );
}

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

    if ( $country =~ /Slovakia Rep\./i ) {
        $country = "Slovakia";
    } elsif ( $country =~ /Rep\. Korea/i ) {
        $country = "Korea, Republic of";
    } elsif ( $country =~ /Dominican Rep./i ) {
        $country = "DO";
    }

    my $o = Common::Country->Get($country);
    if ( !$o ) {
        return $country;    # Return original name and let MapFace deal with it
    }
    return $o->alpha2();
}

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

    if ( $config =~ /^AudTrack$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( $config =~ /^Audio LP$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    }

    return $self->handleError( "Unknown config: $config", RPS::DB::Item::SaleImportError::kErrorProductType );
}

sub mediaType {
    my $self   = shift;
    my $config = $self->_getByFieldName('productType');
    if ( $config =~ /^AudTrack$/i || $config =~ /^Audio LP$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    return $self->handleError( "Unknown config: $config", RPS::DB::Item::SaleImportError::kErrorMediaType );
}

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

    if ( $channel =~ /^Subscrip$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $channel =~ /^Online$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $channel =~ /^Mobile$/i ) {
        return RPS::File::Sale::FORMAT_RINGTONE;
    } elsif ( $channel =~ /^Locker-D$/i
        || $channel =~ /^Locker-S$/i )    #FB2966
    {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $channel =~ /^BRKG-ONL$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $channel =~ /^BRKG-SUB$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $channel =~ /^BRKG-MOB$/i ) {
        return RPS::File::Sale::FORMAT_RINGTONE;
    }

    return $self->handleError( "Unknown dist channel: $channel", RPS::DB::Item::SaleImportError::kErrorFormatType );
}

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

    if ( !defined $self->productType ) {
        my $config = $self->_getByFieldName('productType');
        return $upc if ( $config =~ /lp/i );    # if the product type contains 'LP', then assume album
    }

    return $upc if ( $self->productType eq RPS::File::Sale::TYPE_ALBUM );
}

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

    if ( !defined $self->productType ) {
        my $config = $self->_getByFieldName('productType');
        return $isrc if ( $config =~ /track/i );    # if the product type contains 'track', then assume track
    }

    return $isrc if ( $self->productType eq RPS::File::Sale::TYPE_TRACK );
}

sub albumName {
    my $self = shift;
    my $name = $self->_getByFieldName('productTitle');

    if ( !defined $self->productType ) {
        my $config = $self->_getByFieldName('productType');
        return $name if ( $config =~ /lp/i );       # if the product type contains 'LP', then assume album
    }

    return $name if ( $self->productType eq RPS::File::Sale::TYPE_ALBUM );
}

sub trackName {
    my $self = shift;
    my $name = $self->_getByFieldName('productTitle');

    if ( !defined $self->productType ) {
        my $config = $self->_getByFieldName('productType');
        return $name if ( $config =~ /track/i );    # if the product type contains 'track', then assume track
    }

    return $name if ( $self->productType eq RPS::File::Sale::TYPE_TRACK );
}

sub unitPrice {
    my $self       = shift;
    my $_unitPrice = $self->SUPER::unitPrice();
    $_unitPrice = 0 if ( $_unitPrice eq "-0" );
    return $_unitPrice;
}

#####    MapFace    #####
#

sub _mapFaceServiceID {
    return ('serviceID');
}

sub _mapFaceCountryCode {
    return ('countryCode');
}

sub _mapFaceProductType {
    return ('productType');
}

sub _mapFaceFormatType {
    return ('formatType');
}

sub _mapFaceMediaType {
    return ('productType');
}

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