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

package RPS::Import::WMG::WarnerMobile;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::UTF8;
use Common::Country;
use Date::Calc qw(Add_Delta_Days Days_in_Month);

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

sub _fieldMap {
    {
        labelName   => 'H',
        dateBegin   => 'C',
        dateEnd     => 'D',
        productType => 'Q',
        formatType  => 'AO',
        artistName  => 'G',
        upc         => 'T',
        albumName   => 'V',
        isrc        => 'R',
        trackName   => 'I',
        units       => 'J', 
        price       => 'O', 
    }
}

sub _unverifiedFieldMap {
    {
        service       => 'B',
        productIdType => 'S',
    }
};

sub _headerIdentifier { ( trackName => 'TITLE' ) }

sub countryCode  { 'US' }
sub currencyCode { 'USD' }

my %formatMediaMap = (
    'WRMST' => {
        format => RPS::File::Sale::FORMAT_RINGTONE,
        media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'PERMDWNL' => {
        format => RPS::File::Sale::FORMAT_DOWNLOAD,
        media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'SUBAICLD' => {
        format => RPS::File::Sale::FORMAT_STREAM,
        media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'SUBASONE' => {
        format => RPS::File::Sale::FORMAT_STREAM,
        media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    #'WRRNGBK' => {
    #    format => , Ringback (new format type - TBA) - should error out right now on these
    #    media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    #},
    'DUALDIST' => {
        format => RPS::File::Sale::FORMAT_DUALDOWNLOAD,
        media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'WRSMST' => {
        format => RPS::File::Sale::FORMAT_RINGTONE,
        media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'SUBASPPE' => {
        format => RPS::File::Sale::FORMAT_STREAM,
        media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    #'WRGRA' => {
    #    format => eWallpaper (new format type - TBA) - should error out right now on these, too
    #    media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    #},
    'UGV' => {
        format => RPS::File::Sale::FORMAT_STREAM,
        media  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'SUBVSONE' => {
        format => RPS::File::Sale::FORMAT_STREAM,
        media  => RPS::DB::Item::MediaType::kMediaTypeVideo,
    },
);

sub _getDates
{
    my $self = shift;
    my $startDate = $self->_getDateBegin();
    my $endDate = $self->_getDateEnd();

    ## Excel date is (supposedly) days since 01 Jan 1900, but have to subtract 2
    ## from that number because (1) 0/1 index issue, and (2) MS wants to pretend
    ## that 1900 was a leap year.

    my ($dateBegin, $dateEnd);
    my ($year, $month, $day) = Add_Delta_Days(1900,1,1, $startDate - 2);
    $dateBegin = sprintf("%d-%02d-%02d", $year, $month, 1);

    ($year, $month, $day) = Add_Delta_Days(1900,1,1, $endDate - 2);

    my $daysInMonth = Days_in_Month($year, $month);
    $dateEnd = sprintf("%d-%02d-%02d", $year, $month, $daysInMonth);

    return ($dateBegin, $dateEnd);
}

sub serviceID {
    my $self = shift;

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

    if( !$id && ($service =~ /^digital breakage adj$/i ||
                 $service =~ /^audit recovery$/i ))
    {
        $id = Client::Service::DSP_WARNER_MOBILE;
    }

    $self->fail("Unknown service: $service") if (!$id);

    return $id;
}


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

    if( lc($type) eq 'a' || lc($type) eq 's' )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    elsif( lc($type) eq 'p' || lc($type) eq 't' || lc($type) eq 'd' )
    {
        return RPS::File::Sale::TYPE_TRACK;
    }

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

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

    if( exists $formatMediaMap{ uc $type }{format} )
    {
        return $formatMediaMap{ uc $type }{format};
    }

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

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

    if( exists $formatMediaMap{ uc $type }{media} )
    {
        return $formatMediaMap{ uc $type }{media};
    }

    $self->fail("Unable to determine media type from formatType($type)");
}

sub upc {
    my $self = shift;
    my $upc = $self->_getByFieldName( 'upc' ) || return;
    $upc =~ s/\\//g;
    return $upc;
}


sub isrc {
    my $self = shift;
    my $type = $self->_getByFieldName( 'productIdType' ) || return;
    my $isrc = $self->_getByFieldName( 'isrc' ) if ($type =~ /^ISRC$/i || $type =~ /^GRID$/);

    $isrc =~ s/\\//g;

    return $isrc;
}


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

    return $name if( lc($type) eq 'p' || lc($type) eq 't' )
}

sub price {
    my $price = abs( shift->SUPER::price() );
    return $price;
}

sub units {
    my $self = shift;
    my $units = $self->SUPER::units() || return;
    my $price = $self->SUPER::price();

    # The 'sign' of the units is driven by the price.
    # If the price is negative, we'll store negative units,
    # regardless of whether the units were positive or negative
    # in the sale file.  Accordingly, the price is always
    # positive.  This ensure that the returns are recorded
    # correctly.
    #
    if( defined($price) )
    {
        return $price < 0 ? abs($units) * -1 : $units;
    }
    else
    {
        return $units;
    }
}

sub unitPrice { abs( shift->SUPER::unitPrice ) }


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