#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Product::DAProduct;

use strict;
use warnings;
use Carp;

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Product;
use RPS::DB::Item::Album;
use RPS::Product::TrackList;
use RPS::Product::PriceList;

use Common::FormObject;
use base 'Common::FormObject';


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

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

	if ($args{_dbItem})
	{
		return $self->_initFromDBItem($args{_dbItem});
	}
	elsif ($args{productID})
	{
		return $self->_initFromDB($args{productID});
	}
	else
	{
		return $self->_initWithDefaults($args{albumID});
	}
}


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

	my $dbItem = RPS::DB::Item::Product->Lookup(product_id => $productID);
	return $self->_initFromDBItem($dbItem);
}


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

    my %properties =
    (
        product_id          => $dbItem->product_id,
        asset_id            => $dbItem->asset_id,
	    product_code        => $dbItem->product_code,
        upc_ean             => $dbItem->upc_ean,
        upc_alt             => $dbItem->upc_alt,
        release_date        => $dbItem->release_date,
        created_date        => $dbItem->date_created,
        modified_date       => $dbItem->date_modified,
        created_by          => $dbItem->created_by,
        modified_by         => $dbItem->modified_by,
        product_type_id     => $dbItem->product_type_id,
        product_price_id    => $dbItem->product_price_id,
        product_status_id   => $dbItem->product_status_id,
        default_price_level_id   => $dbItem->default_price_level_id,
    );


    return $self->_initProperties(\%properties);
}


sub _initWithDefaults
{
	my ($self, $albumID) = @_;

    # !!! This is a little odd - we'll need to set the type correctly.
    # We don't currently display track_products...
    #

    my %defaultProperties =
    (
        asset_id        => $albumID,
        product_type_id => RPS::DB::Item::Product::kProductTypeDigital,
        product_status_id => RPS::DB::Item::Product::kProductStatusActive,
    );

    return $self->_initProperties(\%defaultProperties);
}


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

	$self->{ProductID}          = Common::FormObject::Scalar->new(value => $prop->{product_id}, readOnly => 1);
	$self->{ProductCode}        = Common::FormObject::Scalar::String->new(value => $prop->{product_code});
	$self->{UPC}                = Common::FormObject::Scalar::String->new(value => $prop->{upc_ean}, required => 1, maxlength => 25);
	$self->{UPCAlt}             = Common::FormObject::Scalar::String->new(value => $prop->{upc_alt}, maxlength => 25);
	$self->{ReleaseDate}        = Common::FormObject::Scalar::Date->new(value => $prop->{release_date});

	$self->{CreatedDate}        = Common::FormObject::Scalar::DateTime->new(value => $prop->{date_created}, readOnly => 1);
	$self->{ModifiedDate}       = Common::FormObject::Scalar::DateTime->new(value => $prop->{date_modified}, readOnly => 1);
	$self->{CreatedBy}          = Common::FormObject::Scalar::UserName->new(value => $prop->{created_by}, readOnly => 1);
	$self->{ModifiedBy}         = Common::FormObject::Scalar::UserName->new(value => $prop->{modified_by}, readOnly => 1);

	$self->{ProductTypeID}      = Common::FormObject::Scalar->new(value => $prop->{product_type_id});
	$self->{ProductPriceID}     = Common::FormObject::Scalar->new(value => $prop->{product_price_id});
	$self->{ProductStatusID}    = Common::FormObject::Scalar->new(value => $prop->{product_status_id});
	$self->{DefaultPriceLevelID}= Common::FormObject::Scalar->new(value => $prop->{default_price_level_id});

	$self->{AssetID}            = Common::FormObject::Scalar->new(value => $prop->{asset_id});


    return $self;
}




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

	# If we don't have a ProductID, then this is a _new_ product...
	# ProductID is given to us by the database.
	#
	my $dbObj;
	if ($self->ProductID())
	{
		$dbObj = RPS::DB::Item::Product->Lookup(product_id => $self->ProductID());
	}
	else
	{
		$dbObj = RPS::DB::Item::Product->Create();
	}


	#
	# !!! Some of these fields will require special encoding...
	#
	$dbObj->product_code($self->ProductCode());
	$dbObj->asset_id($albumID);
	$dbObj->upc_ean($self->UPC());
	$dbObj->upc_alt($self->UPCAlt());
	$dbObj->release_date($self->ReleaseDate());
	$dbObj->product_type_id($self->ProductTypeID());
	$dbObj->product_price_id($self->ProductPriceID());
	$dbObj->product_status_id($self->ProductStatusID());
	$dbObj->default_price_level_id($self->DefaultPriceLevelID());


	# Did anything change?  Then save it!
	#
	if ($dbObj->isDirty())
	{
		$dbObj->save();

        $self->_initProperties($dbObj);
	}


}




1;

