#!/usr/bin/perl -w
#
package Common::Date::Pattern;

use strict;

use Common::Assert;

sub new {
    my $class = shift;
    my $self = bless {}, $class;

    $self->_init(@_);
    return $self;
}

sub _init {
    my $self = shift;
    my %args = @_;

    $self->{_dayIndex}   = $args{day};
    $self->{_monthIndex} = $args{month};
    $self->{_yearIndex}  = $args{year};

    $self->{_pattern} = $args{pattern};

    assert( $self->{_pattern} );

    assert( $self->{_dayIndex} =~ /^\d+$/ )   if ( $self->{_dayIndex} );
    assert( $self->{_monthIndex} =~ /^\d+$/ ) if ( $self->{_monthIndex} );
    assert( $self->{_yearIndex} =~ /^\d+$/ )  if ( $self->{_yearIndex} );
}

sub match {
    my $self = shift;
    my $data = shift || return;

    if ( $data =~ /^$self->{_pattern}$/i ) {
        my @match = ( $1, $2, $3 );

        $self->{_day} = $match[ $self->{_dayIndex} - 1 ]
          if ( $self->{_dayIndex} );
        $self->{_month} = $match[ $self->{_monthIndex} - 1 ]
          if ( $self->{_monthIndex} );
        $self->{_year} = $match[ $self->{_yearIndex} - 1 ]
          if ( $self->{_yearIndex} );

        Log->debug(" - Test if '$data' matches $self->{_pattern} - Match");
        return 1;
    }

    Log->debug(" - Test if '$data' matches $self->{_pattern} - No Match");
    return undef;
}

sub pattern { shift->{_pattern} }

sub monthIndex { shift->{_monthIndex} }
sub dayIndex   { shift->{_dayIndex} }
sub yearIndex  { shift->{_yearIndex} }

sub month { shift->{_month} }
sub day   { shift->{_day} }
sub year  { shift->{_year} }

1;
