package RPS::Portal::Command::Resend;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use RSApache::Response::Download;
use Common::Assert;
use Common::Debug;
use Common::DB::Item::PayeeType;

use lib '/app/tools/rps/lib';
use RPS::Portal::Command::Main;
use RPS::Portal::SendInvite;
use RPS::ArtistPayee::Command::Show;
use RPS::Publisher::Command::US::Show;
use RPS::Publisher::Command::CA::Show;

use base 'RPS::Command';

sub area { return "portal"; }
sub cmd  { return "resend"; }

use constant kTemplate => '/app/tools/rps/templates/portal/main.xsl';


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

    # Give any inherited methods the first crack at this.
    # (This checks for login and basic access permissions).
    #
    my $response = $self->SUPER::execute();
    return $response if $response;

    my $resend           = $self->getParam('resend')           || '';    # | payee | recipient
    my $payeeID          = $self->getParam('payeeID')          || 0;
    my $payeeType        = $self->getParam('payeeType')        || 0;
    my $payeeRecipientID = $self->getParam('payeeRecipientID') || 0;

    my $message;

    # resendAll will check to see if any unconfirmed payees have been sent invites in the past 24 hours.
    # If they have, it will just return 0 (and we'll display an error message).
    # If they haven't it will queue up a job to resend the invite to all of them.
    if ($resend) {
        my $resending;

        if ($payeeID) {
            # if payeeRecipientID is greater than zero, we are going to resend a invitation to an additional recipient
            $resending = RPS::Portal::SendInvite->_sendInvite(
                payee_id => $payeeID,
                payee_type => $payeeType,
                statement_recipient_id => $payeeRecipientID
            );
        } else {
            $resending = RPS::Portal::SendInvite->resendAll($payeeType, $resend);
        }

        if ( !$resending ) {
            # too soon
            $message = RPS::Message->new( type => "error", code => Common::FormObject::kErrFormSubmit );
        } else {
            # success
            $message = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
        }
    }

    my $command;
    if ($payeeID) {
        if ( $payeeType == Common::DB::Item::PayeeType::kPayeeTypeArtist ) {
            $command = RPS::ArtistPayee::Command::Show->new(
                msg => $message->toString,
                ArtistPayeeID => $payeeID
            );
        } elsif ( $payeeType == Common::DB::Item::PayeeType::kPayeeTypePublisherUS ) {
            $command = RPS::Publisher::Command::US::Show->new(
                msg => $message->toString,
                PublisherID => $payeeID
            );
        } elsif ( $payeeType == Common::DB::Item::PayeeType::kPayeeTypePublisherCA ) {
            $command = RPS::Publisher::Command::CA::Show->new(
                msg => $message->toString,
                PublisherID => $payeeID
            );
        }
    } else {
        $command = RPS::Portal::Command::Main->new( msg => $message->toString, );
    }

    return RSApache::Response::Redirect->new( $command->getRedirect() );
}


1;
