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

extends 'Catalyst::Model';

=head1 NAME

myHive::Model::BroodType::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 'account' 		=> ( is => 'rw', isa => 'Int', 			default => 0 );
has 'handler' 		=> ( is => 'rw', isa => 'myHive::Model::DB', 	default => sub { myHive::Model::DB->new(); } );
has 'collection' 	=> ( is => 'rw', isa => 'HashRef',		default => sub { {}; } );

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

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

}



sub _setBroodTypes {
	my ( $self ) = @_;
	my $id = $self->{account};
	print STDERR "_setBroodTypes called id : $id";
	my $sth = $self->handler->selectSTH(
			table   => qw[broodtype],
			fields  => ['*'],
			where   => qq/account = $id/,
		);
	
	while ( my $row = $sth->fetchrow_hashref ) {
		my $brood = myHive::Model::BroodType->new( {
			account       	=> $id,
			id		=> $row->{id},
			name		=> $row->{name},
		});
			
		push ( @ { $self->collection->{broodtypes} }, $brood );

	} 
	$sth->finish;
	return;

}






__PACKAGE__->meta->make_immutable;

1;
