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

use lib '/app/tools/rps/lib';
use RPS::Track::TrackContract;
use RPS::Catalog::Command::Show;
use RPS::Command;
use RPS::DB::Item::Track;

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

use base 'RPS::Command';

sub cmd  { return 'detach'; }
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 $trackContractID = $self->getParam('TrackContractID');
    assert($trackContractID);

    my $obj = RPS::Track::TrackContract->new( trackContractID => $trackContractID );

    my $message;
    if ( $obj->delete() ) {
        $message = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
    } else {
        $message = RPS::Message->new( type => "error", code => Common::FormObject::kErrFormSubmit );
    }

    # Going to need to album id to redirect properly
    #
    my $trackData = RPS::DB::Item::Track->Lookup( track_id => $obj->TrackID() );
    my $albumID = $trackData->album_id;

    my %showParams = (
        AlbumID => $albumID,
        msg     => $message->toString(),
        tab     => RPS::Catalog::Command::Show::kTabContracts,
    );
    my $command = RPS::Catalog::Command::Show->new(%showParams);
    return RSApache::Response::Redirect->new( $command->getRedirect() );
}

1;
