#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::ArtistContract::Command::Edit;
use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::ArtistContract::EditForm;
use RPS::ArtistContract::Command::Show;

use RPS::Command;
use RPS::CommandLog;
use base 'RPS::Command';

use lib '/app/tools/common/lib';
use Common::FormObject;
use RSApache::Response::XML;
use RSApache::Response::XSLT;
use RSApache::Response::Redirect;

sub cmd  { return 'edit'; }
sub area { return 'artist_contract'; }

use constant kTemplate => "/app/tools/rps/templates/artist_contract/index.xsl";

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

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

    my $id = $self->getParam('ArtistContractID') || 0;

    # these arguments are only available in contract copy mode
    my $patternArtistContractID         = $self->getParam('PatternArtistContractID')         || 0;
    my $copyContractTerms               = $self->getParam('CopyContractTerms')               || 0;
    my $copyReserveSchedule             = $self->getParam('CopyReserveSchedule')             || 0;
    my $copyLicenseIncomeSources        = $self->getParam('CopyLicenseIncomeSources')        || 0;
    my $copyRecoupableExpenseTypes      = $self->getParam('CopyRecoupableExpenseTypes')      || 0;
    my $copyControlledCompositionClause = $self->getParam('CopyControlledCompositionClause') || 0;

    my $form = RPS::ArtistContract::EditForm->new(
        artistContractID                => $id,
        patternArtistContractID         => $patternArtistContractID,
        copyContractTerms               => $copyContractTerms,
        copyReserveSchedule             => $copyReserveSchedule,
        copyLicenseIncomeSources        => $copyLicenseIncomeSources,
        copyRecoupableExpenseTypes      => $copyRecoupableExpenseTypes,
        copyControlledCompositionClause => $copyControlledCompositionClause,
    );

    if ( $self->getParam('formSubmit') ) {
        if ( $form->assignCGIParams( 'Form', $self->getParams() ) && $form->validate() ) {
            $form->save();
            RPS::CommandLog::Log( $self, $id );

            # We want to change views, so we want to redirect to the Show command now.
            #
            my $successMessage = RSApache::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
            my %showParams = (
                ArtistContractID => $form->contractID(),
                msg              => $successMessage->toString(),
            );
            my $command = RPS::ArtistContract::Command::Show->new(%showParams);
            return RSApache::Response::Redirect->new( $command->getRedirect() );
        } else {
            $form->setError(Common::FormObject::kErrFormSubmit);
            $self->addMessageXML( type => "error", code => Common::FormObject::kErrFormSubmit );
        }
    }

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

    return $response;
}

1;
