#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------

#------------------------------------------------------------
# Validation Rule Key Value List Object.  Extends the Validate
# Rule class.  This object contains the method to do validation
# on piece of data.
#
# Determine if a field contains a valid key value pair list.
# keys and values are delimeted by an '=' and pairs by ';' by
# default.
#
# Parameter Requirement:
#
#    The parameter used in config_rule must be a:
#
# Base Class:
#   Validation::Rule
#
# Public methods:
#   + Validate::Rule::Code new();
#   + $fail_status         ok();
#   + $scalar              AUTOLOAD( $scalar );
#------------------------------------------------------------
package Validate::Rule::Constraint::PriceTiersList;
use strict;

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::ServicePriceTier;

use lib '/app/tools/metadata/lib';
use base 'Validate';
use base 'Validate::Rule::Constraint::KeyValueList';

use vars qw($AUTOLOAD);

###
# Define addition public accessors
###
use constant kClassAttributes => qw( message parameter fail_status );

###
# Default failure message if the OK method fails.
###
use constant kFailureMessage => "Must be a valid retailer and valid price tier";

sub ok {
    my $self  = shift;
    my $value = shift;

    my $fail =
      defined( $self->{_fail_status} )
      ? $self->{_fail_status}
      : Validate::kStatusDefault;
    my $success = Validate::kStatusOK;

    return undef unless ( $self->is_configured() );

    return $success unless ($value);

    my $parentStatus = $self->SUPER::ok($value);

    return $parentStatus
      if ( defined($parentStatus) && $parentStatus == $fail );

    my @tiers = $self->_dataAsList($value);

    $self->message(kFailureMessage);
    return $self->_validPriceTiers( \@tiers ) ? $success : $fail;

}

#------------------------------------------------------------
# Private Methods
#------------------------------------------------------------

sub _validPriceTiers {
    my $self  = shift;
    my $pairs = shift;

    my %tierChecked;

    for ( my $i = 0 ; $i < @{$pairs} - 1 ; $i += 2 ) {
        my $service = $pairs->[$i];
        my $tier    = $pairs->[ $i + 1 ];

        return unless ( $service && $tier );

        my $key = lc($service) . "_" . lc($tier);

        if ( !defined( $tierChecked{$key} ) ) {
            $tierChecked{$key} = 1;

            my $record = RPS::DB::Item::ServicePriceTier->LookupByTierName( service_name => $service, tier_name => $tier );

            return unless ($record);
        }
    }

    return 1;
}

#------------------------------------------------------------
# Validate::Rule _init()
# Initialize the object.
#
# - Build accessor method hash
# - set internal private data ( message, fail_status, and
#   parameter ). See the base class for parameter defintions.
#
#------------------------------------------------------------
sub _init {
    my ( $self, %args ) = @_;

    # Build a hash with our valid class attributes
    foreach ( (kClassAttributes) ) {
        $self->{_attributes}->{$_}++;
    }

    $self->message( $args{message}                    ? $args{message}     : kFailureMessage );
    $self->fail_status( defined( $args{fail_status} ) ? $args{fail_status} : Validate::kStatusDefault );
    $self->parameter( $args{parameter} ) if ( $args{parameter} );

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

    return $self;
}
1;
