package myHive::Controller::Admin::Site;
use Moose;
use namespace::autoclean;
use Data::Dumper;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

myHive::Controller::Admin::Site - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

sub begin : Private {
        my ( $self, $c ) = @_;
        $c->config->{session}->{'Catalyst::Plugin::Session'} = 'bug';#horrible
        $c->{authentication}    = myHive::Model::Authentication->new;
        $c->{landowner}         = myHive::Model::Landowner::Manager->new;
        $c->{area}              = myHive::Model::Area::Manager->new;
	$c->stash->{pageType}   = 'site';	
	
        if  ( ! $c->session_expires  ) {
                $c->forward('login');
        }


        return;
}


=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;
    my $account_id = $c->session->{account_id};
    print STDERR "account_id: ".$account_id;
    if ( ! defined  $account_id  ) {
		$c->forward('login');
    }
   else {
		my $params = $c->request->body_parameters;
		my $id     = $c->request->query_parameters->{id} || undef;
		my $update = $c->request->query_parameters->{update} || undef;
		print STDERR Dumper($c->session);
		#$c->stash->{template}   = 'admin/site.tt';

		#return;
		#my $account_id = $c->session->{account_id};
		$c->{manager}  = myHive::Model::Site::Manager->new(  account => $c->session->{account_id} );
		#$c->{manager}->account( $account_id );	
		#$c->stash->{template}   = 'admin/site.tt';
		#return;
		if ( $id ) {
			$c->stash->{update} = $c->{manager}->getSite( id => $id );
		}
		elsif ( $update ) {
			print STDERR "updated callled : $update ";			
			$params->{account} = $c->session->{account_id};
			$params->{site_id} = $update;
			my @broodtypes =  ();
			foreach my $el ( grep(/broodtype_\d/, keys %$params  ) ) {
                                my $id = $el;
                                $id =~ s/broodtype_(\d+)/$1/;
                                push( @broodtypes, qq|{$id, $params->{$el }}| );
                                delete $params->{$el};

                        }
                        $params->{broodtypes} = '{' . join(',', @broodtypes  ) . '}';
			$c->{manager}->update( %{ $params } );
			$c->stash->{update} = $c->{manager}->getSite( id => $update );	

		}
		elsif ( keys %{ $params } ) {
			$params->{account} = $c->session->{account_id};
			my @broodtypes =  ();
			foreach my $el ( grep(/broodtype_\d/, keys %$params  ) ) {
                        	my $id = $el;
                        	$id =~ s/broodtype_(\d+)/$1/;
                        	push( @broodtypes, qq|{$id, $params->{$el }}| );
				delete $params->{$el};

               	 	}
			$params->{broodtypes} = '{' . join(',', @broodtypes  ) . '}';

			$c->{manager}->add( %{ $params } );
		}
		print STDERR Dumper($c->session);
		$c->stash->{loggedin}	= $c->session->{name};
    		#$c->{manager}            = myHive::Model::Site::Manager->new( { account => $c->session->{account_id} } );
    		$c->stash->{landowners}  = $c->{manager}->getLandowners(  account => $c->session->{account_id} );
		$c->stash->{areas}       = $c->{manager}->getAreas(  account => $c->session->{account_id} );
		$c->stash->{sites}	 = $c->{manager}->getSites( account => $c->session->{account_id} );
		$c->stash->{broodtypes}  = $c->{manager}->getBroodTypes( account => $c->session->{account_id} );
		
    }
    $c->stash->{template}   = 'admin/site.tt';
    return;
    #$c->response->body('Matched myHive::Controller::Admin::Site in Admin::Site.');
}

