package EPrints::Plugin::MePrints::Widget::UserGallery;

use EPrints::Plugin::MePrints::Widget;
@ISA = ( 'EPrints::Plugin::MePrints::Widget' );

use strict;

sub new
{
	
	my( $class, %params ) = @_;
	
	my $self = $class->SUPER::new( %params );
	
	if ( !$self->{session} )
	{
		$self->{session} = $self->{processor}->{session};
	}

	$self->{name} = "EPrints Profile System: User Gallery Widget";
	$self->{visible} = "all";
	$self->{advertise} = 1;

	$self->{surround} = 'Simple';
	$self->{render_title} = 0;
	$self->{max_display} = 10;
	
	return $self;
}

sub render_content
{
	my( $self ) = @_;

	my $session = $self->{session};
	my $user = $self->{user};

	my $frag = $session->make_doc_fragment;
	my $size = 'preview';
	my $max = 7;
	my $docs = get_user_documents( $session, $max, $size, $user );

	if ( scalar @$docs )
	{
		my $div = $session->make_element( "div", id => "slideshow" );
		$frag->appendChild( $div );
		my $div_cnt = $session->make_element( "div", id => "slideshow_cnt" );
		$div->appendChild( $div_cnt );
		my $div_idx = $session->make_element( "div", id => "slideshow_idx", style => "display: none" );
		$div->appendChild( $div_idx );

		my $idx_ul = $session->make_element( "ul" );
		$div_idx->appendChild( $idx_ul );

		my $first = 1;
		foreach my $doc (@$docs)
		{
			my $eprint = $doc->get_eprint;

			# index icon
			my $li = $session->make_element( "li",
				class => "icon",
				title => $eprint->value( "title" ),
			);
			$idx_ul->appendChild( $li );
			$li->appendChild( $session->make_element( "img",
				src => $doc->icon_url( size => "small" )
				) );

			# slide
			my $div = $session->make_element( "div", class => "slide" );
			$div_cnt->appendChild( $div );
			if( $first )
			{
				$first = 0;
				$div->setAttribute( style => "display: block" );
			}
			else
			{
				$div->setAttribute( style => "display: none" );
				$li->setAttribute( style => "opacity: .5; filter: alpha(opacity=50); -moz-opacity: .5; -khtml-opacity: .5;" );
			}

			my $abs_link = $session->render_link( $doc->get_eprint->get_url );
			$div->appendChild( $abs_link );
			my $img = $session->make_element( "img",
				src => $doc->thumbnail_url( $size )
			);
			$abs_link->appendChild( $img );
			$div->appendChild( $session->make_element( "br" ) );

			my $caption = $eprint->render_citation_link( "default" );
			$div->appendChild( $caption );
		}
	}
	else
	{
		$frag->appendChild( $self->html_phrase( "noitems" ) );
	}

	return $frag;
}

sub get_user_documents
{
	my( $session, $max, $size, $user ) = @_;

	my $docs = [];

	my $users_name = $user->get_value("name");
	my $creators_name = $users_name->{family}.", ".$users_name->{given};

	my $ds = $session->get_repository->get_dataset( "archive" );
	
        my $searchexp = new EPrints::Search(
                session=>$session,
		satisfy_all => 0,
                dataset=>$ds );

	$searchexp->add_field( $ds->field( "creators_name" ), $creators_name, "EQ" ) if $ds->has_field( "creators_name" );
	$searchexp->add_field( $ds->field( "contributors_name" ), $creators_name, "EQ" ) if $ds->has_field( "contributors_name" );
	$searchexp->add_field( $ds->field( "editors_name" ), $creators_name, "EQ" ) if $ds->has_field( "editors_name" );
	$searchexp->add_field( $ds->field( "exhibitors_name" ), $creators_name, "EQ" ) if $ds->has_field( "exhibitors_name" );
	$searchexp->add_field( $ds->field( "curators_name" ), $creators_name, "EQ" ) if $ds->has_field( "curators_name" );
	$searchexp->add_field( $ds->field( "publication_creators_name" ), $creators_name, "EQ" ) if $ds->has_field( "publication_creators_name" );
	$searchexp->add_field( $ds->field( "publication_editors_name" ), $creators_name, "EQ" ) if $ds->has_field( "publication_editors_name" );

        my $list = $searchexp->perform_search;

	my $ids = $list->ids;
	my @rand_list = List::Util::shuffle(@$ids);
	foreach my $id (@rand_list)
	{
		if ($max > 0)
		{
			my $eprint = $ds->dataobj( $id );
			my @eprint_docs = $eprint->get_all_documents;
			DOCS: foreach my $doc (@eprint_docs)
			{
				next unless defined $doc;
				next unless $doc->thumbnail_url( $size );
				push @$docs, $doc;
				$max--;
				last DOCS;
			}
		}
	}

	$list->dispose;

	return $docs;
}



1;
