#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Matcher::Command::Unmatch;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use Common::Assert;
use Common::Log;

use lib '/app/tools/bookpub/lib';
use BookPub::Matcher::Command::Sale;
use BookPub::Tracker::File;
use BookPub::DB::Item::ProductInputMap;
use BookPub::DB::Item::Sale;
use BookPub::Price::Validator;

use base 'BookPub::Command';

#sub cmd  { return "unmatch"; }
#sub area { return "matcher"; }
#
#use constant kTemplate => '/app/tools/bookpub/templates/main/matcher.xsl';

sub _getSaleFromSaleID {
    my ( $self, $saleID ) = @_;
    assert( 0, 'overrride' );
}

sub _getSaleItemsWithMapID {
    my ( $self, $mapID ) = @_;
    assert( 0, 'override' );
}

sub _saleObjFromSaleDBItem {
    my ( $self, $saleDB ) = @_;
    assert( 0, 'override' );
}

sub _fileFromFileID {
    my ( $self, $fileID ) = @_;
    assert( 0, 'override' );
}

sub _removeValidation {
    my ( $self, $saleID ) = @_;
    assert( 0, 'override' );
}

sub _redirectToNextSale {
    my ( $self, $nextSaleID, $pageParam );
    assert( 0, 'override' );
}

sub _redirectToFilePage {
    my ( $self, $fileID, $pageParam );
    assert( 0, 'override' );
}

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 $saleID = $self->getParam('sale');
    die "Error - Missing sale parameter" unless $saleID;

    # We're going to re-visit the 'sale' command no matter what happens.
    #
    #my %cmdArgs;
    #$cmdArgs{sale} = $saleID;
    #$cmdArgs{trackerPage} = $self->getParam('trackerPage') if $self->getParam('trackerPage');
    #my $newCmd = BookPub::Matcher::Command::Sale->new(%cmdArgs);
    my $pageParam = $self->getParam('trackerPage');

    my $sale  = $self->_getSaleFromSaleID($saleID);
    my $mapID = $sale->MapID();

    if ( BookPub::DB::Item::ProductInputMap->CanUnmatch($mapID) ) {

        # Let's keep track of the files we've seen, and update their exception counts at the end of this loop.
        #
        my %fileIDs;

        my $salesToUnmatch = $self->_getSaleItemsWithMapID($mapID);
        while ( my $saleDB = $salesToUnmatch->next() ) {
            my $saleObj = $self->_saleObjFromSaleDBItem($saleDB);

            $saleObj->MapID(undef);
            $saleObj->ProductID(undef);
            $saleObj->ImportStatus(BookPub::DB::Item::Sale::STATUS_NOMATCH);
            $saleObj->save();

            $fileIDs{ $saleObj->FileID } = 1;
        }

        foreach my $fileID ( keys %fileIDs ) {
            my $file = $self->_fileFromFileID($fileID);
            $file->recalculateRemainingExceptions();
            $file->save();
        }

        # Delete the map entry.
        #
        my $productInputMap = BookPub::DB::Item::ProductInputMap->Lookup( map_id => $mapID );
        $productInputMap->delete();

        $self->_removeValidation($saleID);

        # Need to re-initialize the Sale object to get the next ids.
        my $nextSaleID = $sale->NextUnfixedID() || $sale->PreviousUnfixedID();
        if ($nextSaleID) {
            Common::Log::Print( "next sale id", $nextSaleID );

            # redirect
            return $self->_redirectToNextSale( $nextSaleID, $pageParam );
        }
    }

    return $self->_redirectToFilePage( $sale->FileID(), $pageParam );
}

1;
