package Vee::Controller::Index;

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

use DateTime;

__PACKAGE__->config->{namespace} = '';

=head1 NAME

Vee::Controller::Index - Index Controller

=head1 SYNOPSIS

See L<Vee>.

=head1 DESCRIPTION

Catalyst Controller for the index page.

=head1 METHODS

=cut

=head2 root

=cut

my $shoutbox_fields = {
    name => { type => 'text', size => 20, maxlength => 20 },
    content => { type => 'textarea', rows => 3, cols => 20 },
};

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

    my $s = $c->stash;
    $s->{page_title} = 'Home/News';
    $s->{link_name}  = 'main';
    $s->{extra_css}  = [ 'index', 'bbcode' ];

    my $news_forum = $c->model('DBIC::Forums')->find( $c->site_opts->{special_forums}{news} );
    my $page_sizes = $c->site_opts->{page_sizes}{index};

    $s->{recent_news}   = $news_forum->threads( undef, { prefetch => 'first_post', order_by => 'me.id DESC', rows => $page_sizes->{news} } );
    $s->{recent_news}   = $s->{recent_news}->search(\ 'NOT FIND_IN_SET("deleted", me.flags)') unless $c->can_i(override_thread_deleted => $news_forum->id);

    $s->{recent_shouts} = $c->model('DBIC::Shoutbox')->search( undef, { order_by => 'me.time DESC', rows => $page_sizes->{shoutbox} } );
    $s->{recent_art}    = $c->model('DBIC::Gallery')->search( undef, { order_by => 'me.time DESC', rows => $page_sizes->{gallery} } );

    # TODO: break this off into a "new posts" sort of page, and just put NEW THREADS here
    my $threads_rs     = $c->model('DBIC::Threads')->search;
    $threads_rs        = $threads_rs->search({ 'me.flags' => { '!=', 'deleted' } }) unless $c->can_i('override_thread_deleted');
    $s->{recent_posts} = $threads_rs->search( undef, {
        order_by => 'last_post.time DESC',
        rows => $page_sizes->{forum},
        prefetch => { last_post => 'user' },
    } );

    # NOT FIND_IN_SET breaks here; why?
    $s->{recent_posts}  = $s->{recent_posts}->search({ 'last_post.flags' => { '!=', 'deleted' } }) unless $c->can_i('override_thread_deleted');

    # Grab a list of unread threads
    if ($c->user) {
        my @unread_ids  = $threads_rs->unread_ids($c->user->obj);
        my %unread_hash = map { $_ => 1 } @unread_ids;

        $s->{is_unread} = sub { $unread_hash{$_[0]->id} };
    }
	
    # check for latest subversion revision every 15 minutes or so
    if (not $c->cache->{svn} or $c->cache->{svn}{last_checked} < time - 15 * 60) {
        eval {
            open my $svn_fh, '-|', 'svn', 'log', '--limit' => 1, '--incremental', $c->site_opts->{svn_url};
            my (undef, $details, undef, @log) = <$svn_fh>;
            close $svn_fh;
            my ($rev, $user, $time, undef) = split / \| /, $details;
            $rev =~ s/^r//;
            my ($yr, $mo, $day, $hr, $min, $sec, $tz) = split /[- :]/, $time, 7;

            $c->cache->{svn} = {
                revision     => $rev,
                user         => $user,
                time         => new DateTime(
                    year   => $yr,
                    month  => $mo,
                    day    => $day,
                    hour   => $hr,
                    minute => $min,
                    second => $sec,
                    time_zone => substr $tz, 0, 5,
                ),
                last_checked => time,
                log          => join '', @log,
            }
        };
    }

    $s->{recent_revision} = $c->cache->{svn};

    $s->{recent_users} = $c->model('DBIC::Users')->search(
        { time_active => { '>', time - $c->site_opts->{user_activity_timeout} } },
        { order_by => 'me.time_active DESC' }
    );    

    my $shoutbox_form = Vee::Form->new(
        id => 'shoutbox',
        fields => $shoutbox_fields,
        params => $c->req->params,
    );

    if ($c->user) {
        $shoutbox_form->force( name => $c->user->name );
    }
    $s->{shoutbox_form} = $shoutbox_form;

    $s->{template} = 'index.tt';
}

=head1 AUTHOR

Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>)

See the included F<AUTHORS> file for a full list of contributers.

=head1 LICENSE

See the included F<LICENSE> file.

=cut

1;
