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

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

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

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

sub cmd  { return 'delete'; }
sub area { return 'license'; }

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

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

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

	my $id = $self->getParam('ProductID');
	my $product;

	if($id)
	{
		$product = RPS::Product::Product->new(productID => $id);
	}
	else
	{
		my $command = RPS::Catalog::Command::Search->new();
		return RSApache::Response::Redirect->new($command->getRedirect());
	}

	if ($product->delete())
	{
		# Redirect to the Show command.
        # We'll have to get the correct album_id.
		#
        my $albumID = $product->AssetID();
        if (RPS::DB::Item::Product::kProductTypeDigitalTrack == $product->ProductTypeID)
        {
            my $trackData = RPS::DB::Item::Track->Lookup(track_id => $albumID);
            $albumID = $trackData->album_id;
        }

		my $successMessage = RPS::Message->new(type => "success", code => Common::FormObject::kSuccessFormSubmit);
		my %showParams = (
			AlbumID => $albumID,
			msg => $successMessage->toString(),
		);
		my $command = RPS::Catalog::Command::Show->new(%showParams);
		return RSApache::Response::Redirect->new($command->getRedirect());
	}
	else
	{
		my $errorMsg = RPS::Message->new(type => "error", code => Common::FormObject::kErrDelete);
		my %showParams = (
			ProductID => $id,
			msg => $errorMsg->toString(),
		);
		my $command = RPS::Product::Command::Edit->new(%showParams);
		return RSApache::Response::Redirect->new($command->getRedirect());
	}

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

	return $response;
}


1;
