package services::Controller::feedrolls;

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

use DateTime::Format::W3CDTF;
use XML::Atom;
use XML::Atom::Feed;
use XML::Atom::Entry;


=head1 NAME

services::Controller::feedrolls - Catalyst Controller

=head1 DESCRIPTION

This is a controller to let you access and create feedrolls, which are lists of
syndicated feeds from sites external to talentspace

=head1 METHODS

The following methods are used by this controller.

=head2 begin

Do all the stuff that needs to happen each time this controller is called

=cut

sub begin :Private
{
	my ($self,$c) = @_;
	
	my $xml_atom_object	:Stashed = $self->feed;
}
 

=head2 feedrolls

This is the root action which connects to the feedroll model.  It is the first
link in a chain that can have two endpoints, one for a list of all feeds and
another for a controller to a feed.

=cut

sub feedrolls :Chained('/') CaptureArgs(0)
{
	my ($self, $c ) = @_;
	
	my $feedrolls	: Stashed = $c->model('db::feedrolls::feedrolls');
}


=head2 feedrolls_all

An endpoint to list of all available feedrolls contained by the query params. I
break it out this way to make it easy to add other lists with extra selectors
on the query.  This is quite efficient since $feedrolls only performs the SQL
when you ask for data.

=cut

sub feedrolls_all :Chained('feedrolls') PathPart('') Args(0)
{
	my ($self, $c) = @_;

	my $service 		:Stash;		
	my $feedrolls 		:Stash;
	my $xml_atom_object	:Stash;
	
		$xml_atom_object->id($c->uri_for($c->action, $c->request->captures));
		
		$xml_atom_object->title('Feedrolls');
		$xml_atom_object->subtitle('Lists of external feeds.');
		$xml_atom_object->updated($feedrolls->last_updated);
		
		$xml_atom_object->add_link({
		
			rel		=> 'self',
			type	=> 'application/xml+atom',
			href	=> $c->uri_for($c->action, $c->request->captures),
		});
	
	while( my $feedroll = $feedrolls->next )
	{
		my $link	= $feedroll->link( $c->uri_for($c->action, $c->request->captures) );
		
		my $entry	= $self->entry;
		
			$entry->id( $link );
			$entry->title($feedroll->title);
			$entry->summary($feedroll->summary);
			$entry->updated($feedroll->updated);
		
			$entry->add_link({
		
				rel		=> 'edit',
				type	=> 'application/xml+atom',
				href	=> $link,
			});
		
		$xml_atom_object->add_entry($entry);
	}
}

	
=head2 feedroll

Get the data associated with the given feedroll_id and make sure it exists.

=cut

sub feedroll :Chained('feedrolls') PathPart('') CaptureArgs(1)
{
	my ($self, $c, $feedroll_id) = @_;
	
	my $feedrolls  :Stash;
	my $feedroll   :Stashed	= $feedrolls->find({feedroll_id => $feedroll_id});
	
	unless( $feedroll )
	{
	    $c->log->debug("Couldn't find the specified feed_id of $feedroll_id");
		$c->response->status($c->response->RC_NOT_FOUND);
		
		## Break out of the chain, don't continue.
		return 0;
	}
}


=head2 feedroll_all

If we are still at the root of the feedroll ('/feedrolls/[feedroll_id] that must
mean the user wants all the feeds in the given feedroll.  This is an endpoint in
the chain.

=cut

sub feedroll_all :Chained('feedroll') PathPart('') Args(0)
{
	my ($self, $c) = @_;
	
	my $feedroll  :Stash;
	my $xml_atom_object :Stash;
	
	my $feeds  = $feedroll->feeds;
	
	$xml_atom_object->id($c->uri_for($c->action, @{$c->request->arguments}));
	$xml_atom_object->title($feedroll->title);
	$xml_atom_object->subtitle($feedroll->summary);
	$xml_atom_object->updated($feedroll->updated);
	
	$xml_atom_object->add_link({
	
		rel		=> 'self',
		type	=> 'application/xml+atom',
		href	=> $c->uri_for($c->action, @{$c->request->arguments}),
	});
	
	while( my $feed = $feeds->next )
	{
		my $link	= $feed->link( $c->uri_for($c->action, @{$c->request->arguments}) );
		my $entry	= $self->entry;
	
		$entry->id( $link );
		$entry->title($feed->title);
		$entry->summary($feed->summary);
		$entry->updated($feed->updated);
		
		$entry->add_link({
	
			rel		=> 'edit',
			type	=> 'application/xml+atom',
			href	=> $link,
		});  
	
		$xml_atom_object->add_entry($entry);
	}
}


=head1 Utility Methods

The following methods will eventually be refactored into a common module

=cut

sub feed
{
	my $self	= shift @_;
	my $params	= shift @_;
	
	my $feed	= XML::Atom::Feed->new(Version=>'1.0');
	
	return $feed;
}

sub entry
{
	my $self = shift @_;
	
	return XML::Atom::Entry->new(Version=>'1.0');
}


=head1 AUTHOR

John Napiorkowski

=head1 LICENSE

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

=cut

1;
