package Common::ClientSettings;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::RSApp;
use Common::Assert;
use Common::DB::Item::Client;

# The following are bit fields that represent client types.
#
use constant kDigitalAdvantage    => 1;
use constant kArtist              => 2;
use constant kLabel               => 4;
use constant kDistribution        => 8;
use constant kUSMechanicals       => 16;
use constant kUKMechanicals       => 32;
use constant kCAMechanicals       => 64;
use constant kBookPublishing      => 128;
use constant kSalePriceValidation => 256;
use constant kProductMonitoring   => 512;

# This provides a simple interface to the client table.
# In particular, this will utilize the app's ram cache to avoid
# hammering the db for things that never change...
#
use constant kCachePath => 'Common::ClientSettings';

sub ClientName {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{client_name};
}

sub ClientNameClean {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{client_name_clean};
}

sub TypeMask {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{type_mask};
}

sub IsClientType {
    my ($type) = @_;
    assert( $type, "ERROR - must specify a type mask" );

    my $mask = TypeMask();
    return ( ( $mask & $type ) ? 1 : 0 );
}

# !!! Will need to change this
#
sub ___DAOnly {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{da_only};
}

sub DBName {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{db_name};
}

sub DBHost {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{db_host};
}

sub DBUser {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{db_user};
}

sub DBPass {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{db_pass};
}

sub HFAManufacturerCode {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{hfa_manufacturer_code};
}

sub UseNetForMechanicalDeductions {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{use_net_for_mechanical_deductions};
}

sub SubtractStatRateFromNetRevenue {
    my $cachedSettings = _getCachedSettings();
    return $cachedSettings->{subtract_stat_rate_from_net_revenue};
}

# private methods

sub _getCachedSettings {
    my $cache = Common::RSApp::GetRAMCache();
    if ( !$cache->{kCachePath} ) {
        _loadSettings($cache);
    }

    return $cache->{kCachePath};
}

sub _loadSettings {
    my ($cache) = @_;

    my $clientID = Common::RSApp::GetClientID();

    my @fields = (
        'client_name', 'client_name_clean', 'db_name', 'db_host', 'db_user', 'db_pass', 'hfa_manufacturer_code',
        'use_net_for_mechanical_deductions',
        'subtract_stat_rate_from_net_revenue', 'type_mask',
    );

    my $settingsObj = Common::DB::Item::Client->Lookup( client_id => $clientID );
    if ($settingsObj) {
        foreach my $field (@fields) {
            $cache->{kCachePath}{$field} = $settingsObj->$field();
        }
    }
}

1;
