package Memories;
use strict;
our $VERSION = "1.2";
use Maypole::Application qw(Upload Authentication::UserSessionCookie -Debug);
use HTML::TagCloud;
use URI;
use Memories::Config;
use Memories::DBI;
use Memories::Photo;
use Memories::Comment;
use Memories::Tag;
use Memories::SystemTag;
use Memories::User;
use Memories::Album;
use URI::Escape;
use Calendar::Simple;
use XML::RSS;
use Tagtools;

Memories->config->auth->{ user_field } = "name";
Memories->config->model("Maypole::Model::CDBI::Plain");
Memories->setup([qw/ Memories::Photo Memories::User Memories::Tag
Memories::Album Memories::SystemTag/]);

sub message {
    my ($self, $message) = @_;
    push @{$self->{template_args}{messages}}, $message;
}

sub check_credentials {
    my $r = shift;
    my ($uid, $user) = $r->SUPER::check_credentials;
    #if (!$uid) { return (-1, undef) }
    return ($uid, $user);
}

sub do_rss {
    my $r = shift;
    $r->model_class->process($r);
    my $photos = $r->get_photos;
    my $rss = XML::RSS->new(version => "2.0");
    $rss->channel(
        title => ($r->config->{application_name}. " : ".ucfirst($r->action)." ".ucfirst($r->table)." ".($r->objects||[])->[0]) ,
        link  => $r->config->{uri_base}."/".$r->path
    );
    for my $item (@$photos) { 
        my $link = $r->config->{uri_base}."photo/view/".$item->id;
        $rss->add_item( title => $item->title, link => $link,
            description => 
    "<a href=\"$link\">
    <img src=\"". $item->thumb_url."\" alt=\"".$item->title."\"></a>",
            dc => { subject => join " ", $item->tags },
            pubDate => $item->uploaded->strftime("%a, %d %b %Y %H:%M:%S %z")
        )
    }
    $r->output($rss->as_string);
    $r->content_type("application/rss+xml");
    return
}

sub get_photos {
    my $r = shift;
    my $maybe_photos = $r->{objects}||[];
    return (@$maybe_photos && $maybe_photos->[0]->isa("Memories::Photo")) 
            ? $maybe_photos :
            ($r->{template_args}->{photos} || []);
}

sub last_search {
    my $r = shift;
    my $photos = $r->get_photos; 
    $r->{session}{last_search} = join ",", map { $_->id } @$photos 
        if @$photos > 1;
}

sub additional_data { 
    my $r = shift;
    if ($r->params->{view_cal}) { 
    $r->{template_args}{view_cal} = eval {
            Time::Piece->strptime($r->{params}{view_cal}, "%Y-%m-%d") }; 
    }
    $r->{template_args}{now} = Time::Piece->new;
    if ($r->session) {
        (tied %{$r->session})->{lock_manager}->clean('/var/lib/memories/sessionlock', 3600) #remove files older than 1 hour
    }
    return $r->do_rss if ($r->params->{format} =~ /rss/)
}

use Maypole::Constants;
sub authenticate {
   my ($self, $r) = @_;
   return DECLINED if $self->path =~/static|store/; # XXX
   $r->get_user;
   return OK; 
}

for my $how (qw(random recent interesting popular)) {
    no strict;
    my $method = "search_$how";
    *{"_$how"} = sub { Memories::Photo->$method };
    *{$how} = sub { shift->do_cached(\&{"_$how"})};
}

sub tag_select {
    my ($r, $tags) = @_;
    my %counter;
    my @photos = Memories::Photo->sth_to_objects(Memories::Tag->multi_search(@$tags));
    for (map {$_->tags} @photos) { 
        $counter{$_->name}++; 
    } 
    delete $counter{$_->name} for @$tags;
    my @super;

    my $cloud = HTML::TagCloud->new();
    my $base = $r->config->uri_base.$r->path."/";
    my $tags;
    for my $name (sort {$a cmp $b} keys %counter) {
        if ($counter{$name} == @photos) {
            push @super, $name;
        } else {
            $cloud->add($name, $base.uri_escape($name), $counter{$name});
            $tags++;
        }
    }
    my %res;
    if (@super) { $res{super} = \@super }
    if ($tags) { $res{cloud} = $cloud }
    \%res;
}
1;
