=head1 NAME

OpenDirectory::MatchResult -- list of categories matching
some criteria.

=cut





package OpenDirectory::MatchResult;


use strict;

use Data::Dumper;





=head1 PROPERTIES

=head2 raCategory

Array ref with OpenDirectory::Category objects.

=cut
sub raCategory { my $self = shift; my $pkg = ref($self);
	my ($val) = @_;

	if(defined($val)) {
		$self->{raCategory} = $val;
		}

	return($self->{raCategory});
	}





=head1 METHODS

=head2 new()

Create new object.

=cut
sub new { my $pkg = shift; $pkg = ref($pkg) || $pkg;

	my $self = {
		};
	bless $self, $pkg;
	$self->raCategory([]);

	return($self);
	}





=head2 add($objCategory)

Add the OpenDirectory::Category $objCategory to raCategory().
Return 1 on success, else 0.

=cut
sub add { my $self = shift; my $pkg = ref($self);
	my ($objCategory) = @_;

	#Add
	my $raCategory = $self->raCategory();
	push(@$raCategory, $objCategory);

	return(1);
	}





=head2 sort()

Sort raCategory(), best matches first.

Return 1 on success, else 0.

=cut
sub sort { my $self = shift; my $pkg = ref($self);
	my $raCategory = $self->raCategory();

	#Sort and set
	$self->raCategory([
			sort { $b->scoreMatch() <=> $a->scoreMatch() }
					@$raCategory
			]);

	return(1);
	}





=head2 raCategoryHierarchy()

Return array ref (containing hash refs) with the contents of
raCategory() in a hierarchical manner. A couple of hash refs
may look something like this:

	{	#First main category
		objCategory => $objCat1,
		text => "/Computers/",
		scoreTotal => 340,			#Total for subcategories
		raSubcategory => [
			{
				objCategory => $objCat2,
				text => "Software/Operating_Systems/Windows/Device_Drivers/",
			},
			{
				objCategory => $objCat3,
				text => "Hardware/Standards/TWAIN/",
			},
			],
	},
	{	#Second main category
		objCategory => $objCat4,
		text => "/Arts/",
		scoreTotal => 262,
		raSubcategory => [
			{
				objCategory => $objCat5,
				text => "Photography/Cameras/",
			},
			{
				objCategory => $objCat6,
				text => "Photography/Techniques_and_Styles/Digital/Resources/",
			},
			{
				objCategory => $objCat7,
				text => "Movies/Titles/T/Tombstone/",
			},
			],
	},

=cut
sub raCategoryHierarchy { my $self = shift; my $pkg = ref($self);


	#Check initial order, longest common topic
	my $noCatMax = 10;		##todo: property!
	my $noCat = 0;
	my $longest = "";
	my @aCatMain;
	for my $objCat (@{$self->raCategory()}) {
		last if($noCat++ >= $noCatMax);
		my $cat = $objCat->category();
print "CAT: ($cat)\n";
		if(!$longest) {
			$longest = $cat;
			}
		else {
			$longest = $self->longestCommonString($longest, $cat);
			}
		
		push(@aCatMain, $objCat);
		}
		
	#It's gotta be a complete topic
	$longest =~ s{(/[^/]+?)$}{/};
print "LONGEST: ($longest)\n";

	#Figure out main topics
	my $regexpLongest = quotemeta($longest);
	my $rhCatMain = {};
	for my $objCat (@aCatMain) {
		my $main = $objCat->category();
		$main =~ s{($regexpLongest[^/]*?/).*$}{$1};
		
		$rhCatMain->{$main}->{objCategory}->{ $objCat->category() } = $objCat;
		$rhCatMain->{$main}->{hit} += $objCat->scoreMatch();
		}

##continue: Build the data structure

print Dumper($rhCatMain);

	return(1);
	}





=head2 longestCommonString($first, $second)

Return the longest common string shared by $first and 
$second, starting from the left.

=cut
sub longestCommonString { my $self = shift; my $pkg = ref($self);
	my ($first, $second) = @_;

	#Shortest string
	my $len = length($first) < length($second) ? length($first) : length($second);

	while(length($first) && $len >= 0 && $first ne $second) {
		$first = substr($first, 0, $len);
		$second = substr($second, 0, $len);
		$len--;
		}

	return($first);
	}





=head1 AUTHOR

Johan Lindström - johanl@bahnhof.se

Copyright (c) 2002.. Johan Lindström. All rights reserved.
This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.

=cut





1;





#EOF
