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

use strict;
use warnings;
use Carp;

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

use lib '/app/tools/distribution/lib';
use Distribution::DB::Item::ProductDistribution;

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

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

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

    $self->{_service_id} = $args{service_id};
    $self->{_product_id} = $args{product_id};

    if ( $args{_dbItem} ) {
        return $self->_initFromDBItem( $args{_dbItem} );
    } else {
        return $self->_initFromDB(%args);
    }
}

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

    if ( $args{product_distribution_id} ) {
        $dbItem = Distribution::DB::Item::ProductDistribution->Lookup(%args);
    } elsif ( $args{service_id} && $args{product_id} ) {
        $dbItem = Distribution::DB::Item::ProductDistribution->Search(%args);
        return $self->_initProperties( {} ) unless ($dbItem);
    } else {
        return undef;
    }

    return $self->_initFromDBItem($dbItem);
}

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

    my %properties = (
        product_distribution_id => $dbItem->product_distribution_id,
        product_id              => $dbItem->product_id,
        service_id              => $dbItem->service_id,
        distribution_process_id => $dbItem->distribution_process_id,
        third_party             => $dbItem->third_party,
        denied                  => $dbItem->denied,
        invalid                 => $dbItem->invalid,
        delivery_date           => $dbItem->delivery_date,
        created_by              => $dbItem->created_by,
        created_date            => $dbItem->date_created,
        modified_date           => $dbItem->date_modified,

        album_id => $dbItem->{asset_id},
    );

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

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

    $self->{ProductDistribtionID} = Common::FormObject::Scalar->new( value => $prop->{product_distribution_id}, readOnly => 1 );
    $self->{ProductID}            = Common::FormObject::Scalar->new( value => $prop->{product_id},              readOnly => 1 );
    $self->{ServiceID}            = Common::FormObject::Scalar->new( value => $prop->{service_id},              readOnly => 1 );
    $self->{DistribtionProcessID} = Common::FormObject::Scalar->new( value => $prop->{distribution_process_id} );
    $self->{ThirdParty}           = Common::FormObject::Scalar->new( value => $prop->{ThirdParty} );
    $self->{Invalid}              = Common::FormObject::Scalar->new( value => $prop->{invalid} );
    $self->{Denied} = Common::FormObject::Scalar::String->new( value => $prop->{denied} );

    $self->{CreatedBy} = Common::FormObject::Scalar::String->new( value => $prop->{create_by} );

    $self->{DeliveryDate} = Common::FormObject::Scalar::DateTime->new( value => $prop->{delivery_date}, readOnly => 1 );
    $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->{AlbumID} = Common::FormObject::Scalar->new( value => $prop->{album_id}, readOnly => 1 );

    return $self;
}

sub toggleStagedDistribution {
    my $self   = shift;
    my $toggle = shift;

    assert($self);
    assert( defined($toggle) );

    if ( !$toggle ) {
        $self->stageDistribution();
    } else {
        $self->clearDistribution();
    }
}

sub stageDistribution {
    my $self = shift;

    assert($self);
    assert( $self->{_product_id} );
    assert( $self->{_service_id} );

    # If we can't clear out the old record then we should be able to add a new
    # one.
    return undef unless ( $self->clearDistribution() );

    $self->forceStageDistribution();

    return 1;
}

sub forceStageDistribution {
    my $self = shift;

    assert($self);
    assert( $self->{_product_id} );
    assert( $self->{_service_id} );

    my $pd_record = Distribution::DB::Item::ProductDistribution->Create(
        product_id => $self->{_product_id},
        service_id => $self->{_service_id}
    );
    $pd_record->save();

    return 1;
}

sub clearDistribution {
    my $self = shift;
    assert($self);

    #  service_id and product_id are required
    return 1 unless ( $self->ProductID && $self->ServiceID );

    #  If we have already distributed then we can't delete
    return undef if ( $self->DeliveryDate || $self->DistribtionProcessID );

    Distribution::DB::Item::ProductDistribution->ClearStagedDistribution(
        product_id => $self->ProductID,
        service_id => $self->ServiceID
    );

    return 1;
}

sub invalidateDistribution {
    my $self = shift;

    assert($self);
    assert( $self->{_product_id} );
    assert( $self->{_service_id} );

    my $pd_record = Distribution::DB::Item::ProductDistribution->Lookup(
        product_id => $self->{_product_id},
        service_id => $self->{_service_id},
        invalid    => 0
    );

    if ($pd_record) {
        $pd_record->invalid(1);
        $pd_record->save();
    }

    return 1;
}

1;
