package RPS::ArtistContract::Command::Delete;

use strict;
use warnings;

use RPS::Command;
use RPS::CommandLog;
use parent 'RPS::Command';

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

use RPS::ArtistContract::Command::Main;
use RPS::DB::Item::NewArtistContract;
use RPS::DB::Item::AlbumContract;
use RPS::DB::Item::TrackContract;


# this action is available only from the "Edit" form
sub cmd  { return 'edit'; }
sub area { return 'artist_contract'; }

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

sub execute {
    my $self = shift;

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

    my $status;
    my $id = $self->getParam('ArtistContractID') || 0;
    if ( $id && $id =~ /^\d+$/ && $self->_isActionAllowed) {
        if ( $self->_isArtistContract($id) ) {
            # a contract has albums or tracks or license income attached
            if ( $self->_hasArtistContractItems($id) ) {
                return $self->_renderEditArtistContractsForm( $id, Common::FormObject::kErrArtistContractHasItems );
            }
            # delete a contract
            if ( !$self->_deleteArtistContract($id) ) {
                return $self->_renderEditArtistContractsForm( $id, Common::FormObject::kErrDelete );
            }

            $status = 'success';
        }
    }

    return $self->_redirectToMainContractsPage($status);
}

sub _isActionAllowed {
    my $self = shift;

    my $accessLevel = $self->_getUser()->AccessLevel() || 0;
    # see RSD-4000
    return $accessLevel =~ /^(?:1|3|4|8)$/ ? 1 : 0;
}

sub _renderEditArtistContractsForm {
    my ($self, $id, $errorMessage) = @_;

    my $oForm = RPS::ArtistContract::EditForm->new( artistContractID => $id );
    $oForm->setError($errorMessage);
    $self->addMessageXML( type => "error", code => $errorMessage );
    $self->{xml}{Form} = $oForm;

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

sub _redirectToMainContractsPage {
    my ($self, $status) = @_;

    my %showParams;
    if ( $status && $status =~ /^success$/ ) {
        my $oSuccessMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessDelete );
        %showParams = ( msg => $oSuccessMessage->toString() );
    }

    my $oCommand = RPS::ArtistContract::Command::Main->new( %showParams );
    return RSApache::Response::Redirect->new( $oCommand->getRedirect() );
}

sub _hasArtistContractItems {
    my ($self, $id) = @_;

    my $oAlbumContracts = RPS::DB::Item::AlbumContract->GetByContractID($id);
    my $oTrackContracts = RPS::DB::Item::TrackContract->GetByContractID($id);
    my $oLicenseIncome  = RPS::DB::Item::LicenseIncome->GetByContractID($id);

    return $oAlbumContracts->size || $oTrackContracts->size || $oLicenseIncome->size;
}

sub _isArtistContract {
    my ($self, $id) = @_;
    return RPS::DB::Item::NewArtistContract->GetCountByArtistContractID($id);
}

sub _deleteArtistContract {
    my ($self, $id) = @_;
    return RPS::DB::Item::NewArtistContract->Delete($id);
}

1;
