#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package RPS::Track::Command::Edit;
use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::Message;
use RPS::Track::Form;
use RPS::Track::DAForm;
use RPS::Catalog::Command::Show;
use RPS::DB::Item::MechanicalRun;
use RPS::DB::Item::CAMechanicalRun;

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

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

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

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

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

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

    my $trackID = $self->getParam('TrackID');
    my $albumID = $self->getParam('AlbumID');
    my $form;

    # We want to use different forms depending on what sort of client we are...
    #
    my $client = Common::Client::Current();
    if (   $client->isClientType(Common::Client::kArtist)
        || $client->isClientType(Common::Client::kUSMechanicals)
        || $client->isClientType(Common::Client::kUKMechanicals)
        || $client->isClientType(Common::Client::kCAMechanicals) ) {
        if ($trackID) {
            $form = RPS::Track::Form->new( trackID => $trackID );
        } elsif ($albumID) {
            $form = RPS::Track::Form->new( albumID => $albumID );
        }
    } else {
        if ($trackID) {
            $form = RPS::Track::DAForm->new( trackID => $trackID );
        } elsif ($albumID) {
            $form = RPS::Track::DAForm->new( albumID => $albumID );
        }
    }

    # Do a little prevalidation to show errors
    if ( $self->getParam('show_errors') ) {
        my $harness = new Metadata::Validate::Harness( failState => Validate::kStatusOK, ignoreCurrentData => 1 );
        $form->validate( metadataValidator => $harness );
    }

    if ( $self->getParam('formSubmit') ) {

        # !!! Going to do something a little different here, and go ahead and validate
        # even if the assignCGIParams fails.   This is to avoid 'hiding' errors between
        # submits.
        my $validator = new Metadata::Validate::Harness();

        my $valid = $form->assignCGIParams( 'Form', $self->getParams() );
        if ( !$form->validate( metadataValidator => $validator ) ) {
            $valid = 0;
        }
        if ($valid) {
            $form->save();

            my $command;
            if ( $self->getParam("addAnother") ) {

                # Add another track
                #
                my $successMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
                $command = RPS::Track::Command::Edit->new( AlbumID => $form->albumID(), msg => $successMessage->toString() );
            } else {

                # Redirect to the Show command.
                #
                my $successMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
                my %showParams = (
                    AlbumID => $form->albumID(),
                    msg     => $successMessage->toString(),
                );
                $command = RPS::Catalog::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;
