package Epoll::Controller::Newpoll;

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

=head1 NAME

Epoll::Controller::Newpoll - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index 

=cut

sub begin : Private {
    my ( $self, $c ) = @_;
    $c->model('Vote')->rollback;
    $c->model('Vote')->clean_old_poll_request and $c->model('Vote')->commit;
}

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

    $c->stash->{page}{title} = $c->localize('Create a new vote');
    if ($c->req->param('mail')) {
        if ($c->config->{newpollpasswd}) {
            if (($c->req->param('passwd') || '') eq $c->config->{newpollpasswd})
                {
                    if (my $pid = $c->model('Vote')->create_poll(
                            $c->req->param('mail'),
                            $c->req->param('label'),
                            $c->req->param('vpasswd'),
                        )
                    ) {
                        $c->session->{'vpass' . $pid} = $c->req->param('vpasswd'); # avoid auth on admin page
                        $c->res->redirect($c->uri_for('/admin',
                                $c->model('Vote')->poll_id_from_uid($pid))
                        );
                    }
                } else {
                    $c->stash->{passwderror} = 1;
                }
        } else {
            my $reqid = $c->model('Vote')->create_poll_request(
                mail => $c->req->param('mail'),
                label => $c->req->param('label'),
            );
            $c->forward(
                q'Epoll::View::Mail', 'render',
                [ 'poll_request.tt', {
                    To => $c->req->param('mail'),
                    Subject => $c->localize("New poll request"),
                    mail => {
                        label => $c->req->param('label'),
                        reqid => $reqid,
                    }
                } ]
            );
            $c->stash->{template} = 'newpoll/request.tt';
        }
    }

}

sub default : LocalPath {
    my ( $self, $c, undef, $id ) = @_;

    $id eq 'confirm' and $id = $c->req->param('id') || '';
    $c->stash->{reqid} = $id;

    if (!$c->model('Vote')->poll_request_info($id)) {
        $c->stash->{page}{title} = $c->localize("No vote creation request");
        $c->stash->{template} = 'newpoll/norequest.tt';
        return;
    }

    $c->stash->{page}{title} = $c->localize('Confirm new vote creation');
    if ($c->req->param('passwd')) {
        my $pid = $c->model('Vote')->poll_from_request($id, $c->req->param('passwd'));
        my $uid = $c->model('Vote')->poll($pid)->uid;
        $c->session->{'vpass' . $uid} = $c->req->param('passwd'); # avoid auth on admin page
        $c->res->redirect($c->uri_for('/admin', $uid));
    }
}

sub end : Private {
    my ($self, $c) = @_;
    if ($c->res->body) { return }
    else { $c->forward(qw/Epoll::View::TT/) }
}

=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 or CeCILL.

=cut

1;
