package EPrints::Plugin::Screen::REF::Report;

use EPrints::Plugin::Screen::REF;
@ISA = ( 'EPrints::Plugin::Screen::REF' );

use strict;

sub wishes_to_export { shift->{session}->param( "ajax" ) }
sub export_mimetype { "text/html; charset=utf-8" }
sub export
{
	my( $self ) = @_;

	my $session = $self->{session};
	my $benchmark = $self->{processor}->{benchmark};
	my $uoa = $self->{processor}->{uoa};

	my $part = $session->param( "ajax" );
	my $f = "ajax_$part";

	if( $self->can( $f ) )
	{
		binmode(STDOUT, ":utf8");
		return $self->$f;
	}

	$session->not_found;
}

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

        my $uoaid = $self->{session}->param( "uoa" );
        my $uoa = $self->{session}->dataset( "subject" )->dataobj( $uoaid );

        return $uoa;
}

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

	return 0 unless( $self->{session}->config( 'ref_enabled' ) );

        my $uoa = $self->current_uoa;
        return 0 if !defined $uoa;

        my $user = $self->{processor}->{user};
        return 0 if !defined $user;

        # return 1 if( defined $user && $user->has_role( 'ref/admin' ) );

        foreach my $uoa_role ( @{$user->get_value( 'ref_uoa_role' )||[]} )
        {
                return 1 if( "$uoa_role" eq $uoa->id );
        }

        return 0;
}

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

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

	my $img = $session->current_url( path => "static", "style/images/progress_bar_orange.png" );
	my $progress = $session->make_element( "div",
		id => "progress",
		style => "clear: both; width: 200px; height: 15px; background-image: url($img); background-repeat: no-repeat; background-position: -200px 0px; border: 1px solid #888; border-radius: 10px; text-align: center; line-height: 15px;"
	);
	$progress->appendChild( $session->make_text( "Loading ..." ) );

	return $progress;
}

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

	my $session = $self->{session};
	my $chunk = $session->make_doc_fragment;

	$chunk->appendChild( $self->SUPER::render_links );

	my $url = $session->current_url( host => 1 );
	my $parameters = URI->new;
	$parameters->query_form(
		$self->hidden_bits,
	);
	$parameters = $parameters->query;

	$chunk->appendChild( $self->make_javascript( <<"EOJ" ) );
var REF_Report = Class.create({
	has_problems: 0,
	count: 0,
	progress: null,
	ids: Array(),
	step: 5,
	prefix: '',
	onProblems: function() {},
	onFinish: function() {},

	initialize: function(opts) {
		if( opts.ids )
			this.ids = opts.ids;
		if( opts.step )
			this.step = opts.step;
		if( opts.prefix )
			this.prefix = opts.prefix;
		if( opts.onFinish )
			this.onFinish = opts.onFinish;
		if( opts.onProblems )
			this.onProblems = opts.onProblems;
	},

	execute: function() {
		for(var i = 0; i < this.ids.length; i+=this.step)
		{
			var args = '&ajax='+this.prefix;
			for(var j = 0; j < this.step && i+j < this.ids.length; j++)
				args += '&' + this.prefix + '=' + this.ids[i+j];
			new Ajax.Request('$url', {
				method: 'get',
				parameters: '$parameters'+args,
				onSuccess: (function(transport) {
					var res = transport.responseText.split( '\\n--\\n' );
					res.pop();
					for(var i = 0; i < res.length; i+=3)
					{
						\$(this.prefix + '_' + res[i]).update( res[i+1] );
						if( res[i+2].length )
						{
							this.has_problems = 1;
							\$(this.prefix + '_problems').insert( res[i+2], { position: 'end' } );
						}
					}
					this.count += res.length / 3;
					if( this.count == this.ids.length )
					{
						\$('progress').parentNode.removeChild(\$('progress'));
						if( this.has_problems )
							this.onProblems(this);
						this.onFinish(this);
					}
					else
					{
						var width = 200;
						\$('progress').style.backgroundPosition = Math.round(-width + width * this.count / this.ids.length) + "px 0px";
					}
				}).bind(this)
			});
		}
	}
});
EOJ

	return $chunk;
}

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

	my $benchmark = $self->{processor}->{benchmark};
	
	return $self->{session}->make_doc_fragment unless( defined $benchmark );

	return $benchmark->render_citation;
}

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

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

	my $chunk = $session->make_doc_fragment;

	my @plugins = $self->export_plugins;

	my $form = $chunk->appendChild( $self->render_form );
	$form->setAttribute( method => "get" );
	my $select = $form->appendChild( $session->render_option_list(
		name => 'export',
		values => [map { $_->get_subtype } @plugins],
		labels => {map { $_->get_subtype => $_->get_name } @plugins},
	) );
	$form->appendChild( 
		$session->render_button(
			name => "_action_export",
			class => "ep_form_action_button",
			value => $session->phrase( 'cgi/users/edit_eprint:export' )
	) );
	
	return $chunk;
}

1;
