package LATMOS::Accounts::Web::Controller::Ajax;

use strict;
use warnings;
use base 'Catalyst::Controller';

=head1 NAME

LATMOS::Accounts::Web::Controller::Ajax - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index 

=cut

sub index : Private {
    my ( $self, $c ) = @_;

    $c->response->body('Matched LATMOS::Accounts::Web::Controller::Ajax in Ajax.');
}

sub rawattr : Local {
    my ($self, $c, $otype) = @_;

    my $base = $c->model('Accounts')->db;

    if (!$c->req->param('id')) {
        $c->stash->{value} = '';
        return;
    }

    my $obj = $base->get_object($otype, $c->req->param('id')) or do {
        $c->stash->{value} = '';
        return;
    };
    $c->stash->{value} = $obj->get_c_field($c->req->param('attr')) || '';
}

sub displayattr : Local {
    my ($self, $c, $otype, $attr) = @_;
    $attr ||= $c->req->param('attr');
    my $attribute = $c->model('Accounts')->db->attribute($otype, $attr) or return;
    $c->stash->{value} = $attribute->display($c->req->param('id'));
}

sub attrvalues : Local {
    my ($self, $c, $otype, $attr) = @_;

    my $base = $c->model('Accounts')->db;
    my $val = $c->req->param('val') || '';
    $c->stash->{values} = [
        sort
        grep { /^\Q$val\E/ } $base->attributes_summary($otype, $attr)
    ];

}

sub objexist : Local {
    my ($self, $c, $otype) = @_;
    my $base = $c->model('Accounts')->db;
    my $val = $c->req->param('val');
    my $obj = $base->get_object($otype, $val);

    $c->stash->{'exists'} = join(', ', $obj->get_attributes($c->req->param('attr') ||
            'displayName') || $obj->id) if($obj);
}

sub check_new_name : Local {
    my ($self, $c, $otype) = @_;
    my $base = $c->model('Accounts')->db;
    my $val = $c->req->param('val');

    warn $otype;
    if ($c->stash->{validity} = (lc($otype) eq 'user' || lc($otype) eq 'group')
        ? LATMOS::Accounts::Utils::check_ug_validity($val)
        : LATMOS::Accounts::Utils::check_oid_validity($val)) {
        warn $c->stash->{validity};
        return;
    }

    if (my $obj = $base->get_object($otype, $val)) {
        $c->stash->{validity} = "$val exists " .
            ($obj->get_attributes('displayName') || '')
    }
}

sub objattrexist : Local {
    my ($self, $c, $otype, $attr) = @_;
    my $base = $c->model('Accounts')->db;
    my $attribute = $base->attribute($otype, $attr) or return;
    my $val = $attribute->input($c->req->param('val'));
    my @obj = $val ? $base->search_objects($otype, "$attr=$val") : ();

    if (my $filter = $c->req->param('exclude')) {
        @obj = grep { $_ ne $filter } @obj;
    }
    $c->stash->{'exists'} = join(', ', @obj);
}

sub cracklib : Local {
    my ( $self, $c, $login ) = @_;
    my $base = $c->model('Accounts')->db;
    my ($password)  = $c->req->param('passwd');
    my ($cpassword) = $c->req->param('cpasswd');
    if (my $obj = $base->get_object('user', $login)) {
        if ($password) {
            if ((my $crl = $obj->check_password($password)) ne 'ok') {
                $c->stash->{cracklib} = 'Mauvais mot de passe: ' . $crl;
            } elsif ($cpassword) {
                if ($password ne $cpassword) {
                    $c->stash->{cracklib} = "Mot de passe différents";
                } else {
                    $c->stash->{cracklib} = 'ok';
                }
            } else {
                $c->stash->{cracklib} = 'Confirmez le mot de passe';
            }
        }
    }
}

sub check_ug_validity : Local {
    my ( $self, $c ) = @_;
    my $val = $c->req->param('val');
    $c->stash->{validity} = LATMOS::Accounts::Utils::check_ug_validity($val);
}

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

}

sub user_to_group_form : Local {
    my ($self, $c, $user) = @_;
    
    my $base = $c->model('Accounts')->db;

    $c->stash->{user} = $base->get_object('user', $user) or do {
        return;
    };

    my %ingroups =
        map { $_ => 1 } 
        @{ $c->stash->{user}->get_c_field('memberOf') || []};

   $c->stash->{othergroups} = [ grep { ! $ingroups{$_} }
       $base->search_objects('group', 'cn~' . ($c->req->param('gfilter') || '*')) ]; 
}

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

    my $objurl = $c->req->param('otype');
    $objurl .= 's' unless ($objurl =~ /s$/);

    $c->res->redirect(
        $c->uri_for('/' . $objurl, $c->req->param('goto'),
        $c->req->param('subform'))
    );
    $c->stash->{template} = 'no_object.tt';
}

sub set_attrs : Local {
    my ($self, $c, $otype, $name) = @_;

    my $base = $c->model('Accounts')->db;
    my $object = $base->get_object($otype, $name) or do {
        return;
    };
    my $form = $c->model('AttrForms', '', $object);
    $form->set_attrs([ $c->req->param('attrs') ]);

}

sub list_objects : Local {
    my ($self, $c, $otype) = @_;
    $c->stash->{ofilter} = $c->model('AttrFilter', $otype);
}

sub select_objects_list : Local {
    my ($self, $c, $otype) = @_;
    $c->stash->{ofilter} = $c->model('AttrFilter', $otype);
}

sub end : Private {
    my ( $self, $c ) = @_;
    $c->forward($c->view('TTpart'));
}

=head1 AUTHOR

Thauvin Olivier

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
