#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package RPS::Import::YouTube3;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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

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

use base 'RPS::Import::Importer';

sub _fieldMap {
    my $self = shift;
    my $version = $self->_version;

    my %field_map = (
        19 => {
            countryCode   => 'H',
            artistName    => 'F',
            isrc          => 'C',
            trackName     => 'E',
            price         => 'Q',
        },
        20 => {
            labelName     => 'B',
            countryCode   => 'J',
            artistName    => 'H',
            upc           => 'E',
            isrc          => 'D',
            trackName     => 'G',
            price         => 'S',
        },
        21 => {
            countryCode   => 'J',
            artistName    => 'H',
            upc           => 'E',
            isrc          => 'D',
            trackName     => 'G',
            price         => 'AA',
        },
        22 => {
            labelName     => 'AL',
            mediaType     => 'J',
            currencyCode  => 'B10',
            artistName    => 'AI',
            upc           => 'AH',
            isrc          => 'AF',
            albumName     => 'AK',
            trackName     => 'AJ',
            price         => 'Y',
        },
        23 => {
            labelName     => 'X',
            mediaType     => 'J',
            artistName    => 'U',
            upc           => 'T',
            isrc          => 'R',
            albumName     => 'W',
            trackName     => 'V',
            price         => 'AO',
        },
        24 => {
            labelName     => 'O',
            countryCode   => 'C',
            mediaType     => 'H',
            artistName    => 'M',
            upc           => 'K',
            isrc          => 'J',
            albumName     => 'N',
            trackName     => 'E',
            units         => 'Q',
            price         => 'AA',
        },
    );
    return $field_map{$version};
}


sub _unverifiedFieldMap {
    my $self = shift;
    my $version = $self->_version;

    my %field_map = (
        19 => {
            watchViews    => 'K',
            embedViews    => 'L',
        },
        20 => {
            watchViews    => 'M',
            embedViews    => 'N',
        },
        21 => {
            watchViews    => 'L',
            embedViews    => 'M',
        },
        22 => {
            watchViews    => 'S',
            embedViews    => 'T',
        },
        23 => {
            watchViews    => 'Z',
            embedViews    => 'AA',
        },
        #24 => {
        #  N/A for this version
        #},
    );
    return $field_map{$version};
}

sub _headerIdentifier { ( trackName => 'Asset Title' ) }

sub currencyCode {
    my $self = shift;
    if(exists $self->_fieldMap->{'currencyCode'})
    {
        return $self->_getByFieldName('currencyCode');
    }
    return 'USD';
}

sub productType { RPS::File::Sale::TYPE_TRACK }

sub formatType { RPS::File::Sale::FORMAT_STREAM }

sub mediaType {
    my $self = shift;
    if(exists $self->_fieldMap->{'mediaType'})
    {
        my $type = $self->_getByFieldName('mediaType');
        if( $type =~ /^audio(-visual)?$/i ||
            $type =~ /^art track$/i || $type =~ /^sound recording$/i || $type =~ /^web$/i ||
            $type =~ /^composition$/i )
        {
            return RPS::DB::Item::MediaType::kMediaTypeAudio;
        }
        elsif( $type =~ /^visual$/i || $type =~ /^music video$/i )
        {
            return RPS::DB::Item::MediaType::kMediaTypeVideo;
        }
        else
        {
            $self->fail("No media type specified");
        }
    }
    return RPS::DB::Item::MediaType::kMediaTypeAudio;
}

sub saleDates {
    my $self = shift;
    return( $self->{_dateBegin}, $self->{_dateEnd} );
}

sub units {
    my $self = shift;
    return $self->_getByFieldName('units') if(exists $self->_fieldMap->{'units'});
    my $watchViews = $self->_getByFieldName('watchViews');
    my $embedViews = $self->_getByFieldName('embedViews');
    return $watchViews + $embedViews;
}

sub countryCode {
    my $self = shift;

    if(!exists $self->_fieldMap->{'countryCode'})
    {
        return 'US';
    }

    my $country = $self->SUPER::countryCode();

    $self->fail( "No country specified" )
        unless( $country && length($country) );

    my %countryMap = (
        'xk'     => 'SRB', # Serbia
    );

    $country = $countryMap{lc $country} if( exists $countryMap{lc $country} );

    my $c = new Common::Country( search => $country );
    return $c->alpha2() if( $c );

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

sub _preProcess {

    my $self = shift;

    my %args = @_;
    my $fileName = $args{file}->OrigFileName();

    # Get date information from the filename
    #
    if ( $fileName =~ /(\d{4})(\d{2})(\d{2})_(\d{4})(\d{2})(\d{2})/ ) # YYYYMMDD_YYYYMMDD
    {
        $self->{_dateBegin} = sprintf("%4d-%02d-%02d", $1, $2, $3 );
        $self->{_dateEnd} = sprintf("%4d-%02d-%02d", $4, $5, $6 );
    }
    else
    {
        $self->fail("Couldn't get dates from filename '$fileName'");
    }
    $self->SUPER::_preProcess(%args);
}

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