package LATMOS::Accounts::Web::Model::AttrFilter;

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

=head1 NAME

LATMOS::Accounts::Web::Model::AttrFilter - Catalyst Model

=head1 DESCRIPTION

Catalyst Model.

=cut

my $fields = {
    user => [ qw(uid sn givenName description uidNumber gidNumber company department manager) ],
    group => [ qw(cn description gidNumber sutype) ],
    site => [ qw(l) ],
    nethost => [ qw(name cname otherName ip macaddr owner serialNumber description) ],
    netzone => [ qw(net group type site) ],
    aliases => [ qw(as) ],
};

sub new {
    my ($class) = @_;
    return bless({}, $class);
}

sub ACCEPT_CONTEXT {
    my ($self, $c, $otype) = @_;

    my $new = {};
    $new->{c} = $c;
    $new->{otype} = $otype;
    $new->{base} = $c->model('Accounts')->db;
    $self = bless($new, __PACKAGE__);

    my $base = $new->{base};

    my @objlist = ();
    if ($c->req->params->{'q'} || exists($c->req->params->{'attr'}) ) {
        my %objs = ();
        if ($c->req->params->{'q'}) {
            foreach my $attr (@{ $c->config->{objects}{$otype}{quick_search} || [ 'name' ]}) {
                foreach ($base->search_objects(
                        $otype, "$attr~" . $c->req->params->{'q'},
                                'oalias=NULL',
                    )) {
                    $objs{$_} = 1;
                }
                @objlist = sort keys %objs;
            }
        }

        if (exists($c->req->params->{'attr'})) {
            $self->{filter}{'attr'} = [ $c->req->param('attr') ];
            $self->{filter}{'attrval'} = [ $c->req->param('attrval') ];
            my @filter = $self->filter;
            @objlist = $base->search_objects($otype, @filter);
            if ($c->req->params->{'q'}) {
                @objlist = grep { $objs{$_} } @objlist;
            }
        }
    } else {
        @objlist = $base->list_objects($otype);
    }

    $self->{objectslist} = \@objlist;

    my $start = $c->req->param('start');
    $self->{uparams} = { %{ $c->req->params || {} } };
    my %initials;
    my @sublist;
    foreach (@{ $self->{objectslist} }) {
        $initials{substr($_, 0, 1)} = 1;
        if ($start) {
            if (index($_, $start) == 0) {
                push(@sublist, $_);
            }
        }
    }
    $self->{sublist} = [ @sublist ];
    $self->{initials} = [ sort keys %initials ];

    $self
}

sub otype {
    my ($self) = @_;
    $self->{otype};
}

sub submit {
    my ($self) = @_;
    return sprintf(
        '<input type="submit" name="%s">',
        $self->escape($self->label),
    );
}

sub current_value {
    my ($self) = @_;
    my @vals = @{ $self->{filter}{attrval} || [] };
    my @fies = @{ $self->{filter}{attr} || [] };
    my @couples;
    foreach (@fies) {
        my $val = shift(@vals);
        $_ && $val or next;
        push(@couples, { f => $_, v => $val});
    }
    push(@couples, {});
    @couples;
}

sub count_field {
    my ($self) = @_;
    return [ (0 .. (scalar($self->current_value) -1)) ];
}

sub search_field {
    my ($self, $count) = @_;
    my $form = $self->{c}->model('AttrForms', '', $self->otype, $self->{c}->stash->{db});
    my $html = '<select name="attr">
    <option value=""></option>';
    my @values = $self->current_value;
    foreach (@{ $fields->{$self->otype} }) {
        $html .= sprintf('<option value="%s"%s>%s</option>' . "\n",
            $_,
            (($values[$count]->{f} || '') eq $_ ? ' selected="on"' : ''),
            $form->attr_label($_),
        );
    }

    $html .= '</select>';
}

sub val_field {
    my ($self, $count) = @_;
    my @values = $self->current_value;
    return sprintf('<input type="text" name="attrval" value="%s">',
        $values[$count]->{v} || '');
}

sub filter {
    my ($self) = @_;
    map { $_->{f} . '~' .  $_->{v} } grep { $_->{f} } $self->current_value;
}

sub objectslist {
    my ($self) = @_;
    $self->{objectslist};
}

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