#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package BookPub::Catalog::EditBookProductForm;

use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/bookpub/lib';
use BookPub::Catalog::Book::FullDisplay;
use BookPub::Catalog::Product::Book::WithFormat;

#use BookPub::Catalog::BookFormatList;
use BookPub::Catalog::ProductFormList;
use BookPub::Catalog::ProductFormDetailList;
use BookPub::Catalog::EPubTypeList;
use BookPub::Catalog::ImprintList;
use BookPub::Catalog::PublisherList;
use BookPub::Catalog::ContributorRoleList::ForBookProduct;
use BookPub::Catalog::LanguageList;

use base 'Common::FormObject';

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

    assert( $args{bookID} || $args{productID} );

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

    my $product;
    my $bookID;
    if ( $args{productID} ) {
        $product = BookPub::Catalog::Product::Book::WithFormat->new( productID => $args{productID} );

        # We have no business being here if there isn't a book product with that product id...
        #
        die( "ERROR - No book_product found with productID " . $args{productID} )
          unless $product;

        $bookID = $product->BookID();
    } else {
        $product = BookPub::Catalog::Product::Book::WithFormat->new();
        $bookID  = $args{bookID};

        # Let the new product know which book it belongs to.
        #
        $product->BookID($bookID);

    }

    my $book = BookPub::Catalog::Book::FullDisplay->new( bookID => $bookID, readOnly => 1 );

    $self->{Product} = $product;
    $self->{Book}    = $book;

    #    $self->{BookFormatList} = BookPub::Catalog::BookFormatList->new();
    $self->{ProductFormList}       = BookPub::Catalog::ProductFormList->new();
    $self->{ProductFormDetailList} = BookPub::Catalog::ProductFormDetailList->new();
    $self->{EPubTypeList}          = BookPub::Catalog::EPubTypeList->new();

    $self->{ImprintList}         = BookPub::Catalog::ImprintList->new();
    $self->{PublisherList}       = BookPub::Catalog::PublisherList->new();
    $self->{ContributorRoleList} = BookPub::Catalog::ContributorRoleList::ForBookProduct->new();
    $self->{LanguageList}        = BookPub::Catalog::LanguageList->new();

    return $self;
}

sub _propertyIsEditable {
    my ( $self, $propertyName ) = @_;

    # The Product (and contained objects) can be edited.

    return 1 if ( $propertyName eq 'Product' );
    return 0;
}

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

    return $self->{Product}->validate(%args);
}

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

    $self->{Product}->save();
}

###
1;    #
###
