package Newslash::Web::Controller::Timeline;
use Mojo::Base 'Newslash::Web::Controller';
use Mojo::Util qw(dumper);

use DateTime;
use DateTime::Format::MySQL;
use Newslash::Util::Items;

use constant CACHE_SEC => 30;

sub _make_navitem {
    my ($self, $item) = @_;
    return if !$item;

    # consider timezone
    my $user = $self->stash('user');
    my $offset_sec = $user->{config}->{ui}->{offset_sec} || 0;

    my $unit = "day";
    $unit = "month" if $item->{content_type} eq "poll";
    my $date = Newslash::Util::Items->get_date_string_from_item($item, $offset_sec, $unit);

    my $id;
    if ($item->{content_type} eq "story") {
        $id = $item->{stoid};
    }
    elsif ($item->{content_type} eq "journal") {
        $id = $item->{id};
    }

    return {
            date => $date,
            id => $id,
            type => $item->{content_type},
           };
}

sub _get_config {
    my ($self, $key, $default) = @_;
    my $config = $self->app->config->{Timeline} || {};
    return $config->{$key} || $default;
}

sub _render_timeline {
    my $self = shift;
    my $params = {@_};

    my $limit = $params->{limit};
    my $items = $params->{items};

    # check if next page exists
    my $prev;
    if (@$items > $limit) {
        my $prev_item = pop @$items;
        $prev = $self->_make_navitem($prev_item);
    }

    my $page = { type => "timeline",
                 content_type => $params->{content_type},
                };

    $self->render("timeline/base",
                  items => $items,
                  prev => $prev,
                  page => $page,
                 );
}

sub stories {
    my $c = shift;
    my $user = $c->stash('user');
    my $limit = $c->_get_config("item_per_page", 10);

    my $show_future = $user->{is_admin} ? 1 : 0;
    my $show_nonpublic = $user->{is_admin} ? 1 : 0;

    my $stories;
    if ($user->{is_login}) {
        $stories = $c->model('stories');
    }
    else {
        $stories = $c->ccache->model('stories');
    }
    my $items = $stories->select(hide_future => !$user->{is_admin},
                                 public_only => !$user->{is_admin},
                                 limit => $limit + 1,
                                 order_by => {create_time => 'desc'},
                                );

    $c->_render_timeline(items => $items,
                         content_type => "story",
                         limit => $limit);
}

sub recent {
    my $c = shift;
    my $user = $c->stash('user');
    my $limit = $c->_get_config("item_per_page", 10);

    my $timeline;
    if ($user->{is_login}) {
        $timeline = $c->model('timeline');
    }
    else {
        $timeline = $c->ccache->model('timeline');
    }
    my $items = $timeline->select(limit => $limit);

    $c->_render_timeline(items => $items,
                         content_type => "recent",
                         limit => $limit,
                        );
};

sub popular {
    my $c = shift;
    my $user = $c->stash('user');
    my $popular_period = $c->_get_config("popular_period");
    my $limit = $c->_get_config("item_per_page", 10);

    if (!$popular_period || ref($popular_period) ne "HASH") {
        $popular_period = {days => 3};
    }

    for my $k (keys %$popular_period) {
        if ($k !~ /^(minutes|hours|days|weeks|months|years)$/) {
            delete $popular_period->{$k};
        }
    }

    # fallback
    if (keys %$popular_period == 0) {
        $popular_period = {days => 3};
    }

    my $timeline;
    if ($user->{is_login}) {
        $timeline = $c->model('timeline');
    }
    else {
        $timeline = $c->ccache->model('timeline');
    }
    my $items = $timeline->select(limit => $limit,
                                  order_by => {popularity => "DESC"},
                                  %$popular_period);

    $c->_render_timeline(items => $items,
                         content_type => "popular",
                         limit => $limit,
                        );
}

sub comments {
    my $c = shift;
    my $user = $c->stash('user');
    my $limit = $c->_get_config("item_per_page", 10);

    my $comments;
    if ($user->{is_login}) {
        $comments = $c->model('comments');
    }
    else {
        $comments = $c->ccache->model('comments');
    }
    my $items = $comments->select(limit => $limit + 1,
                                  order_by => {create_time => 'DESC'});

    $c->_render_timeline(items => $items,
                         limit => $limit,
                         content_type => "comment",
                        );
}


sub journals {
    my $c = shift;
    my $user = $c->stash('user');
    my $limit = $c->_get_config("item_per_page", 10);

    my $journals;
    if ($user->{is_login}) {
        $journals = $c->model('journals');
    }
    else {
        $journals = $c->ccache->model('journals');
    }
    my $items = $journals->select(limit => $limit + 1,
                                  order_by => {create_time => 'DESC'});

    $c->_render_timeline(items => $items,
                         limit => $limit,
                         content_type => "journal",
                        );
}

sub submissions {
    my $c = shift;
    my $user = $c->stash('user');
    my $limit = $c->_get_config("item_per_page", 10);

    my $submissions;
    if ($user->{is_login}) {
        $submissions = $c->model('submissions');
    }
    else {
        $submissions = $c->ccache->model('submissions');
    }
    my $items = $submissions->select(limit => $limit + 1,
                                     order_by => {create_time => 'DESC'});

    $c->_render_timeline(items => $items,
                         limit => $limit,
                         content_type => "submission",
                        );
}

sub polls {
    my $c = shift;
    my $user = $c->stash('user');
    my $limit = $c->_get_config("item_per_page", 10);

    my $polls;
    if ($user->{is_login}) {
        $polls = $c->model('polls');
    }
    else {
        $polls = $c->ccache->model('polls');
    }
    my $items = $polls->select(limit => $limit + 1,
                               order_by => {create_time => 'DESC'});

    $c->_render_timeline(items => $items,
                         limit => $limit,
                         content_type => "poll",
                        );
}

1;

