/**
 * @fileoverview PriceCalculator module
 * @author Josh Johnston josh@xhtmlized.com
 */

/**
 * PriceCalculator: Calculates the price of an order
 * @namespace
 */
var PriceCalculator = function(){
	var _basePricePerPage = 299;
	var _pricePerPage = 299;
	var _expressMultiplier = 3;

	/*global jQuery */
	var self = /** @scope PriceCalculator */{
		/**
		 * Get price options
		 * @param {hash} factors Factors which affect the price
		 * @config {int} num_pages
		 * @return {hash}
		 * @config {price} express_price
		 * @config {price} standard_price
		 */
		getPriceOptions: function(factors) {
			// if there are more than the maximum pages, only 'dedicated' option is available
			if (factors.num_pages > 5) {
				return {
					dedicated: 'variable'
				};
			}

			// if there are less than the minimum pages, make the number of pages equal to
			// the minimum
			if (factors.num_pages < 2) {
				factors.num_pages = 2;
			}

			// Set price per page based on country selected
			if (config.currency.country_defaults[factors.country]) {
				var cc = config.currency.country_defaults[factors.country].currency_code;
				_pricePerPage = config.currency_info[cc].exchange_rate * _basePricePerPage;
			}
			else {
				_pricePerPage = _basePricePerPage;
			}

			// get prices from the price matrix
			var matrix = self.getPriceMatrix();
			var options = {
				express: matrix.express(factors),
				standard: matrix.standard(factors),
				dedicated: 'variable'
			};

			return options;
		},

		/**
		 * Get a price matrix
		 * @return {hash}
		 */
		getPriceMatrix: function() {
			return {
				standard: function(factors) {
					return self.getPriceComponents('standard', factors);
				},
				express: function(factors) {
					return self.getPriceComponents('express', factors);
				}
			};
		},

		getPriceComponents: function(type, factors) {
			var extax = self.getExtaxAmount(type, factors);
			var discount = extax * self.getDiscountPercent(factors);
			// apply the discount to the ex-tax price. Is this right?
			var extax_with_discount = extax - discount;
			// then apply the tax to the discounted price
			var tax = extax_with_discount * self.getTaxPercent(factors);
			var inctax = extax_with_discount + tax;
			var inctax_no_discount = extax + (extax * self.getTaxPercent(factors));

			return {
				extax: extax,
				discount: Math.ceil(discount),
				extax_with_discount: Math.ceil(extax_with_discount),
				tax: Math.ceil(tax),
				final_price_pre_discount: Math.ceil(inctax_no_discount),
				final_price: Math.ceil(inctax)
			};
		},

		getExtaxAmount: function(type, factors) {
			var base = factors.num_pages * _pricePerPage;
			return type == 'standard'
				? base
				: base * _expressMultiplier - (100 * factors.num_pages);
		},

		/**
		 * Get the discount percent
		 * @param {hash} factors
		 * @return {float}
		 */
		getDiscountPercent: function(factors) {
			return factors.payment_option == 'pay_upfront' ? 0.1 : 0;
		},

		/**
		 * Get the tax percent
		 * @param {hash} factors
		 * @return {float}
		 */
		getTaxPercent: function(factors) {
			return config.currency.country_defaults[factors.country]
				? config.currency.country_defaults[factors.country].tax_percent
				: 0;
		}
	};

	return self;
}();

