package org.ofbiz.accounting.tax;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.order.shoppingcart.ShoppingCart;

public class IncludedAdjustmentsHelper {
	GenericDelegator delegator;
	String orderId;
	List orderTaxAdjustments;
	List itemTaxAdjustments;
	/**
	 * @param delegator
	 * @throws GenericEntityException 
	 */
	public IncludedAdjustmentsHelper(GenericDelegator delegator, String orderId, ShoppingCart cart) throws Exception {
		this.delegator = delegator;
		this.orderId = orderId;
		if(cart == null){
			if(orderId == null){
				throw new Exception("Invalid order");
			}
			orderTaxAdjustments = this.getOrderTaxAdjustmentsFromDB();
			itemTaxAdjustments = this.getItemTaxAdjustmentsFromDB();
		}
		else{
			orderTaxAdjustments = cart.getOrderTaxAdjustments();
			itemTaxAdjustments = cart.getItemTaxAdjustments();
		}
	}
	
	public List getOrderTaxAdjustments(){
    	return this.orderTaxAdjustments;
    }
    
    public List getItemTaxAdjustments(){
    	return this.itemTaxAdjustments;
    }
	
    private List getOrderTaxAdjustmentsFromDB() throws GenericEntityException{
    	List taxAdjustments = delegator.findByAnd("OrderIncludedAdjustment", UtilMisc.toMap("orderId", this.orderId, "orderAdjustmentTypeId", "INCLUDED_TAX"));
    	return taxAdjustments;
    }
    
    private List getItemTaxAdjustmentsFromDB() throws GenericEntityException{
    	List taxAdjustments = delegator.findByAnd("OrderIncludedAdjustment", UtilMisc.toMap("orderId", this.orderId, "orderAdjustmentTypeId", "INCLUDED_ITEM_TAX"));
    	return taxAdjustments;
    }
    
	
	public double getTaxForItem(GenericValue orderItem, List taxAdjustments) throws TaxServicesException{
		List itemAdjustments = EntityUtil.filterByAnd(taxAdjustments, UtilMisc.toMap("orderAdjustmentTypeId", "INCLUDED_ITEM_TAX", "orderItemSeqId", orderItem.getString("orderItemSeqId")));
		if(itemAdjustments.size() > 0){
			GenericValue itemValue = (GenericValue) itemAdjustments.get(0);
			String taxRateStr = itemValue.getString("sourcePercentage");
			BigDecimal taxRate = new BigDecimal(taxRateStr);
			taxRate = taxRate.multiply(new BigDecimal(100));
			taxRate = taxRate.setScale(0, BigDecimal.ROUND_HALF_UP);
			
			return taxRate.doubleValue();
		}else{
			return 0;
		}
	}
    
	
    public double getAdjustmentTotal(){
    	List taxAdjustments = this.getOrderTaxAdjustments();
    	BigDecimal totalAmount = new BigDecimal("0.00");
    	for(Iterator it = taxAdjustments.iterator();it.hasNext();){
    		GenericValue taxAdjustment = (GenericValue) it.next();
    		if(taxAdjustment != null){
	    		BigDecimal adjustmentValue = new BigDecimal(taxAdjustment.getString("amount"));
	    		totalAmount = totalAmount.add(adjustmentValue);
    		}
    	}
    	totalAmount = totalAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
    	return totalAmount.doubleValue();
    }
    
	public String getDescriptionForTax(GenericValue adjValue) throws GenericEntityException{
		if (adjValue != null) {
			String taxRateStr = adjValue.getString("sourcePercentage");
			BigDecimal taxRate = new BigDecimal(taxRateStr);
			taxRate = taxRate.multiply(new BigDecimal(100));
			taxRate = taxRate.setScale(0, BigDecimal.ROUND_HALF_UP);

			// String id = adjValue.getString("orderAdjustmentTypeId");
			// GenericValue adjustmentType =
			// delegator.findByPrimaryKey("OrderAdjustmentType",
			// UtilMisc.toMap("orderAdjustmentTypeId", id)); // + " " + taxRate
			// + "%";
			return taxRate + "%";
		} else {
			return "";
		}
		
	}
	
	
}
