package myHive::Model::InspectionType::Manager;
use Moose;
use namespace::autoclean;
use myHive::Model::InspectionType;
use DateTime;
extends 'Catalyst::Model';

=head1 NAME

myHive::Model::InspectionType::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::Site',             default => sub { myHive::Model::DB::Site->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->{inspectiontypes} = [];
        return;
}

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

}



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

        while ( my $row = $sth->fetchrow_hashref ) {
		
                my $inspectiontypes = myHive::Model::InspectionType->new( {
                        account         => $id,
                        id              => $row->{id},
                        name            => $row->{name},
			updated		=> $self->getDateTime( $row->{updated} ),
                });

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

        }
        $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;
