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

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::BookProductContributor;
use BookPub::DB::Item::Contributor;
use Common::RSApp;
use Common::Util;
use Common::Assert;
use Common::FormObject;
use BookPub::Catalog::Contributor;

use base 'Common::FormObject';

sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    my %properties;
    if ( $args{dbItem} ) {
        $self->_propertiesFromDBItem( \%properties, $args{dbItem} );
    } elsif ( $args{bookProductContributorID} ) {
        $self->_propertiesFromDB( \%properties, $args{bookProductContributorID} );
    } else {
        $self->_propertiesFromDefaults( \%properties );
    }

    $self->_initProperties( \%properties );

    return $self;
}

sub _propertiesFromDB {
    my ( $self, $hrProperties, $id ) = @_;

    my $dbItem = BookPub::DB::Item::BookProductContributor->Lookup( book_product_contributor_id => $id );

    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

sub _propertiesFromDBItem {
    my ( $self, $hrProperties, $dbItem ) = @_;

    $hrProperties->{book_product_contributor_id} = $dbItem->book_product_contributor_id;
    $hrProperties->{product_id}                  = $dbItem->product_id;
    $hrProperties->{contributor_role_id}         = $dbItem->contributor_role_id;
    $hrProperties->{contributor_id}              = $dbItem->contributor_id;
    $hrProperties->{sequence}                    = $dbItem->sequence;
}

sub _initProperties {
    my ( $self, $props ) = @_;

    # Not editable...
    $self->{BookProductContributorID} = $props->{book_product_contributor_id};

    $self->{ProductID}         = Common::FormObject::Scalar->new( value => $props->{product_id} );
    $self->{ContributorRoleID} = Common::FormObject::Scalar->new( value => $props->{contributor_role_id} );
    $self->{ContributorID}     = Common::FormObject::Scalar->new( value => $props->{contributor_id} );
    $self->{Sequence}          = Common::FormObject::Scalar->new( value => $props->{sequence} );

    my $name;
    if ( $props->{contributor_id} ) {
        my $contributor = BookPub::DB::Item::Contributor->Lookup( contributor_id => $props->{contributor_id} );
        $name = $contributor->name_inverted();
    }
    $self->{Name} = Common::FormObject::Scalar::String->new( value => $name );

    # Going to add in a 'placeholder' element, to catch form requests to delete this Contributor.
    #
    $self->{Delete} = Common::FormObject::Scalar::Boolean->new( value => 0 );
}

sub _propertiesFromDefaults {
    my ( $self, $hrProperties ) = @_;
}

sub save {
    my ( $self, %args ) = @_;

    my $dbObj = $self->_saveBookProductContributor(%args);
    $self->_init( _dbItem => $dbObj );
}

# jpk - broke this out into an 'inner' method to make it easier to subclass
#
sub _saveBookProductContributor {
    my ( $self, %args ) = @_;

    if ( !$self->ProductID() ) {
        $self->ProductID( $args{productID} );
    }

    # Going to have to save DB::Item::Contributor objects as well, if we're creating a brand new contributor.
    #
    # !!! So, the trick here is that we use a 'Name' field to capture the contributor name.
    # This is assumed to be last, first notation.
    if ( !$self->ContributorID() ) {
        my $newContributor = BookPub::Catalog::Contributor->new();
        my ( $last, $first ) = split( ',', $self->Name() );
        if ( defined $first ) {
            $first = Common::Util::trimspaces($first);
        }
        $newContributor->LastName($last);
        $newContributor->FirstName($first);
        $newContributor->save();

        $self->ContributorID( $newContributor->ContributorID() );
    }

    my $dbObj;
    if ( $self->BookProductContributorID() ) {
        $dbObj = BookPub::DB::Item::BookProductContributor->Lookup( book_product_contributor_id => $self->BookProductContributorID() );
    } else {
        $dbObj = BookPub::DB::Item::BookProductContributor->Create();
    }

    $dbObj->product_id( $self->ProductID() );
    $dbObj->contributor_role_id( $self->ContributorRoleID() );
    $dbObj->contributor_id( $self->ContributorID() );

    $dbObj->save();

    return $dbObj;
}

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

    # Don't need to really do anything if we don't have an ID yet.
    #
    if ( $self->BookProductContributorID() ) {
        my $dbObj = BookPub::DB::Item::BookProductContributor->Lookup( book_product_contributor_id => $self->BookProductContributorID() );
        $dbObj->delete();
    }
}

###
1;    #
###
