package com.konakart.bl.modules.ordertotal.intereses;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.torque.TorqueException;

import com.konakart.app.KKConfiguration;
import com.konakart.app.KKException;
import com.konakart.app.Order;
import com.konakart.app.OrderTotal;
import com.konakart.appif.KKEngIf;
import com.konakart.bl.modules.BaseModule;
import com.konakart.bl.modules.ordertotal.BaseOrderTotalModule;
import com.konakart.bl.modules.ordertotal.OrderTotalInterface;
import com.konakart.bl.modules.payment.PaymentExtendedInterface;
import com.workingdogs.village.DataSetException;

/**
 * Module that creates an OrderTotal object for Intereses cost.
 * 
 */
public class Intereses extends BaseOrderTotalModule implements OrderTotalInterface
{

    private static int sortOrder = -1;

    private static String code = "ot_intereses";

    private static String bundleName = BaseModule.basePackage 
    		+ ".ordertotal.intereses.Intereses";

    private static HashMap<Locale,ResourceBundle> resourceBundleMap = new HashMap<Locale,ResourceBundle>();

    private static String mutex = "otInteresesMutex";

    // Configuration Keys

    private final static String MODULE_ORDER_TOTAL_INTERESES_SORT_ORDER = "MODULE_ORDER_TOTAL_INTERESES_SORT_ORDER";

    private final static String MODULE_ORDER_TOTAL_INTERESES_STATUS = "MODULE_ORDER_TOTAL_INTERESES_STATUS";

    // Message Catalogue Keys

    private final static String MODULE_ORDER_TOTAL_INTERESES_TITLE = "module.order.total.intereses.title";

    // private final static String MODULE_ORDER_TOTAL_INTERESES_DESCRIPTION =
    // "module.order.total.shipping.description";

    /**
     * Constructor
     * 
     * @param eng
     * 
     * @throws DataSetException
     * @throws KKException
     * @throws TorqueException
     * 
     */
    public Intereses(KKEngIf eng) throws TorqueException, KKException, DataSetException
    {
        super.init(eng);

        // Create the static maps from the configuration info
        if (sortOrder == -1)
        {
            synchronized (mutex)
            {
                if (sortOrder == -1)
                {
                    setStaticVariables();
                }
            }
        }
    }

    /**
     * Sets some static variables during setup
     * 
     * @throws KKException
     * 
     */
    public void setStaticVariables() throws KKException
    {
        KKConfiguration conf;

        conf = getEng().getConfiguration(MODULE_ORDER_TOTAL_INTERESES_SORT_ORDER);
        if (conf == null)
        {
            sortOrder = 0;
        } else
        {
            sortOrder = new Integer(conf.getValue()).intValue();
        }
    }

    /**
     * Returns true or false
     * 
     * @throws KKException
     */
    public boolean isAvailable() throws KKException
    {
        return isAvailable(getEng(), MODULE_ORDER_TOTAL_INTERESES_STATUS);
    }

    /**
     * Create and return an OrderTotal object for the shipping cost.
     * 
     * @param order
     * @param dispPriceWithTax
     * @param locale
     * @return Returns an OrderTotal object for this module
     * @throws Exception
     */
    public OrderTotal getOrderTotal(Order order, boolean dispPriceWithTax, Locale locale)
            throws Exception
    {
        OrderTotal ot;

        // Get the resource bundle
        ResourceBundle rb = getResourceBundle(mutex, bundleName, resourceBundleMap, locale);
        if (rb == null)
        {
            throw new KKException("A resource file cannot be found for the country "
                    + locale.getCountry());
        }

        ot = new OrderTotal();
        ot.setSortOrder(sortOrder);
        ot.setClassName(code);
        
        if (order.getPaymentDetails() != null && order.getPaymentDetails() instanceof PaymentExtendedInterface)
        {
        	BigDecimal interes = new BigDecimal(((PaymentExtendedInterface)order.getPaymentDetails()).getInteres(Integer.parseInt(order.getPaymentDetails().getCustom3())-1) / 100);
            ot.setTitle(" Intereses ("
                    + order.getPaymentDetails().getCustom3() + "):");
            BigDecimal cost;
            if (dispPriceWithTax)
            {
                cost = order.getSubTotalIncTax().multiply(interes);
            } else
            {
                cost = order.getSubTotalExTax().multiply(interes);
            }
            ot.setValue(cost);
            ot.setText(getCurrMgr().formatPrice(cost, order.getCurrencyCode()));
        }
        else
        {
            ot.setTitle(rb.getString(MODULE_ORDER_TOTAL_INTERESES_TITLE) + ":");
            ot.setValue(new BigDecimal(0));
            ot.setText(getCurrMgr().formatPrice(new BigDecimal(0), order.getCurrencyCode()));
        } 
        return ot;
    }

    public int getSortOrder()
    {
        return sortOrder;
    }

    public String getCode()
    {
        return code;
    }
}
