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

package RPS::Import::Remark::v1;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);
use Spreadsheet::ParseExcel;

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

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

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

sub _fieldMap {
    {
        dateBegin        => 'A',
        formatType       => 'G',
        artistName       => 'F',
        serviceProductID => 'C',
        isrc             => 'D',
        trackName        => 'E',    # can be track or album title
        units            => 'L',
        price            => 'M',
    };
}

sub _headerIdentifier { ( artistName => qr/\x{ff21}\x{ff32}\x{ff34}\x{ff29}\x{ff33}\x{ff34}/ ) }    # ARTIST

sub _isValidSaleRecord {
    my $self = shift;

    return $self->units || $self->price;
}

sub countryCode { 'JP' }

sub currencyCode { 'JPY' }

sub trackName {
    my $self  = shift;
    my $title = $self->_getByFieldName('trackName');
    return ( $self->productType eq RPS::File::Sale::TYPE_TRACK ) ? $title : "";
}

sub albumName {
    my $self  = shift;
    my $title = $self->_getByFieldName('trackName');
    return ( $self->productType eq RPS::File::Sale::TYPE_ALBUM ) ? $title : "";
}

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

    if ( $fmt =~ /^album \(full length true tone\)$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif (
        $fmt =~ /^full length true tone$/i
        || $fmt =~ /^ring back tone$/i
        || $fmt =~ /^True tone$/i
        || $fmt =~ /^movie$/i
        || $fmt =~ /^SmartphoneFulllengthtruetone$/i
        || $fmt =~ /^SmartphoneTruetone$/i
        || $fmt =~ /^#N\/A!$/i
        || $fmt =~ /^\x{30EA}\x{30F3}\x{30B0}\x{30D0}\x{30C3}\x{30AF}\x{30C8}\x{30FC}\x{30F3}$/i
        ||    # リングバックトーン
        $fmt =~ /^streaming$/i
      ) {
        return RPS::File::Sale::TYPE_TRACK;
    }
    $self->fail("Unknown product type '$fmt'");
}

sub mediaType {
    my $self = shift;
    my $fmt  = $self->_getByFieldName('formatType');
    if (   $fmt =~ /^full length true tone$/i
        || $fmt =~ /^ring back tone$/i
        || $fmt =~ /^SmartphoneFulllengthtruetone$/i
        || $fmt =~ /^SmartphoneTruetone$/i
        || $fmt =~ /^True tone$/i
        || $fmt =~ /^album \(full length true tone\)$/i
        || $fmt =~ /^#N\/A!$/i
        || $fmt =~ /^\x{30EA}\x{30F3}\x{30B0}\x{30D0}\x{30C3}\x{30AF}\x{30C8}\x{30FC}\x{30F3}$/i
        || $fmt =~ /^streaming$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    } elsif ( $fmt =~ /^movie$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }
    $self->fail("Unknown format type '$fmt'");
}

sub saleDates {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin');

    if ( $dateBegin =~ /^(\d{4})\/(\d{1,2})$/ ) {
        my ( $year, $month ) = ( $1, $2 );
        $dateBegin = sprintf( "%04d-%02d-01", $year, $month );

    } elsif ( $dateBegin =~ /\d{5}/ ) {
        $dateBegin = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $dateBegin );
    }

    return $self->_getDates( date_begin => $dateBegin );
}

sub formatType {
    my $self = shift;
    my $fmt  = $self->_getByFieldName('formatType');
    if (   $fmt =~ /^full length true tone$/i
        || $fmt =~ /^ring back tone$/i
        || $fmt =~ /^SmartphoneFulllengthtruetone$/i
        || $fmt =~ /^SmartphoneTruetone$/i
        || $fmt =~ /^True tone$/i
        || $fmt =~ /^album \(full length true tone\)$/i
        || $fmt =~ /^\x{30EA}\x{30F3}\x{30B0}\x{30D0}\x{30C3}\x{30AF}\x{30C8}\x{30FC}\x{30F3}$/i ) {
        return RPS::File::Sale::FORMAT_RINGTONE;
    } elsif ( $fmt =~ /^movie$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $fmt =~ /^#N\/A!$/i
        || $fmt =~ /^streaming$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    $self->fail("Unknown format type '$fmt'");
}

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

    if ( $self->{sheetname} =~ /^REPORT$/i ) {
        return $self->SUPER::_processSheet( $sheet, $sheetnum );
    }
}

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

    # Convert any fractional units into regular integers.
    #
    return int($u);
}

sub upc {
    my $self = shift;

    return $self->productType eq RPS::File::Sale::TYPE_ALBUM
        ? $self->_getByFieldName('isrc')
        : undef;
}

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