package myHive::Model::PestControl::Manager;
use Moose;
use namespace::autoclean;
use myHive::Model::PestControl;

extends 'Catalyst::Model';

=head1 NAME

myHive::Model::PestControl::Manager - Catalyst Model

=head1 DESCRIPTION

Catalyst Model.

=head1 AUTHOR

root

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

has 'handler'           => ( is => 'ro', isa => 'myHive::Model::DB',             	default => sub { myHive::Model::DB->new();      } );
has 'collection'        => ( is => 'rw', isa => 'HashRef',                     		default => sub { {}; }                         	);
has 'account'           => ( is => 'rw', isa => 'Int'          , 			default => 0, required => 1                     );


sub BUILD {
        my ( $self ) = @_;
        $self->collection->{pestcontroltypes} = [];
        return;
}

sub get {
        my ( $self, %params ) = @_;
        #$self->account = $params{account};
        if ( $self->account == 0 ) {
                $self->{account}  = $params{account};
        }
        unless ( @{ $self->collection->{pestcontroltypes} } > 0 ) {
                print STDERR "brood size empty";
                $self->_setPestcontrolTypes;
        }
        return $self->collection->{pestcontrolypes};

}



sub _setPestcontrolTypes {
        my ( $self ) = @_;
        my $id = $self->{account};
        #print STDERR "_setBroodTypes called id : $id";
        my $sth = $self->handler->selectSTH(
                        table   => qw[pestcontrol],
                        fields  => ['*'],
                        where   => qq/account = $id/,
                );

        while ( my $row = $sth->fetchrow_hashref ) {

                my $pestcontroltypes = myHive::Model::PestControl->new( {
                        account         => $id,
                        id              => $row->{id},
                        name            => $row->{description},
                        updated         => $self->getDateTime( $row->{updated} ),
			
                });
		

                push ( @ { $self->collection->{pestcontrolypes} }, $pestcontroltypes );

        }
        $sth->finish;
        return;

}

sub getDateTime {
        my ( $self, $date ) = @_;
        $date =~ m{\A (\d+)-(\d+)-(\d+) \z}xgm;

        return DateTime->new(
                        year       => $1,
                        month      => $2,
                        day        => $3,
                );
}



__PACKAGE__->meta->make_immutable;

1;
