#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::Catalog::Author;
use strict;
use warnings;

use Data::Dumper;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use lib '/app/tools/mediaserve/lib';
use Common::Util qw(clean word_containment);
use Common::Assert;

use BookPub::Import::Importer;

use base 'Common::FormObject';

use overload( '""' => 'asString' );

sub new {
    my $class = shift;
    my $self = bless {}, $class;

    return $self->_init(@_);
}

sub _init {
    my $self = shift;
    my %args;
    my $author = $self->_parseArgs(@_);

    $self->{Author} = $author;

    return $self;
}

sub _parseArgs {
    my $self = shift;
    my %args;

    if ( @_ == 1 ) {
        return $_[0];
    } else {
        %args = @_;
        return defined( $args{parse} ) ? $self->_parseAuthor( $args{parse} ) : $args{author};
    }

    die "Should never get here";
}

sub _parseTitle {
    my $self = shift;
    return shift;
}

sub asString { shift->Author }

sub Clean {
    my $author = shift->Author || return;
    return Common::Util::clean($author);
}

sub exactMatch {
    my $self   = shift;
    my $author = $self->_parseArgs(@_);

    assert( $self->Author );

    # Do we have a title match?
    return unless ($author);
    return $self->Author eq $author;
}

sub fuzzyMatch {
    my $self   = shift;
    my $target = ref($self)->new(@_);

    return 1 if ( $self->exactMatch(@_) );

    ## Insert Fuzzy Logic Here
    return $self->contains(@_) ? 1 : undef;
}

sub contains {
    my $self   = shift;
    my $target = ref($self)->new(@_);

    my $rvalue = $target->Clean();
    my $lvalue = $self->Clean();

    return unless ( defined($rvalue) && defined($lvalue) );

    $rvalue =~ s/_+/ /g;
    $lvalue =~ s/_+/ /g;

    return word_containment( $rvalue, $lvalue );
}

###
1;    #
###
