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

use strict;
use warnings;

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

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

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

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

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

    $self->{ServiceCategory} = [];

    # Check if the client has any SoundExchange sales. If so then we'll populate
    # the ServiceCategory hash, otherwise it'll be left empty and the template
    # code will suppress the display of the service category widget since that
    # only applies to clients with SoundExchange sales.
    #
    my $options = RPS::DB::Item::ClientOptions->Lookup( name => 'has_soundexchange' );
    if ( $options ) {
        if( $options->value && '1' eq $options->value ) {
            my $serviceCollection = Common::DB::Item::ServiceCategory->GetAll();
            while ( $serviceCollection->hasNext() ) {
                my $serviceObj = $serviceCollection->next();
                push @{ $self->{ServiceCategory} }, RPS::ServiceCategory->new( _dbItem => $serviceObj );
            }
        }
    }

    return $self;
}

sub validate {
    my ($self) = @_;
    # Nothing to validate here
    return 1;
}

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

    # Get the set of excluded categories and save them as a comma-delimited
    # list in the client_options table.
    my @categories;
    foreach my $serviceCategory ( @{ $self->{ServiceCategory} } ) {
        if( $serviceCategory->ExcludeArtist() == 1 ) {
            push @categories, $serviceCategory->CategoryCode();
        }
    }
    my $excludeCategories = join( ",", @categories );

    my $options = RPS::DB::Item::ClientOptions->Lookup( name => 'artist_category_service' );
    if( !$options ) {
        $options = RPS::DB::Item::ClientOptions->Create( name => 'artist_category_service' );
    }
    $options->value($excludeCategories);
    $options->save();
}

sub _listProperties {
    return { ServiceCategory => 1 };
}

sub _propertyIsEditable {
    my ( $self, $propertyName ) = @_;

    if ( $propertyName eq 'ServiceCategory' ) {
        return 1;
    }
    return 0;
}

sub ServiceCategory {
    my ( $self, $index, $methodAddressList, $newValue ) = @_;

    if ( 0 == scalar @$methodAddressList ) {
        if ( defined $newValue ) {
            $self->{ServiceCategory}[$index] = $newValue;
        }
        return 1;
    }

    # May need to dynamically add objects to the list...
    #
    if ( !defined $self->{ServiceCategory}[$index] ) {
        # something bad is happening here
        assert(0);
    }

    return $self->{ServiceCategory}[$index]->_accessProperty( $methodAddressList, $newValue );
}

sub getList {
    my ($self) = @_;

    return $self->{ServiceCategory};
}

1;
