package RPS::Import::VidZoneDigital::v5;

use strict;
use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_date);
use Common::Consts;
use Common::Country;

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

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

sub _fieldMap {
    {
        # header
        currencyCode => 'P',

        # detail
        dateBegin    => 'L',
        serviceID    => 'H',
        countryCode  => 'G',
        productType  => 'K',
        upc          => 'A',
        albumName    => 'F',
        isrc         => 'B',
        trackName    => 'D',
        units        => 'M',
        price        => 'P',
    }
}

sub _unverifiedFieldMap {
    {
        trackArtist => 'C',
        albumArtist => 'E',
    }
}

my %gProductFormatMediaMap = (
    "real tune" => {
        product_type => RPS::File::Sale::TYPE_TRACK,
        format_type  => RPS::File::Sale::FORMAT_RINGTONE,
        media_type   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    "music video" => {
        product_type => RPS::File::Sale::TYPE_TRACK,
        format_type  => RPS::File::Sale::FORMAT_DOWNLOAD,
        media_type   => RPS::DB::Item::MediaType::kMediaTypeVideo,
    },
    "audio track" => {
        product_type => RPS::File::Sale::TYPE_TRACK,
        format_type  => RPS::File::Sale::FORMAT_DOWNLOAD,
        media_type   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    "dial tune" => {
        product_type => RPS::File::Sale::TYPE_TRACK,
        format_type  => RPS::File::Sale::FORMAT_RINGTONE,
        media_type   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    "on demand stream" => {
        product_type => RPS::File::Sale::TYPE_TRACK,
        format_type  => RPS::File::Sale::FORMAT_STREAM,
        media_type   => RPS::DB::Item::MediaType::kMediaTypeVideo,
    },
    "tv channel stream" => {
        product_type => RPS::File::Sale::TYPE_TRACK,
        format_type  => RPS::File::Sale::FORMAT_STREAM, # was FORMAT_VIDEOSTREAM (FB1324).
        media_type   => RPS::DB::Item::MediaType::kMediaTypeVideo,
    },
    "product" => {
        product_type => RPS::File::Sale::TYPE_ALBUM,
        format_type  => RPS::File::Sale::FORMAT_DOWNLOAD,
        media_type   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    "streaming audio pc" => {
        product_type => RPS::File::Sale::TYPE_TRACK,
        format_type  => RPS::File::Sale::FORMAT_STREAM,
        media_type   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    "streaming video pc" => {
        product_type => RPS::File::Sale::TYPE_TRACK,
        format_type  => RPS::File::Sale::FORMAT_STREAM,
        media_type   => RPS::DB::Item::MediaType::kMediaTypeVideo,
    },
);

sub _headerIdentifier { ( upc => 'UPC' ) }

sub currencyCode {
    my $self = shift;
    return $self->{_currencyCode} if( $self->{_currencyCode} ); # from header; see _preProcess
}

sub artistName {
    my $self = shift;

    if( $self->productType() eq RPS::File::Sale::TYPE_ALBUM )
    {
        return $self->_getByFieldName('albumArtist');
    }
    else
    {
        return $self->_getByFieldName('trackArtist');
    }
}

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

    if( ! exists  $gProductFormatMediaMap{ lc $type }{product_type} )
    {
        $self->fail("Unable to get product type from '$type'");
    }
    return $gProductFormatMediaMap{ lc $type }{product_type};
}

sub formatType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    my $service = $self->_getByFieldName('serviceID');
    
    if( $type =~ /^audio track$/i && $service =~ /^itunes match$/i )
    {
        # If the content type shows 'audio track' and the vendor
        # shows 'itunes match', then treat the sale as a stream.
        # See Case 10.
        #
        return RPS::File::Sale::FORMAT_STREAM;
    }

    # .. otherwise get the format from the productFormat map
    #
    return $gProductFormatMediaMap{ lc $type }{format_type};
}

sub mediaType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    return $gProductFormatMediaMap{ lc $type }{media_type};
}

sub countryCode {
    my $self = shift;
    my $country = $self->_getByFieldName('countryCode');
    my $o = Common::Country->Get( $country );
    if( $o )
    {
        return $o->alpha2();
    }
    else
    {
        $self->errstr("Unknown country '$country'");
    }
}

sub saleDates {
    my $self = shift;

    my $startDate = $self->_getByFieldName('dateBegin');
    if( $startDate =~ /\d{5}/ )
    {
        $startDate = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $startDate );
    }
    return $self->_getDates( date_begin => $startDate );
}

sub _processSheet {
    my $self  = shift;
    my $sheet = shift;

    my $sheetnum = $self->{sheetnum};

    if ( $self->{sheetname} !~ /^summary$/i )
    {
        my $foundCurrency; # set if we find a currency header on current sheet

        foreach my $line(@$sheet)
        {
            $self->{line} = $line;

            my $cc = $self->_getByFieldName('currencyCode');

            if( $self->_getByFieldName('currencyCode') =~ /value \((\w+)\)/i )
            {
                # We should be setting _currencyCode from each sheet 
                #
                $self->{_currencyCode} = $1;

                $foundCurrency = 1;
                last;
            }
        }

        if( !$foundCurrency )
        {
           $self->fail("Unable to find currency information on sheet $sheetnum");
        }
    }

    return $self->SUPER::_processSheet( $sheet );

}

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

    $self->fail( "Service name not defined" ) unless( $service );

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

    $self->fail( "Unable to determine service id from '$service'" ) unless( $serviceID );

    return $serviceID;
}

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