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


BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

myHive::Controller::Admin - 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;
	if  ( ! $c->session_expires  ) {
                $c->forward('login');
        }

	
	return;
}


=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    $c->response->body('Matched myHive::Controller::Admin in Admin.');
}

sub area : Local : Args() {
	my ( $self, $c, $area ) = @_;
	print STDERR Dumper($c->session);
	if  ( ! defined  $c->session->{account_id}  ) {
                $c->forward('login');
        }
	else {
		my $data = $c->request->body_parameters;
		my $id     = $c->request->query_parameters->{id} || undef;
                my $update = $c->request->query_parameters->{update} || undef;
		if ( $id ) {
			$c->stash->{update} = $c->{area}->getArea( id => $id );

		}
		elsif ( $update ) {
			$c->{area}->updateArea( name => $data->{area_name}, id => $update );
			$c->stash->{update} = $c->{area}->getArea( id => $update );
		}
		
		elsif ( keys %{ $data }  ) {
			$data->{account} = $c->session->{account_id};
			$c->{area}->add( %{ $data } );

		}
		$c->stash->{collection} = $c->{area}->getAreas( account => $c->session->{account_id} );
		$c->stash->{template} = 'admin/area.tt';
	}
	#$c->stash->{collection} = $c->{area}->getAreas( account => $c->session->{account_id} );
	#$c->stash->{template} = 'admin/area.tt';
	return;
}

sub site : Local : Args() {
	my ( $self, $c ) = @_;
	if ( ! $c->session_expires ||  !exists  $c->session->{account_id}  ) {
		$c->forward('login');
	}
	else {

	}
	#$c->stash->{collection} = $c->{area}->getAreas( account => $c->session->{account_id} );
        $c->stash->{template} = 'admin/site.tt';
	return;


}

sub mainmenu : Local : Args() {
	my ( $self, $c ) = @_;
	if ( ! $c->session_expires ||  !exists  $c->session->{account_id}  ) {
                $c->forward('login');
        }
	$c->stash->{loggedin}   = $c->session->{name};
	$c->stash->{template} = 'admin/index.tt';
	return;
}

sub landowner : Local : Args() {
	my ( $self, $c ) = @_;
	print STDERR "called landowner";
	if  ( ! $c->session_expires  ) {
		$c->forward('login');
	}
	else {

		my $data = $c->request->body_parameters;
		if ( keys %{ $data } ) {
			$data->{account} = $c->session->{account_id};
			$c->{landowner}->add( %{ $data } );
		}
		$c->stash->{collection} = $c->{landowner}->getLandowners( account => $c->session->{account_id} ); 
		$c->stash->{data} = $data;
		$c->stash->{template} = 'admin/landowner.tt';
	}
	$c->stash->{pageName} = 'landowner';
}


sub settings : Local : Args() {
	my ( $self, $c ) = @_;
	return;


}



sub login : Global : Args() {
	my ( $self, $c ) = @_;
	my $params = $c->request->body_parameters;
	if ( $c->session_expires && exists  $c->session->{account_id}  ) {
		$c->response->redirect( '/page/admin/mainmenu' );
	}
	if ( keys %$params ) {
		unless ( defined $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' );
                }

	}
	$c->stash->{page}     = 'login';
	$c->stash->{template} = 'admin/login.tt';
	return;

}

sub register : Local : Args() {
	my ( $self, $c ) = @_;
	

	
}

sub logout : Local : Args() {
	my ( $self, $c ) = @_;
	$c->delete_session;
	#$c->forward('login');
	$c->response->redirect( '/page/login' );
	#$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;