sub add : Local : Args() {
	my ( $self, $c ) = @_;
	my $account_id = $c->session->{account_id};
	if ( ! defined  $account_id  ) {
                $c->forward('login');
    	} else {
		$c->{manager}  = myHive::Model::Site::Manager->new(  account => $c->session->{account_id} );
		my $params = $c->request->body_parameters;


		if (keys %{ $params } ) {
		$params->{account} = $c->session->{account_id};
                        my @broodtypes =  ();
                        foreach my $el ( grep(/broodtype_\d/, keys %$params  ) ) {
                                my $id = $el;
                                $id =~ s/broodtype_(\d+)/$1/;
                                push( @broodtypes, qq|{$id, $params->{$el }}| );
                                delete $params->{$el};

                        }
                        $params->{broodtypes} = '{' . join(',', @broodtypes  ) . '}';

                        $c->{manager}->add( %{ $params } );
			$c->response->redirect( '/page/admin/site' );


		}
   
		$c->stash->{template} =  'admin/site_new.tt';
		$c->stash->{areas}       = $c->{manager}->getAreas(  account => $c->session->{account_id} );
		$c->stash->{broodtypes}  = $c->{manager}->getBroodTypes( account => $c->session->{account_id} );
		$c->stash->{landowners}  = $c->{manager}->getLandowners(  account => $c->session->{account_id} );
		$c->stash->{loggedin}    = $c->session->{name};
		$c->stash->{showmap}	 = 1;
	}
	return;



}

sub update : Local : Args() {
	my ( $self, $c  ) = @_;
	my $account_id = $c->session->{account_id};
	print STDERR "account_id: ".$account_id;
	unless( defined  $account_id  ) {
                $c->forward('login');
	} else {
		$c->{manager}  = myHive::Model::Site::Manager->new(  account => $c->session->{account_id} );
		my $params = $c->request->body_parameters;
		if ( keys %{ $params } ) {
			$params->{account} = $c->session->{account_id};
			my $id     = $c->request->query_parameters->{id} || undef;
			print STDERR 'id = '. $id;
                        $params->{site_id} = $id;
                        my @broodtypes =  ();
                        foreach my $el ( grep(/broodtype_\d/, keys %$params  ) ) {
                                my $id = $el;
                                $id =~ s/broodtype_(\d+)/$1/;
                                push( @broodtypes, qq|{$id, $params->{$el }}| );
                                delete $params->{$el};

                        }
                        $params->{broodtypes} = '{' . join(',', @broodtypes  ) . '}';
			print STDERR Dumper($params);

                        $c->{manager}->update( %{ $params } );
                        #$c->stash->{update} = $c->{manager}->getSite( id => $update );



		}
	my $id     = $c->request->query_parameters->{id};
	$c->{manager}  = myHive::Model::Site::Manager->new(  account => $c->session->{account_id} );
	$c->stash->{content} = $c->{manager}->getSite( id => $id );
	$c->stash->{areas}       = $c->{manager}->getAreas(  account => $c->session->{account_id} );
	$c->stash->{landowners}  = $c->{manager}->getLandowners(  account => $c->session->{account_id} );
	$c->stash->{broodtypes}  = $c->{manager}->getBroodTypes( account => $c->session->{account_id} );

	$c->stash->{template} = 'admin/site_details.tt'; 
	$c->stash->{loggedin}   = $c->session->{name}; 
	}
	return;
}

#sub details : Local : Args

sub login : Local : Args() {
        my ( $self, $c ) = @_;
        my $params = $c->request->body_parameters;
	$c->stash->{page}     = 'login';
        $c->stash->{template} = 'admin/login.tt';
        if ( $c->session_expires  ) {
                $c->response->redirect( '/page/login' );
        }
        if ( keys %$params ) {
		unless ($c->{authentication} ) {
			$c->{authentication}    = myHive::Model::Authentication->new;
		}
                my $account_id = $c->{authentication}->checkCredentials(
                                        username => $params->{username},
                                        password => $params->{password},
                                );

                if ( $account_id )  {
                        $c->session(
                                {
                                        loggedin        => 'yes',
                                        username        =>  $params->{username},
                                        account_id      =>  $account_id,
					name            =>  $c->{authentication}->getUserName( $account_id ),
                                        expires         =>  5
                                }
                        );
                        #return;
			$c->response->redirect( '/page/admin/mainmenu' );
                }

        }
        return;

}



=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

__PACKAGE__->meta->make_immutable;

1;
