#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Support::ProductDistribution::Command::AjaxMarkDistribution;
use strict;
use warnings;

use lib '/app/tools/support/lib';
use base 'Support::Command::Submit';

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::ProductDigital;
use RPS::Product::ProductDigitalWithServices;
use RPS::Product::Product;

use lib '/app/tools/support/lib';
use Support::Service::DistributionServiceList;

use Data::Dumper;

sub cmd  { return 'ajax_mark_distribute'; }
sub area { return 'cm'; }

use constant kTemplate => "/app/tools/support/templates/ajax.xsl";

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

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

    $self->deliver();

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

sub deliver {
    my $self = shift;

    Common::Log::Debug("Deliver / Redeliver Products");

    my $d = $self->getParam('deliver');
    my $r = $self->getParam('redeliver');

    my @deliver   = ref($d) ? @$d : ($d);
    my @redeliver = ref($r) ? @$r : ($r);

    my %deliverHash;
    my %redeliverHash;

    foreach (@deliver)   { $deliverHash{$_}   = 1 if ($_) }
    foreach (@redeliver) { $redeliverHash{$_} = 1 if ($_) }

    my $dbProducts = RPS::DB::Item::ProductDigital->GetAll();
    my $dbProduct;
    my $dbProductDigital;
    my $product;
    my $product_digital;

    while ( $dbProducts->hasNext() ) {
        $dbProductDigital = $dbProducts->next();
        $product_digital = RPS::Product::ProductDigitalWithServices->new( productID => $dbProductDigital->product_id );

        # Do nothing unless the product is cleared for distribution
        next unless ( $product_digital->DistAllow() );

        foreach my $service ( $product_digital->Services() ) {
            Common::Log::Debug( "Checking Service: " . $service->ServiceID );

            # Only delivery to services that we're configured to use.
            next unless ( $service->Configured );

            Common::Log::Debug(" -- Service is configured, now check if we need to do anything");

            print Dumper $service;

            # If the product has been delivered to a service and we requested a redelivery OR
            # the product hasn't been delivered and we requested a delivery
            if (   ( $service->{Staged} && $redeliverHash{ $service->ServiceID } )
                || ( !$service->{Staged} && $deliverHash{ $service->ServiceID } ) ) {

                my $productService = new RPS::Product::ProductDistribution(
                    product_id => $dbProductDigital->product_id,
                    service_id => $service->ServiceID
                );

                $productService->invalidateDistribution()
                  if ( $service->{Staged} && $redeliverHash{ $service->ServiceID } );

                $productService->forceStageDistribution();

                $productService = undef;
            }
        }

    }

}

1;
