#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Support::ServiceAccount::Command::Show;
use strict;
use warnings;

use lib '/app/tools/support/lib';
use base 'Support::Command::Submit';

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;

use lib '/app/tools/support/lib';
use Support::ServiceAccount::ServiceAccount;
use Support::Service::DistributionServiceList;

sub cmd  { return 'service_account_detail'; }
sub area { return 'cm'; }

use constant kTemplate => "/app/tools/support/templates/cm/service_account_detail.xsl";

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

    my $response = $self->SUPER::execute();
    return $response if $response;

    my $clientID = $self->getParam('ClientID');

    my $form =
      new Support::ServiceAccount::ServiceAccountSimple( clientID => $clientID, serviceAccountID => $self->getParam('ServiceAccountID') );
    $form->{ServiceCredentials}   = new Support::ServiceCredential::ServiceCredentialList();
    $form->{DistributionServices} = new Support::Service::DistributionServiceList();

    $self->saveOnSubmit($form);

    $self->{xml}{Form} = $form;
    $response = RSApache::Response::XSLT->new( $self->{xml}, kTemplate );
    return $response;
}

sub saveOnSubmit {
    my $self = shift;
    my $form = shift;

    my $clientID = $self->getParam('ClientID');

    # If we have a client ID then do the mapping as well
    if ( $self->SUPER::saveOnSubmit($form) && $clientID ) {
        Common::Log::Debug(
            "Linking Service account to client, (Client ID: " . $clientID . ", Service Account ID: " . $form->ServiceAccountID . " )" );
        my $client = new Support::Client::Client( clientID => $clientID );
        $client->assignServiceAccount( serviceID => $form->ServiceID, serviceAccountID => $form->ServiceAccountID );
    }
}

1;
