package myHive::Controller::Admin::Area;
use Moose;
use namespace::autoclean;
use Data::Dumper;
BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

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


=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;
    if  ( ! defined  $c->session->{account_id}  ) {
                $c->forward('login');
    } else {
	$c->stash->{collection} = $c->{area}->getAreas( account => $c->session->{account_id} );
        $c->stash->{template} = 'admin/area.tt';
	$c->stash->{loggedin}   = $c->session->{name};


   }

    #$c->response->body('Matched myHive::Controller::Admin::Area in Admin::Area.');
	#$c->stash->{template} = 'admin/area.tt';
	return;
}


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 add : Local : Args() {
	my ( $self, $c ) = @_;
	$c->stash->{template} = 'admin/area_new.tt';
	 my $account_id = $c->session->{account_id};
        if ( ! defined  $account_id  ) {
                $c->forward('login');
        }
        else {
		$c->{manager}  = myHive::Model::Area::Manager->new(  account => $c->session->{account_id} );
		my $data = $c->request->body_parameters;
		if ( keys %{ $data } ) {
			$data->{account} = $c->session->{account_id};
			$c->{manager}->add( %{ $data } );
			$c->response->redirect( '/page/admin/area' );	
		}
                $c->stash->{template} =  'admin/area_new.tt';
                $c->stash->{loggedin}    = $c->session->{name};
        }
        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 {
		my $data = $c->request->body_parameters;
		print STDERR Dumper($data);
                my $id     = $c->request->query_parameters->{id} || undef;
                my $update = $c->request->query_parameters->{update} || undef;
		$c->{manager}  = myHive::Model::Area::Manager->new(  account => $c->session->{account_id} );

                if ( $id ) {
                        $c->stash->{update} = $c->{area}->getArea( id => $id );
			$c->stash->{content} = $c->{manager}->getArea( id => $id );

                }
                elsif ( $update ) {
                        $c->{area}->updateArea( name => $data->{aname}, id => $update );
                        $c->stash->{content} = $c->{area}->getArea( id => $update );
                }

                elsif ( keys %{ $data }  ) {
                        $data->{account} = $c->session->{account_id};
                        $c->{area}->add( %{ $data } );
			$c->stash->{content} = $c->{manager}->getArea( id => $id );


                }
		 #$c->{manager}  = myHive::Model::Area::Manager->new(  account => $c->session->{account_id} );
		$c->stash->{loggedin}    = $c->session->{name};
		$c->stash->{template} = 'admin/area_details.tt';

	}
	return;
}




=encoding utf8

=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;
