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

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::Chapter::WithAuthor;
use BookPub::Catalog::Product::Chapter;
use BookPub::Catalog::BookFormatList;

use base 'Common::FormObject';

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

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

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

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

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

        $chapterID = $product->ChapterID();
    } else {
        $product   = BookPub::Catalog::Product::Chapter->new();
        $chapterID = $args{chapterID};

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

    }
    my $chapter = BookPub::Catalog::Chapter->new( chapterID => $chapterID, readOnly => 1 );

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

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

    $self->{BookFormatList} = BookPub::Catalog::BookFormatList->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;    #
###
