package LATMOS::Accounts::Web::Controller::Root;

use strict;
use warnings;
use base 'Catalyst::Controller';
no utf8;
use POSIX;
use DateTime;
use Date::Calc;
use Date::Parse;

#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config->{namespace} = '';

=head1 NAME

LATMOS::Accounts::Web::Controller::Root - Root Controller for LATMOS::Accounts::Web

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

sub component_disabled : Private {
    my ( $self, $c ) = @_;
    $c->res->status(403);
}

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

    my ($locale) = split(',', $c->req->header('Accept-Language') || '');
    $locale ||= '';
    $locale =~ s/(\s\s)/$1/;
    if (my $plocale = ($c->req->param('locale')|| $c->session->{'locale'})) {
        $c->session->{'locale'} = $locale = $plocale;
    }
    POSIX::setlocale(POSIX::LC_ALL, $locale);
    $c->languages($locale ? [ $locale ] : '');
}

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

    if ($c->user_exists) {
        # Set login user:
        $c->model('Accounts')->db->{_user} = $c->user->{username};
        return 1;
    } else {
        # No need to login for About section
        $c->detach('needlogin');
        return;
    }
}

=head2 default

=cut

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

    $c->res->status(404); 
}

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

    if ($c->user) {
        $c->res->redirect(
            $c->uri_for('/users', $c->user->{username})
        )
    }
}

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

    $c->session->{redirurl} ||= $c->req->uri;
    $c->session->{redirparam} ||= $c->req->params;

    my $sslid = $c->config->{ssl}->{SSL_ID} || 'HTTP_SSL_CLIENT_S_DN_EMAIL';
    $c->stash->{sslid} = $ENV{$sslid};

    $c->stash->{template} = 'login/index.tt';
    return 0;
}

sub no_object : Local {
    my ($self, $c) = @_;
    $c->stash->{template} = 'no_object.tt';
}

sub obj_to_uri : Private {
    my ($self, $c, $ref) = @_;
    my $uri_part = {
        user => 'users',
        group => 'groups',
        nethost => 'nethosts',
        netzone => 'netzones',
        site => 'sites',
        aliases => 'aliases',
        service => 'services',
    }->{$ref} || $ref;
}

sub obj_to_label : Private {
    my ($self, $c, $ref) = @_;
    my $uri_part = {
        user => 'Utilisateurs',
        group => 'Groupes',
        nethost => 'Hôtes réseaux',
        netzone => 'Zones réseau',
        site => 'Sites',
        aliases => 'Alias',
        service => 'Services',
    }->{$ref} || $ref;
}

=head2 end

Attempt to render a view, if needed.

=cut

sub end : ActionClass('RenderView') {
    my ($self, $c) = @_;
    $c->forward($c->view('TT')) unless($c->res->body);
    $c->model('Accounts')->db->rollback;
    $c->model('Accounts')->call_batch_sync;
}

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