package RPS::Import::WMG::SprintBase;

use strict;

use Date::Calc qw(Days_in_Month Add_Delta_Days);

use lib '/app/tools/data_classes/lib';

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::WMG::Util;
use base 'RPS::Import::Importer';

sub _decodeSprintFormatID
{
    my $self = shift;
    my $title = shift;

    return
        $title =~ /B0578/ ? 'voiceringer' :
        $title =~ /B0656/ ? 'screen_saver' :
        $title =~ /B1625/ ? 'mastertone' :
        $title =~ /B1626/ ? 'mastertone' :
        $title =~ /B1627/ ? 'mastertone' :
        $title =~ /B2175/ ? 'videoringer' :
        $title =~ /B2176/ ? 'videoringer' :
        $title =~ /B2540/ ? 'dual_download' :
        '';
}

sub _parseDescription
{
    my $self = shift;
    my $desc = reverse shift;
    my $formatMatch = shift;

    $desc =~ s/^.*?$formatMatch\s*-\s*//i; # strip the potential format name
    $desc =~ s/^(.+?)\s*-\s*//;

    my $artist = reverse $1;
    $desc = reverse $desc;

    return ($desc, $artist);
}

sub _setDateRangeFromNumeric
{
    my $self = shift;
    my $lines = shift;
    my $dateField = shift;

    my $minDate = 100000;
    my $maxDate = 0;
    foreach my $line(@$lines)
    {
        my $date = $self->numeric($line->[$dateField]);
        next unless $date;
        $minDate = $date if ($date < $minDate);
        $maxDate = $date if ($date > $maxDate);
    }

    my $dateBegin = $self->numericToDate($minDate);
    my $dateEnd   = $self->numericToDate($maxDate);

    ($dateBegin, $dateEnd) = Common::Util::normalize_sales_month_dates($dateBegin, $dateEnd);
    ($self->{_yearBegin}, $self->{_monthBegin}) = $dateBegin =~ m/^(\d\d\d\d)-(\d\d)/;
    ($self->{_yearEnd}, $self->{_monthEnd}) = $dateEnd =~ m/^(\d\d\d\d)-(\d\d)/;
    #($self->{dateBegin}, $self->{dateEnd}) = Common::Util::normalize_sales_month_dates($dateBegin, $dateEnd);
}

sub _getDates
{
    my $self = shift;
    my $saleDate = shift;

    my ($year, $month);
    if ($saleDate =~ m/^(\d{4})\D(\d+)\D/)
    {
        ($year, $month) = ($1, $2);
    }
    elsif ($saleDate =~ m/^(\d\d)\D\d+\D(\d{4})/)
    {
        ($month, $year) = ($1, $2);
    }
    elsif ($saleDate =~ m/^\d+$/)
    {
        my $day;
        ($year, $month, $day) = $self->numericToDate($saleDate);
    }

    return unless $month;

    return (
        sprintf("%d-%02d-01", $year, $month),
        sprintf("%d-%02d-%d", $year, $month, Days_in_Month($year, $month))
    );
}

sub _getDatesFromNumeric
{
    my $self = shift;
    my $num = shift;

    my ($year, $month, $day) = $self->numericToDate($num);

    if ($self->{_yearBegin})
    {
        if ($year > $self->{_yearEnd})
        {
            $year = $self->{_yearEnd};
            $month = $self->{_monthEnd};
        }
        elsif ($month > $self->{_monthEnd})
        {
            $month = $self->{_monthEnd};
        }
        elsif ($year < $self->{_yearBegin})
        {
            $year = $self->{_yearBegin};
            $month = $self->{_monthBegin};
        }
        elsif ($month < $self->{_monthStart})
        {
            $month = $self->{_monthBegin};
        }
    }

    return (
        sprintf("%4d-%02d-01", $year, $month),
        sprintf("%4d-%02d-%0d", $year, $month, Days_in_Month($year, $month))
    );
}

sub _getDatesMY
{
    my $self = shift;
    my ($month, $year) = @_;

    return unless ($month && $year);
    return (
        sprintf("%4d-%02d-01", $year, $month),
        sprintf("%4d-%02d-%0d", $year, $month, Days_in_Month($year, $month))
    );
}

sub numericToDate
{
    my $self = shift;
    my @date = Add_Delta_Days(1900,1,1,shift);
    return wantarray ? @date : sprintf("%d-%02d-%02d", @date);
}

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