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

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

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{productID} ) {
        $self->_propertiesFromDB( \%properties, $args{productID} );
    } else {
        $self->_propertiesFromDefaults( \%properties );
    }

    $self->_initProperties( \%properties );

    return $self;
}

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

    my $dbItem = BookPub::DB::Item::Product->Lookup( product_id => $productID );

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

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

    $hrProperties->{product_id}      = $dbItem->product_id;
    $hrProperties->{product_type_id} = $dbItem->product_type_id;
    $hrProperties->{release_date}    = $dbItem->release_date;
}

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

    # Not editable...
    #
    $self->{ProductID} = $props->{product_id};

    # !!! Going to comment this out for now, until we figure out what
    # status even means...
    #
    #    $self->{Status} = Common::FormObject::Scalar::Enum->new
    #    (
    #        value => $props->{status},
    #        validValues =>
    #        [
    #            BookPub::DB::Item::Product::kStatusActive(),
    #            BookPub::DB::Item::Product::kStatusInactive(),
    #        ],
    #    );

    $self->{ProductTypeID} = Common::FormObject::Scalar->new( value => $props->{product_type_id} );
    $self->{ReleaseDate} = Common::FormObject::Scalar::Date->new( value => $props->{release_date} );
}

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

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

    my $dbObj = $self->_saveProduct();

    # JPK - Usually, I will re-initialize using the dbItem mechanism.
    # But in the case of this Product class, it's better to use the ID, because
    # we're actually dealing with more than one table.
    #
    $self->_init( productID => $dbObj->product_id );
}

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

    my $dbObj;
    if ( $self->ProductID() ) {
        $dbObj = BookPub::DB::Item::Product->Lookup( product_id => $self->ProductID() );
    } else {
        $dbObj = BookPub::DB::Item::Product->Create();
    }

    $dbObj->product_type_id( $self->ProductTypeID() );
    $dbObj->release_date( $self->ReleaseDate() );

    $dbObj->save();

    return $dbObj;

}

###
1;    #
###
