# classifier plugin for sorting sections alphabetically

# this is very similar to AZList except it sorts by
# section level metadata (excluding the top level)
# instead of just top level metadata

# the only change is to the classify() subroutine which 
# must now iterate through each section, adding each
# to the classification

package AZSectionList;

use sorttools;

sub new {
    my ($class, @options) = @_;

    if (!defined @options || !scalar @options) {
	die "AZSectionList used with no metadata name to classify by\n";
    }

    return bless {
	'list'=>{},
	'metaname' => $options[0]
	}, $class;
}

sub init {
    my $self = shift (@_);

    $self->{'list'} = {};
}

sub classify {
    my $self = shift (@_);
    my ($doc_obj) = @_;

    my $doc_OID = $doc_obj->get_OID();
    my $thissection = $doc_obj->get_next_section ($doc_obj->get_top_section());

    while (defined $thissection) {
	$self->classify_section ($thissection, $doc_obj);
	$thissection = $doc_obj->get_next_section ($thissection);
    }
}

sub classify_section {
    my $self = shift (@_);
    my ($section, $doc_obj) = @_;

    my $doc_OID = $doc_obj->get_OID();
    my $metavalue = $doc_obj->get_metadata_element ($section, $self->{'metaname'});

    # if this section doesn't contain the metadata element we're
    # sorting by we won't include it in this classification
    if (defined $metavalue && $metavalue =~ /\w/) {
	if ($self->{'metaname'} eq 'Creator') {
	    &sorttools::format_string_name_english (\$metavalue);
	} else {
	    &sorttools::format_string_english (\$metavalue);
	}
	if (defined $self->{'list'}->{"$doc_OID.$section"}) {
	    print STDERR "WARNING: AZSectionList::classify called multiple times for $doc_OID.$section\n";
	} 
	$self->{'list'}->{"$doc_OID.$section"} = $metavalue;
    }
}

sub get_classify_info {
    my $self = shift (@_);

    my @classlist = sort {$self->{'list'}->{$a} cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};

    return $self->splitlist (\@classlist);
}

sub get_entry {
    my $self = shift (@_);
    my ($title, $classifytype) = @_;
    
    # organise into classification structure
    my %classifyinfo = ('classifytype'=>$classifytype,
			'Title'=>$title,
			'contains'=>[]);

    return \%classifyinfo;
}

# splitlist takes an ordered list of classifications (@$classlistref) and splits it
# up into alphabetical sub-sections.
sub splitlist {
    my $self = shift (@_);
    my ($classlistref) = @_;
    my $classhash = {};

    # top level
    my $classifyinfo = $self->get_entry ($self->{'metaname'}, "AZList");

    # don't need to do any splitting if there are less than 20 classifications
    if ((scalar @$classlistref) <= 20) {
	foreach $subOID (@$classlistref) {
	    push (@{$classifyinfo->{'contains'}}, {'OID'=>$subOID});
	}
	return $classifyinfo;
    }
	
    # first split up the list into separate A-Z and 0-9 classifications
    foreach $classification (@$classlistref) {
	my $title = $self->{'list'}->{$classification};
	$title =~ s/^(.).*$/$1/;
	$title =~ tr/[a-z]/[A-Z]/;
	if ($title =~ /^[0-9]$/) {$title = '0-9';}
	elsif ($title !~ /^[A-Z]$/) {
	    print STDERR "AZSectionList: WARNING $classification has badly formatted title ($title)\n";
	}
	$classhash->{$title} = [] unless defined $classhash->{$title};
	push (@{$classhash->{$title}}, $classification);
    }
    $classhash = $self->compactlist ($classhash);

    my @tmparr = ();
    foreach $subsection (sort keys (%$classhash)) {
	push (@tmparr, $subsection);
    }
    
    # if there's a 0-9 section it will have been sorted to the beginning
    # but we want it at the end
    if ($tmparr[0] eq '0-9') {
	shift @tmparr;
	push (@tmparr, '0-9');
    }

    foreach $subclass (@tmparr) {
	my $tempclassify = $self->get_entry($subclass, "AZList");
	foreach $subsubOID (@{$classhash->{$subclass}}) {
	    push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
	}
	push (@{$classifyinfo->{'contains'}}, $tempclassify);
    }

    return $classifyinfo;
}

sub compactlist {
    my $self = shift (@_);
    my ($classhashref) = @_;
    my $compactedhash = {};
    my @currentOIDs = ();
    my $currentfirstletter = "";
    my $currentlastletter = "";
    my $lastkey = "";

    # minimum and maximum documents to be displayed per page.
    # the actual maximum will be max + (min-1).
    # the smallest sub-section is a single letter at present
    # so in this case there may be many times max documents
    # displayed on a page.
    my $min = 10; 
    my $max = 30;

    foreach $subsection (sort keys %$classhashref) {
	if ($subsection eq '0-9') {
	    @{$compactedhash->{$subsection}} = @{$classhashref->{$subsection}};
	    next;
	}
	$currentfirstletter = $subsection if $currentfirstletter eq "";
	if ((scalar (@currentOIDs) < $min) ||
	    ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
	    push (@currentOIDs, @{$classhashref->{$subsection}});
	    $currentlastletter = $subsection;
	} else {

	    if ($currentfirstletter eq $currentlastletter) {
		@{$compactedhash->{$currentfirstletter}} = @currentOIDs;
		$lastkey = $currentfirstletter;
	    } else {
		@{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
		$lastkey = "$currentfirstletter-$currentlastletter";
	    } 
	    if (scalar (@{$classhashref->{$subsection}}) >= $max) {
		$compactedhash->{$subsection} = $classhashref->{$subsection};
		@currentOIDs = ();
		$currentfirstletter = "";
	    } else {
		@currentOIDs = @{$classhashref->{$subsection}};
		$currentfirstletter = $subsection;
		$currentlastletter = $subsection;
	    }
	}
    }

    # add final OIDs to last sub-classification if there aren't many otherwise
    # add final sub-classification
    if (scalar (@currentOIDs) < $min) {
	my ($newkey) = $lastkey =~ /^(.)/;
	@currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
	delete $compactedhash->{$lastkey};
	@{$compactedhash->{"$newkey-$currentlastletter"}} = @currentOIDs;	
    } else {
	if ($currentfirstletter eq $currentlastletter) {
	    @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
	} else {
	    @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
	} 
    }

    return $compactedhash;
}

1;
