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

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

use lib '/app/tools/rps/lib';
use Common::FormObject;
use RPS::Product::Product;
use RPS::Product::ProductDigital;
use RPS::Catalog::AlbumSimple;
use RPS::LabelList;
use RPS::CountryList;
use Data::Dumper;

use base 'Common::FormObject';

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

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

    my $productID = $args{productID};
    assert($productID);
    $self->{Product} = RPS::Product::Product->new( productID => $productID );
    $self->{ProductDigital} = RPS::Product::ProductDigital->new( productID => $self->{Product}->ProductID() );
    $self->{LabelList} = RPS::LabelList->new();

    my $countrySkipList = {};
    my $territories     = $self->{ProductDigital}->{TerritoriesList}->access();
    my @territoryList   = split( /\s+/, $territories );

    foreach my $code (@territoryList) {
        $countrySkipList->{$code} = 1;
        push @{ $self->{ProductDigital}->{TerritoryList}->{Territory} },
          { CountryCode => $code, Name => RPS::CountryList::CountryNameFor($code) };
    }

    $countrySkipList->{XU} = 1;
    $countrySkipList->{EU} = 1;

    $self->{CountryList} = RPS::CountryList->new( omit => $countrySkipList );

    $self->{Album} = RPS::Catalog::AlbumSimple->new( albumID => $self->{Product}->AssetID(), loadSubs => 0 );

    return $self;
}

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

    # The Product (and contained objects) can be edited.
    #
    if ( $propertyName eq 'ProductDigital' ) {
        return 1;
    }
    return 0;
}

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

    # JPK - This is one of those situations where I _don't_ want to
    # validate everything this object contains.  It's a waste of time
    # to validate the <Album>, for example, since we don't support making
    # changes to it.
    #
    my $valid = $self->{ProductDigital}->validate();
    return $valid;
}

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

    return $self->{ProductDigital}->ProductID();
}

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

    return $self->{Album}->AlbumID();
}

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

    my $userID = Common::RSApp::GetActiveUserID();

    # I need a way to simply save the product(s) that have been edited.
    #
    # !!! I may be able to rely on the DB::Item objects keeping track of
    #     their own dirtiness, and simply iterate over everything.
    #
    $self->{ProductDigital}->save();
}

###
1;    #
###
