package LATMOS::Accounts::Web::Controller::About;
use strict;
use warnings;
use base 'Catalyst::Controller';

use POSIX;
use DateTime;
use Date::Calc;
use Date::Parse;

=head1 NAME

LATMOS::Accounts::Web::Controller::About - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

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

    $c->stash->{page}{title} = "A propos...";

    if ($c->user) {
        my $ouser = $c->model('Accounts')->db->get_object(
            'user',
            $c->user->{username},
        );

        my @cd = split('-', DateTime->from_epoch(
            epoch => str2time(
                $ouser->get_attributes('create'),
            )
        )->ymd('-'));
        my @now = split('-', DateTime->now()->ymd('-'));

        my ($Dy,$Dm,$Dd) = Date::Calc::N_Delta_YMD(@cd, @now);
        $c->stash->{accountExists} = join(', ',
            ($Dy ? $c->loc('[_1] years', $Dy)  : ()),
            ($Dm ? $c->loc('[_1] months', $Dm) : ()),
            ($Dd ? $c->loc('[_1] days', $Dd)   : ()),
        );

        if (my $expire = $ouser->get_attributes('expire')) {
            my @ed = split('-', DateTime->from_epoch(
                epoch => str2time(
                    $expire
                )
            )->ymd('-'));

            my ($Dy,$Dm,$Dd) = Date::Calc::N_Delta_YMD(@now, @ed);
            $c->stash->{accountExpire} = join(', ',
                ($Dy ? $c->loc('[_1] years', $Dy)  : ()),
                ($Dm ? $c->loc('[_1] months', $Dm) : ()),
                ($Dd ? $c->loc('[_1] days', $Dd)   : ()),
            );
        }
    }
}

sub echo : XMLRPC {
    my ( $self, $c, @args ) = @_;
    return RPC::XML::fault->new( 400, "No input!"
    ) unless @args;
    return join ' ', @args;
}

=head1 AUTHOR

Olivier Thauvin

=head1 LICENSE

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

=cut

1;
