# $Id: //ncom_2_4/stores/farm/lib/BAMM.pm#21 $

package BAMM;
use LWP::UserAgent;
use HTTP::Request::Common;
use FarmAgent;
use strict;

sub link {
	my ($isbn) = shift;
	my ($link) = "http://www.booksamillion.com/ncom/books?pid=" . $isbn;
	return $link;
}

sub name {
	#my ( $pretty ) = shift;
	#return "Books-A-Million" if ( $pretty eq "pretty" );
	return "Booksamillion.com";
}

sub pf_link {
	my ($isbn) = shift;
	my ($link) = 'http://www.pricefarmer.com/cgi-bin/link?site=bamm&isbn=' . $isbn;
	return $link;
}

sub affiliate_link {
	my ($isbn) = shift;
	# my ($link) = 'http://www.commission-junction.com/track/track.dll?' . 
	#	'AID=42121&PID=165082&URL=http%3A%2F%2Fwww%2Ebooksamillion%2Ecom%2F' .
	#	'ncom%2Fbooks%3Fisbn%3D' . $isbn;
	my ( $link ) = 'http://www.qksrv.net/click-790938-42121?isbn=' . $isbn;
	# target="_top">TITLE</a><img src="http://www.qksrv.net/image-790938-42121" width="1" height="1" border="0">
	
	return $link;
}

sub avail {
	my ($days) = shift;
	my ($avail);
	
	if ($days == 3) {
		$avail = "2-3 days";
	} elsif ($days == 1) {
		$avail = "24 hours";
	} elsif ($days == 4) {
		$avail = "3-4 days";
	} elsif ($days == 35) {
		$avail = "3-5 weeks";
	} elsif ($days == 800) {
		$avail = "Advance order";
	} elsif ($days >= 900) {
		$avail = "N/A";
	} else {
		$avail = $days . " days";
	}
	return $avail;
}

sub fetch {
	my ($isbn,$master_list_price,$timeout) = @_;
	my ($link,$page,$list_price,$our_price,$club_price,$days_to_ship,$avail,$cover,$rating);
	my ($our_discount,$club_discount,$image,$title);
	my ($res,$ua);

	# Set a default timeout of 30 secs
	$timeout = $timeout || 30;

	if ($isbn) {
		$link = "http://www.booksamillion.com/ncom/pid?isbn=" . $isbn;
		# getstore($link,'bamm.html');
		# $page = get($link);

		# Get the page
		$ua = LWP::UserAgent->new;

		# Set the timeout
		$ua->timeout($timeout);

		# Set the user agent
		$ua->agent('PriceFarmer.com');

		$res = $ua->request (GET $link);

		# Test for success
		if ($res->is_success) {
			$page = $res->content;
		} else {
			return 0;
		}

		chomp($page);
		
		($isbn,$title,$list_price,$our_price,$club_price,$avail,$image) = split("\t",$page);
		$rating = 0;

		if ($avail =~ /2-3 day/i) {
			$days_to_ship = 3;
		} elsif ($avail =~ /24 hour/i) {
			$days_to_ship = 1;
		} elsif ($avail =~ /3-4 day/i) {
			$days_to_ship = 4;
		} elsif ($avail =~ /7 day/i) {
			$days_to_ship = 7;
		} elsif ($avail =~ /3-5 week/i) {
			$days_to_ship = 35;
		} elsif ($avail =~ /will ship/i) {
			$days_to_ship = 800;
		} else {
			$days_to_ship = 900;
		}

		if ($image =~ /IMAGEYES/i) {
			$cover = "Y";
		} else {
			$cover = "N";
		}

		$our_discount = 0;
		$club_discount = 0;
		if ( $list_price > 0 ) {
			if ( $our_price > 0 ) {
				$our_discount = int(.5 + (100 * (1 - ($our_price / $list_price))));
			}
			if ( $club_price > 0 ) {
				$club_discount = int(.5 + (100 * (1 - ($club_price / $list_price))));
			}
		} elsif ( $list_price eq 'TBA' ) {
			$list_price = $our_price = -1;
			$club_price = 12345.67;  # WARNING!!! This is an arbitrary, large number.
						 # It's not being set to zero because we want it to
						 # sort correctly later on (in PriceFarm->print_all()).
		}
	}
	return ('bamm',$isbn,$list_price,$our_price,$club_price,$our_discount,$club_discount,$days_to_ship,$cover,$rating);
}

1;
