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

use lib '/app/tools/rps/lib';
use RPS::Track::TrackList;

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

use RPS::Catalog::Command::Search;
use RPS::Catalog::Command::Show;
use RPS::Command;
use RPS::CommandLog;
use base 'RPS::Command';

sub cmd { return 'copy'; }

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

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

    my $trackID        = $self->getParam('TrackID');
    my $trackLicenseID = $self->getParam('TrackLicenseID');
    my $mainForm;
    my $trackIDError;
    my $validateError;

    if ($trackLicenseID) {
        $mainForm = $self->_form()->new( trackLicenseID => $trackLicenseID );
    } else {
        my $command = RPS::Catalog::Command::Search->new();
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    }

    if ( $self->getParam('formSubmit') ) {
        if ( !$trackID ) {
            print STDERR "Copy: submitted, but without trackID, flag error\n";
            $trackIDError = 1;
            $self->{xml}{Form} = $mainForm;
        }

        if ( $mainForm->assignCGIParams( 'Form', $self->getParams() ) && !$mainForm->validate() ) {
            print STDERR "Copy: submitted, but main form validate failed\n";
            $validateError = 1;
            $self->{xml}{Form} = $mainForm;
        }

        if ( !$trackIDError && !$validateError ) {
            my $tracksAref = ( ref($trackID) eq 'ARRAY' ) ? $trackID : [$trackID];
            my $success = 1;
            my $albumID;

            foreach my $id (@$tracksAref) {
                my $tmpForm = $self->_form()->new(
                    trackID                 => $id,
                    licenseType             => $self->getParam('LicenseType'),
                    controlledCompositionID => $self->getParam('ControlledCompositionID')
                );

                if ( $tmpForm->assignCGIParams( 'Form', $self->getParams() ) && $tmpForm->validate() ) {
                    $tmpForm->save();
                    $albumID = $tmpForm->Track()->AlbumID();
                } else {
                    print STDERR "Copy: (foreach track) form validate failed\n";
                    $validateError     = 1;
                    $self->{xml}{Form} = $mainForm;
                    $success           = 0;
                    last;
                }
            }

            if ($success) {
                RPS::CommandLog::Log( $self, $trackLicenseID );

                # We want to change views, so we want to redirect to the Show command now.
                #
                my $successMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
                my $showParams = $self->getPassThroughParams();
                $showParams->{AlbumID} = $albumID, $showParams->{msg} = $successMessage->toString();
                $showParams->{tab} = $self->_tab();
                my $command = RPS::Catalog::Command::Show->new(%$showParams);
                return RSApache::Response::Redirect->new( $command->getRedirect() );
            }
        }
    } else {
        if ($trackLicenseID) {
            print STDERR "Copy: got trackLicenseID, display normal form\n";
            $self->{xml}{Form} = $mainForm;
        } else {
            my $command = RPS::Catalog::Command::Search->new();
            return RSApache::Response::Redirect->new( $command->getRedirect() );
        }
    }

    $self->{xml}{TrackList} = RPS::Track::TrackList->new( albumID => $mainForm->Track()->AlbumID(), loadSubs => 0 );

    # check for an error
    #
    if ( $trackIDError || $validateError ) {
        if ($trackIDError) {
            $self->{xml}{TrackList}->setError(Common::FormObject::kErrFieldBlank);
        }

        if ($validateError) {
            $self->{xml}{Form}->setError(Common::FormObject::kErrFormSubmit);
        }
        $self->addMessageXML( type => "error", code => Common::FormObject::kErrFormSubmit );
    }

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

    return $response;
}

1;
