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

use strict;
use warnings;

use Data::Dumper;

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

use lib '/app/tools/rps/lib';

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

    $self->_initProperties(\%properties);

    return $self;
}

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

	my $dbItem;

    if( $args{servicePriceTierID} ) {
        $dbItem = Distribution::DB::Item::ServicePriceTier->Lookup(service_price_tier_id => $args{servicePriceTierID});
    } elsif( $args{serviceID} && $args{productID} ) {
        $dbItem = Distribution::DB::Item::ServicePriceTier->Lookup(service_id => $args{serviceID}, product_id => $args{productID});
    } elsif( $args{serviceID} && $args{default} ) {
        $dbItem = Distribution::DB::Item::ServicePriceTier->Lookup(service_id => $args{serviceID}, default => 1);
    }

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

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

    return unless($dbItem);

	$hrProperties->{service_price_tier_id} = $dbItem->service_price_tier_id;
	$hrProperties->{service_id}            = $dbItem->service_id;
	$hrProperties->{tier_name}             = $dbItem->tier_name;
	$hrProperties->{tier_code}             = $dbItem->tier_code;
	$hrProperties->{default_tier}          = $dbItem->default_tier;
	$hrProperties->{display_order}         = $dbItem->display_order;
}

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

	$self->{ServicePriceTierID} = Common::FormObject::Scalar->new(value => $props->{service_price_tier_id}, readOnly => 1);
	$self->{ServiceID}          = Common::FormObject::Scalar->new(value => $props->{service_id}, readOnly => 1);
	$self->{TierName}           = Common::FormObject::Scalar->new(value => $props->{tier_name}, readOnly => 1);
	$self->{TierCode}           = Common::FormObject::Scalar->new(value => $props->{tier_code}, readOnly => 1);
	$self->{DefaultTier}        = Common::FormObject::Scalar->new(value => $props->{default_tier}, readOnly => 1);
	$self->{DisplayOrder}       = Common::FormObject::Scalar->new(value => $props->{display_order}, readOnly => 1);
}

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

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


    # Do nothing if we haven't provided a new price tier id
    return unless( defined($args{service_price_tier_id}) );

    # Clear out the pricing tier if price tier 0 is provided.
    unless( $args{service_price_tier_id} ) {
        Common::Log::Debug( "Clearing Price Tier, CODE: $args{service_price_tier_id}" );
        return $self->clearPricingTier( %args );
    }

    assert($self);
    assert($args{service_id});
    assert($args{product_id});
    assert($args{service_price_tier_id});

    unless( $self->ServicePriceTierID ) {
        Common::Log::Debug( "Service Price Tier not found" );
        return undef;
    }

    if( $self->ServiceID != $args{service_id} ) {
        Common::Log::Debug( "Service ID Price Tier Mismatch" );
        return undef;
    }

    my $p_tier = Distribution::DB::Item::ProductServicePricing->Search( %args );

    unless( $p_tier ) {
        Common::Log::Debug( "Create new Price Tier, CODE: $args{service_price_tier_id}" );
        $p_tier = Distribution::DB::Item::ProductServicePricing->Create();
        $p_tier->service_id( $args{service_id} );
        $p_tier->product_id( $args{product_id} );
        $p_tier->save();
    }

    Common::Log::Debug( "Setting Price Tier, CODE: " . $self->ServicePriceTierID . ", Service: $args{service_id}, Product: $args{product_id}" );
    Distribution::DB::Item::ProductServicePricing->UpdateTier( %args );

    $p_tier->save();

    return 1;
}

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

    assert($self);
    assert($args{service_id});
    assert($args{product_id});

    Distribution::DB::Item::ProductServicePricing->ClearPricingTier( %args );
}

1;
