#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::MapFace::Command::Undo;

use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::DB::Item::MapFaceFile;
use RSApache::Response::XSLT;
use Common::RSApp;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::MapFaceTempMapping;
use RPS::DB::Item::MapFaceTempMappingHistory;
use RPS::DB::Item::SaleImportError;
use RPS::MapFace::Command::Map;

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

use constant kTemplate => '/app/tools/rps/templates/mapface/map.xsl';

sub cmd  { return "undo"; }
sub area { return "mapface"; }

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 $mapID = $self->getParam('mapID');

	my $mapping = RPS::DB::Item::MapFaceTempMapping->Lookup(mapface_temp_mapping_id => $mapID);
	my $fileID = $mapping->file_id;
	my $groupNum = $mapping->group_num;	
	
	# Let's make sure we can undo this first.
	if ($mapping->status() == RPS::DB::Item::MapFaceTempMapping::STATUS_MAPPED ||
	    $mapping->status() == RPS::DB::Item::MapFaceTempMapping::STATUS_APPROVED)
	{
	    my $clientID = Common::RSApp::GetClientID();
    	my $mapFaceFiles = Common::DB::Item::MapFaceFile->GetByClientAndFileID($clientID,$fileID);
    	my $mapFaceFile;
    	while ($mapFaceFiles->hasNext() ) {	
    	    $mapFaceFile = $mapFaceFiles->next();
    	}	    
    	
    	if ($mapFaceFile->status == Common::DB::Item::MapFaceFile::STATUS_NEW || 
    	    $mapFaceFile->status == Common::DB::Item::MapFaceFile::STATUS_REVIEW)
    	{
	        # Ok, we're good to go.  Let's do this.
        	$mapping->status(RPS::DB::Item::MapFaceTempMapping::STATUS_UNDONE);
        	$mapping->save();      
        	
        	# Also need to update the remaining_errors field in the mapface_file table in RSCOMMON.
        	my $remainingCount = RPS::DB::Item::MapFaceTempMapping->GetRemainingErrorCountByFileID($fileID);
        	my $approvedCount = RPS::DB::Item::MapFaceTempMapping->GetApprovedCountByFileID($fileID);
    
    	    $mapFaceFile->approved_mappings($approvedCount);
    		$mapFaceFile->remaining_errors($remainingCount);		
    		$mapFaceFile->save();	    	
        	
        	# And add the event to this history.
        	my $userID = Common::RSApp::GetActiveUserID();
        	
        	my $newHistoryAction = RPS::DB::Item::MapFaceTempMappingHistory->Create(
        		mapface_temp_mapping_id => $mapID,
        		user_id => $userID,
        		action  => RPS::DB::Item::MapFaceTempMappingHistory::kActionUndone,
        	);
        	
        	$newHistoryAction->save();	
        }
    }

	my %mapParams = (
		file => $fileID,
		mappingGroup => $groupNum,
	);
	my $command = RPS::MapFace::Command::Map->new(%mapParams);
	return RSApache::Response::Redirect->new($command->getRedirect());
}


1;
