package Period::YearPeriod;

use strict;

#use warnings;

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

use constant DB_TABLE    => "period_month";
use constant PERIOD_TYPE => "Y";

# private attributes
my @attributes     = qw(year date_begin date_end);
my @xml_attributes = qw(PeriodID PeriodType StartDate EndDate);

# --------------------------------
# Constructor
# --------------------------------
sub new {
    my $class = shift;
    my %args  = @_;

    my $self = $class->SUPER::new(@_);
    $self->_init(%args);

    return $self;
}

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

    # initialize user properties
    foreach (@attributes) {
        $self->{$_} = undef;
    }

    # initialize user objects
    $self->{errstr} = undef;

    # are we loading an existing Period?
    my $id = $args{period_id} || $args{year} || 0;
    if ( $id > 0 ) {
        $self->Load( year => $id );
    }
}

# --------------------------------
# Properties
# (month_id date_begin date_end quarter year);
# --------------------------------
sub PeriodID {
    my $self = shift;
    $self->{year};
}

sub PeriodType {
    return PERIOD_TYPE();
}

sub StartDate {
    my $self = shift;
    return $self->{date_begin};
}

sub EndDate {
    my $self = shift;
    return $self->{date_end};
}

sub Year {
    my $self = shift;
    return $self->{year};
}

sub GetObjectXML {
    my $self = shift;

    my $xml = $self->SUPER::GetObjectXML(@xml_attributes);

    my $start_href = { Day => '01', Month => 'Jan', Year => $self->Year };
    my $end_href   = { Day => 31,   Month => 'Dec', Year => $self->Year };

    $xml->{StartDateDisp} = $start_href;
    $xml->{EndDateDisp}   = $end_href;
    $xml->{PrevPeriodID}  = $self->GetPrevPeriodID();
    $xml->{NextPeriodID}  = $self->GetNextPeriodID();

    return $xml;
}

# look in agg_q_summary (month_id)
sub GetPrevPeriodID {
    my $self = shift;

    my $sql  = 'SELECT max(year) FROM agg_y_summary WHERE year < ' . $self->PeriodID;
    my $sth  = $self->{dbo}->DoCmd($sql);
    my $aref = $sth->fetchrow_arrayref() if $sth;

    return $aref ? $aref->[0] : undef;
}

sub GetNextPeriodID {
    my $self = shift;

    my $sql  = 'SELECT min(year) FROM agg_y_summary WHERE year > ' . $self->PeriodID;
    my $sth  = $self->{dbo}->DoCmd($sql);
    my $aref = $sth->fetchrow_arrayref() if $sth;

    return $aref ? $aref->[0] : undef;
}

sub Error {
    my $self = shift;
    $self->{errstr};
}

# -------------------------------
# Public Methods
# -------------------------------
sub LoadCurrent {
    my $self = shift;

    my $sql  = "SELECT max(year) FROM agg_y_summary";
    my $sth  = $self->{dbo}->DoCmd($sql);
    my $aref = $sth->fetchrow_arrayref() if $sth;

    return undef unless ( $aref && $aref->[0] );
    $self->Load( period_id => $aref->[0] );
}

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

    my $year;
    unless ( defined $args{period_id} && $args{period_id} =~ /^\d+$/ ) {
        $self->{errstr} = "period_id ($args{period_id}) not specified or not valid";
        return undef;
    }

    $year = $args{period_id};

    $self->_load( {
            year       => $year,
            date_begin => $year . '-01-01',
            date_end   => $year . '-12-31',
        }
    );

    return 1;
}

# -------------------------------
# Private Methods
# -------------------------------
sub _load {
    my $self = shift;
    my $href = shift;

    map { $self->{$_} = $href->{$_} } @attributes;
}

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