package myHive::Controller::Admin::Landowner;
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

myHive::Controller::Admin::Landowner - 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->{manager}           = myHive::Model::Landowner::Manager->new;
        $c->{area}              = myHive::Model::Area::Manager->new;
	$c->stash->{pageType}   = 'landowner';

        if  ( ! $c->session->{account_id}  ) {
                $c->forward('login');
        }


        return;
}



=head2 index

=cut

sub index :Path :Args(0) {
	my ( $self, $c ) = @_;
	my $account_id = $c->session->{account_id};
    	print STDERR "called landowner";
	unless( defined  $account_id  ) {
                $c->forward('login');
        } else {
                my $data = $c->request->body_parameters;
                if ( keys %{ $data } ) {
                        $data->{account} = $c->session->{account_id};
                        $c->{manager}->add( %{ $data } );
                }
                $c->stash->{collection} = $c->{manager}->getLandowners( account => $c->session->{account_id} );
                $c->stash->{data} = $data;
                $c->stash->{template} = 'admin/landowner.tt';
        }
        $c->stash->{pageName} = 'landowner';
	$c->stash->{loggedin}   = $c->session->{name};
    

}

sub update : Local : Args() {
	my ( $self, $c ) = @_;
	if  ( ! $c->session->{account_id}  ) {
                $c->forward('login');
        }
	else {
		my $id     = $c->request->query_parameters->{id} || undef;
		my $data = $c->request->body_parameters;
		if ( keys %{ $data } ) {
			$data->{account} = $c->session->{account_id};
			$data->{id}      = $id;
			$c->{manager}->update( %{ $data } );

		}
		$c->stash->{pageName} = 'landowner';
		$c->stash->{base} = '../';
		$c->stash->{update}       = $c->{manager}->getLandowner( account => $c->session->{account_id}, id => $id );
		$c->stash->{collection} = $c->{manager}->getLandowners( account => $c->session->{account_id} );
		$c->stash->{areas}       = $c->{area}->getAreas(  account => $c->session->{account_id} );
		$c->stash->{loggedin}   = $c->session->{name};
		$c->stash->{template} = 'admin/landowner_details.tt';

	}
		return;

}

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

	my $account_id = $c->session->{account_id};
        if ( ! defined  $account_id  ) {
                $c->forward('login');
        }
        else {

                $c->stash->{template} =  'admin/landowner_new.tt';
                $c->{manager}  = myHive::Model::Site::Manager->new(  account => $c->session->{account_id} );
                $c->stash->{areas}       = $c->{manager}->getAreas(  account => $c->session->{account_id} );
                $c->stash->{loggedin}    = $c->session->{name};
        }
        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

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 ) {
                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;

}






__PACKAGE__->meta->make_immutable;

1;
