package myHive::Controller::API::SiteInspection;
use Moose;
use namespace::autoclean;
use Data::Dumper;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

myHive::Controller::API::SiteInspection - 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::Site::Manager->new;

        return;
}




=head2 index

=cut

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

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

=head2 add


| Parameter                           | Value                                |
+-------------------------------------+--------------------------------------+
| AFP_CHECK                           | <null>                               |
| BROOD_4                             | 2                                    |
| BROOD_5                             | 2                                    |
| BROOD_6                             | 2                                    |
| EXTRA_INFO                          |                                      |
| HASH                                | 791c9badeed782872ee4216aa1479544     |
| HEAlTH                              | Good                                 |
| ID				      | 1                                    | 
| IMAGE                               | no photo                             |
| INSP_DATE                           | 06/08/2013                           |
| PEST_CONTROL                        | 0                                    |
| SITE_ID                             | 3                                    |
| TYPE                                | Inspection                           |



=cut


sub add :Local : Args() {
        my ( $self, $c, $seed ) = @_;
        if ($seed eq $c->session->{seed} ) {

                my %params;
                my $data = $c->request->body_parameters;
		my @uploads= $c->request->upload;
		if ( scalar(@uploads) > 0  ) {
			print STDERR "has image";
			print STDERR "upload" .Dumper($c->request->uploads );
			my $upload = $c->request->upload('image');
			my $new_file = $upload->filename;
                	$upload->copy_to(qq{/var/www/myHive/images/$new_file});
		}

                $params{account}                = $c->session->{account_id};
                $params{siteId}                 = $data->{SITE_ID};
                $params{date}                   = $data->{INSP_DATE};
		$params{type}                   = $data->{TYPE};
		$params{health}			= $data->{HEALTH};
		$params{hives}                  = $data->{HIVES};
		$params{afb}			= $data->{AFB_CHECK} || q{no};
		$params{pestcontrol}		= $data->{PEST_CONTROL};
		$params{photo}			= $data->{IMAGE};
		$params{hash}			= $data->{HASH};
		#$params{broods}                 = [];
		my $broods			= [];
		foreach my $broodKey (grep(/BROOD_\d/ , keys %$data ) ) {
				# my $types = '{' . join(',', @{ $params{types} } ) . '}';
				my $broodCount = $data->{$broodKey};
				$broodKey =~ s/BROOD_(\d+)/$1/;
				push(@{$broods} , qq|{$broodKey,$broodCount}| );
				
		}
		$params{broods} = '{' . join(',', @{ $broods } ) . '}';

		



                $c->{manager}                   =  myHive::Model::Site::Manager->new(  account => $c->session->{account_id} );
                my ($status  )     = $c->{manager}->addInspection( %params );
                if ( $status) {
                        $c->stash->{updated} = 'Y';
                        $c->stash->{hash} = $params{hash};
                }
                else {
                        $c->stash->{updated} = 'N';
                }
        
        }
        else {
                $c->stash->{updated} = 'timed out';
        }
        $c->stash->{template} = 'rest/addsiteinspection.tt';
        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;
