#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Support::Service::BookPubServiceList;

use strict;
use warnings;
use Carp;

use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::ServiceSetting;

use lib '/app/tools/support/lib';
use Support::Service::BookPubService;

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

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

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

    my $serviceID = $args{serviceID};

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

    my $service;
    $self->{Service} = [];

    my @serviceIDs =
      $serviceID
      ? ($serviceID)
      : BookPub::Tracker::Service->GetAllServiceIDs();

    foreach my $id (@serviceIDs) {
        my $serviceObj = Support::Service::BookPubService->new( serviceID => $id );
        push( @{ $self->{Service} }, $serviceObj );
    }

    return $self;
}

sub Services {
    my $self = shift;
    return @{ $self->{Service} };
}

sub assignCGIParams {
    my $self   = shift;
    my $tag    = shift;
    my $params = shift;

    my $settings = {};

    foreach my $key ( keys(%$params) ) {
        if ( $key =~ /^(\w+)_(\d+)$/ ) {
            $settings->{$2}->{$1} = 1;
        }
    }

    $self->{_cgiSettings} = $settings;
}

sub save {
    my $self     = shift;
    my $settings = $self->{_cgiSettings};

    return unless ( defined($settings) );

    my @serviceIDs = BookPub::Tracker::Service->GetAllServiceIDs();

    foreach my $id (@serviceIDs) {
        my $priceValidation = $settings->{$id}->{ValidatePrice};
        my $disableAgency   = $settings->{$id}->{DisableAgencyPricing};
        my $disableRRP      = $settings->{$id}->{DisableRRPPricing};

        BookPub::DB::Item::ServiceSetting->UpdateSetting(
            serviceID     => $id,
            validatePrice => $priceValidation,
            disableAgency => $disableAgency,
            disableRRP    => $disableRRP
        );
    }
}

1;
