#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::RDAS::Process;

use strict;

use Time::HiRes;
use Data::Dumper;

use lib '/app/tools/common/lib';
use base 'BookPub::Script';

use lib '/app/tools/bookpub/lib';
use BookPub::RDAS::Service;
use BookPub::RDAS::Request::ProductPrice;
use BookPub::Catalog::Product::Book;
use BookPub::DB::Item::BookProduct;

sub _options {
    {
        client_id => {
            short       => 'c',
            required    => 1,
            description => 'Limit to this client id',
            parameter   => 'i'
        },
        service_id => {
            short       => 's',
            description => 'Limit to this service id',
            parameter   => 'i'
        },
        product_id => {
            short       => 'p',
            description => 'Limit to this product id',
            parameter   => 'i'
        },
        count => {
            short       => 'n',
            description => 'Only fetch/generate n urls',
            parameter   => 'i'
        },
        speed => {
            short       => 'g',
            description => 'Number of urls to fetch/generate per minute',
            parameter   => 'i'
        },
        service_list => {
            short       => 'y',
            description => 'List all monitored services',
        }
    };
}

sub run {
    my $self = shift;
    $self->param('service_list') ? $self->_displayServices() : $self->SUPER::run(@_);
}

sub _process {
    my $self = shift;

    my @ids = $self->_getServiceIDs();

    foreach my $id (@ids) {
        $self->_processService($id);
    }
}

sub _processService { die "must be overloaded" }

sub _getServiceIDs {
    my $self = shift;

    if ( $self->param('service_id') ) {
        if ( $self->_isValidServiceID( $self->param('service_id') ) ) {
            return ( $self->param('service_id') );
        } else {
            die "Service ID is not a RDAS service";
        }
    } else {
        return sort { $a <=> $b } BookPub::RDAS::Service->GetAllServiceIDs();
    }
}

sub _isValidServiceID {
    my $self = shift;
    my $serviceID = shift || return;

    for my $id ( BookPub::RDAS::Service->GetAllServiceIDs() ) {
        return 1 if ( $id == $serviceID );
    }

    return;
}

sub _calculateSleep {
    my $self = shift;
    my $sleep = $self->param('speed') || return;

    Log->info(" - Calculating sleep time for $sleep records per minute");
    return if ( $sleep == 0 );

    return 60 / $sleep;
}

sub _displayServices {
    my $self = shift;
    my %services;

    foreach my $serviceID ( BookPub::RDAS::Service->GetAllServiceIDs() ) {
        my $name = BookPub::Tracker::Service::GetDefaultServiceName( service_id => $serviceID );
        die "Unknown service, ID: $serviceID" unless ($name);

        $services{$name} = $serviceID;
    }

    foreach my $service ( sort keys %services ) {
        printf( "%-20s(ID: %d)\n", $service, $services{$service} );
    }

}

1;
