#!/usr/bin/perl

# $Id: //ncom_2_4/stores/farm/lib/CB.pm#4 $

package CB;
use LWP::UserAgent;
use HTTP::Request::Common;
use lib "/ncom/lib";
use Utils;
use lib "/ncom/stores/farm/lib";
use FarmAgent;
use strict;

sub link {
	my ($isbn) = shift;
	my ($link) = "http://www.christianbook.com/Christian/Books/product/?isbn=" . $isbn;
	return $link;
}

sub name {
	return "Christianbook.com";
}

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

sub affiliate_link {
	my ($isbn) = shift;
	my ($link) = &link( $isbn );
	return $link;
}

sub avail {
	my ($days) = shift;
	my $avail;

	if ($days == 1) {
		$avail = "24-48 hours";
	} elsif ($days == 800) {
		$avail = "Advance order";
	} elsif ($days == 900) {
		$avail = "N/A";
	} elsif ($days == 1000) {
		$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);
	my ($our_discount,$club_discount);
	my ($res,$ua);

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

	if ($isbn) {
		$link = &link($isbn);

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

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

		# Set the user agent
		$ua->agent(FarmAgent::agent());

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

		# Test for success
		if ($res->is_success) {
			$page = $res->content;
		} elsif ( $res->content =~ /We're\s*?sorry/i ) {		# regex for file not found
			return ('cb',$isbn,0,0,0,0,0,1000,'N');
		} else {
			return 0;
		}

		if ( $page =~ /Retail Price:.*?\$([0-9.,]+)/ ) {
			$list_price = $1;
			$list_price =~ s/\,//g;
		} else {
			$list_price = 0;
		}

		if ( $page =~ /CBD Price:.*?\$([0-9.,]+)/ ) {
			$our_price = $1;
			$our_price =~ s/\,//g;
			$club_price = $our_price;
		} else {
			$our_price = $club_price = 0;
		}

		if ( $page =~ /Availability:\s*<\/B>(.*?)\<\// ) {
			$avail = $1;
		} else {
			$avail = "";
		}

		if ( $page =~ /We're\ssorry/i ) {			# Error page ==> NA
			$days_to_ship = 1000;
		} elsif ($avail =~ /In\sStock/i) {
			$days_to_ship = 1;
		
		######
		# Christianbook.com displays all their backorders and advances int he same way.
		# They say "Available to ship on or about MM/DD/YY."  I'm simply calculating
		#  how many days away that is.  There's no way to detect Advance orders.
		#####
		
		} elsif ($page =~ m#Available\sto\sship\son\sor\sabout\s(\d\d)/(\d\d)/(\d\d)#i) {	
			my($avail_mon) = $1;
			my($avail_day) = $2;
			my($avail_yr) = 2000 + $3;
			my ($current_day, $current_mon, $current_yr ) = (localtime) [3, 4, 5];
			$current_mon += 1;
			$current_yr += 1900;
# Debug
#print "Today: $current_mon\/$current_day\/$current_yr.\n";
#print "Available: $avail_mon\/$avail_day\/$avail_yr.\n";
			my ($current_time) = Utils::str_to_unixtime( "$current_yr-$current_mon-$current_day" );
			my ($avail_time) = Utils::str_to_unixtime( "$avail_yr-$avail_mon-$avail_day" );
			if ( $avail_time < $current_time ) {
				$days_to_ship = 900;
			} else {
				$days_to_ship = int ( ( $avail_time - $current_time ) / 86400 );
			}
		} else {
			$days_to_ship = 900;
		}

		$page =~ m/Stock\sNumber:.*?WW(\w)(\w*?)</i;
		my ($dir) = $1;
		my ($pid) = $dir . $2;
		
		if ($page =~ m#graphics\.christianbook\.com/Graphics/product/$dir/$pid\.gif#i) {
			$cover = "Y";
		} else {
			$cover = "N";
		}
	
		$our_discount = 0;
		$club_discount = 0;

		# BAMM has a list for this item -- use it
		if (($master_list_price > 0) && ($our_price > 0)) {
			$our_discount = int(.5 + (100 * (1 - ($our_price / $master_list_price))));
		} elsif (($list_price > 0) && ($our_price > 0)) {
			$our_discount = int(.5 + (100 * (1 - ($our_price / $list_price))));
		}

		$club_discount = $our_discount;
	}

	return ('cb',$isbn,$list_price,$our_price,$club_price,$our_discount,$club_discount,$days_to_ship,$cover);		
}

1;
