/**
 *
 */
package my.com.kfh.enterprise.services.ib103_tcd;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import my.com.kfh.enterprise.services.IBEnterpriseServicesWorkspace;
import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.error.exception.IBBusinessException;
import my.com.kfh.error.exception.IBGenericException;
import my.com.kfh.error.exception.IBServiceException;
import my.com.kfh.error.handler.CommonErrorCode;
import my.com.kfh.error.handler.IBErrorHandler;
import my.com.kfh.web.utils.DataValidator;
import my.com.kfh.web.utils.FormatterUtils;
import my.com.kfh.web.utils.IBDynaBean;
import my.com.kfh.web.utils.IBDynaBeanManager;
import my.com.kfh.web.utils.IBLogger;
import my.com.kfh.web.utils.IBSessionObjects;
import my.com.kfh.web.utils.IBTimer;
import my.com.kfh.web.utils.constant.CurrencyCode;
import my.com.kfh.web.utils.constant.ServiceInfo;

/**
 * <pre>
 * PROGRAMMER: Chang Giat Yeng
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Dec 4, 2008
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

/**
 * This class handle process to get data and format it to be displayed.
 */
public class IBTcdPlacementConfirmServices extends IBEnterpriseServicesWorkspace
{
	private static Class c_oClass = IBTcdPlacementConfirmServices.class;

	/**
	 * Class constructor specifying session object.
	 *
	 * @param m_oIBSessionObjects Session object contains all information stored in HTTP session.
	 */
	public IBTcdPlacementConfirmServices (IBSessionObjects m_oIBSessionObjects)
	{
		super (m_oIBSessionObjects);
	}

	/**
	 * Get details for placement, eg. From Account number, instruction on maturity. Then format it to be displayed.
	 *
	 * @throws IBGenericException
	 */
	public void confirmTcdPlacement () throws IBGenericException
	{
		String m_sMethodName = "confirmTcdPlacement";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);

		IBDynaBean m_oTcdPlacementView = null;

		IBDynaBean m_oAccountBean = null;
		String m_sFromAccount = null;
		String m_sFromAccountDisplay = null;
		Map m_oFromAccountMap = new LinkedHashMap();

		String m_sAmount = null;
		String m_sAmountDisplay = null;
		String m_sIsoCurrency = null;
		String m_sAvailableBalance = null;

		String m_sInstructionMaturity = null;
		String m_sInstructionMaturityDisplay = null;
		Map m_oInstructionMaturityMap = new HashMap ();

		try
		{
			m_oTcdPlacementView = (IBDynaBean) getIBDynaBean ().get (TCDConstants.TCD_PLACEMENT_VIEW);
			if (m_oTcdPlacementView == null)
			{
				m_oTcdPlacementView = IBDynaBeanManager.createIBDynaBean (TCDConstants.TCD_PLACEMENT_VIEW, getIBDynaBean ().getActionMapping ());
			}

			m_sFromAccount = (String)getIBDynaBean ().get (TCDConstants.FROM_ACCOUNT);
			m_oFromAccountMap = (Map)m_oTcdPlacementView.get (TCDConstants.FROM_ACCOUNT_MAP);
			m_oAccountBean = (IBDynaBean)m_oFromAccountMap.get (m_sFromAccount);
			m_sFromAccountDisplay = DataValidator.isEmptyString (m_sFromAccount) ?
					(String)m_oAccountBean.get (CommonConstants.ACCOUNT_NICKNAME) : m_sFromAccount.concat (" ").concat ((String)m_oAccountBean.get (CommonConstants.ACCOUNT_NICKNAME));
			m_oTcdPlacementView.set (TCDConstants.FROM_ACCOUNT_DISPLAY, m_sFromAccountDisplay);

			m_sIsoCurrency = (String)m_oAccountBean.get (CommonConstants.ISO_CURRENCY);
			m_sIsoCurrency = DataValidator.isEmptyString (m_sIsoCurrency) ? CurrencyCode.MALAYSIA_RINGGIT : m_sIsoCurrency;
			m_oTcdPlacementView.set (TCDConstants.ISO_CURRENCY, m_sIsoCurrency);

			m_sAvailableBalance = (String)m_oAccountBean.get (CommonConstants.AVAILABLE_BALANCE);
			m_sAvailableBalance = FormatterUtils.getAmountInNumeric (m_sAvailableBalance);
			verifyMinPlacement (m_sIsoCurrency, m_sAvailableBalance);

			m_sAmount = (String)getIBDynaBean ().get (TCDConstants.AMOUNT);
			m_sAmountDisplay = DataValidator.isEmptyString (m_sAmount) ? "0" : FormatterUtils.formatMoneyDisplay (m_sAmount, m_sIsoCurrency, true);
			m_oTcdPlacementView.set (TCDConstants.AMOUNT_DISPLAY, m_sAmountDisplay);

			m_sInstructionMaturity = (String)getIBDynaBean ().get (TCDConstants.INST_MAT);
			m_oInstructionMaturityMap = (Map)m_oTcdPlacementView.get (TCDConstants.INST_MAT_OPTIONS_MAP);
			m_sInstructionMaturityDisplay = (String)m_oInstructionMaturityMap.get (m_sInstructionMaturity);
			m_oTcdPlacementView.set (TCDConstants.INST_MAT_DISPLAY, m_sInstructionMaturityDisplay);

			getIBDynaBean ().set (TCDConstants.TCD_PLACEMENT_VIEW, m_oTcdPlacementView);
			
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
			IBDynaBean m_oServiceInfoBean = (IBDynaBean) m_oTcdPlacementView.get(ServiceInfo.SERVICE_INFO_BEAN);
			if (((Boolean) m_oServiceInfoBean.get (ServiceInfo.BEAN_ATT_TAC_REQUIRED)).booleanValue ())
			{
				IBDynaBean m_oSMSViewBean = (IBDynaBean) getIBSessionObjects().getHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN);
				String beneName = getIBSessionObjects().getVisitor().getName();
				m_oSMSViewBean.set(CommonConstants.AMOUNT, m_oTcdPlacementView.get(TCDConstants.AMOUNT_DISPLAY));
				if(!DataValidator.isEmptyString(beneName)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, beneName.length()>18?beneName.substring(0, 18):beneName);
				}
				getIBSessionObjects().setHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN, m_oSMSViewBean);
			}
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
		}
		catch (Exception e)
		{
			if (e instanceof IBGenericException)
			{
				throw (IBGenericException) e;
			}

			throw IBErrorHandler.createServiceException(IBServiceException.GENERAL_SERVICE_ERROR, true,
					new String[] {CommonErrorCode.GENERIC_CODE, "", "", "", IBErrorHandler.generateErrorMessage(e)}, c_oClass);
		}

		IBLogger.logBSFNMethodLeavingMessage (c_oClass, m_sMethodName, m_oTimer);
	}

	/**
	 * Get minimum principal amount to be placement based on tenure and ISO currency.
	 * Check available balance of account to make sure the balance is sufficient to make placement.
	 *
	 * @param p_sISOCurrency ISO currency
	 * @param p_sAvailableBalance Available balance of account
	 * @throws IBGenericException
	 */
	public void verifyMinPlacement (String p_sISOCurrency, String p_sAvailableBalance) throws IBGenericException
	{
		String m_sMethodName = "verifyMinPlacement";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName, String.class.getName ());

		String m_sTenure = null;
		int m_iTenure = 0;

		String m_sAmount = null;
		double m_dAmount = 0;
		double m_dAvailableBalance = 0;

		try
		{
			m_sTenure = (String)getIBDynaBean ().get (TCDConstants.TENURE);
			m_iTenure = DataValidator.isEmptyString (m_sTenure) ? 0 : Integer.parseInt (m_sTenure);

			m_sAmount = (String)getIBDynaBean ().get (TCDConstants.AMOUNT);
			m_dAmount = DataValidator.isEmptyString (m_sAmount) ? 0 : Double.parseDouble (m_sAmount);
			m_dAvailableBalance = DataValidator.isEmptyString (p_sAvailableBalance) ? 0 : Double.parseDouble (p_sAvailableBalance);

			if(CurrencyCode.isMalaysiaRinggit (p_sISOCurrency) || CurrencyCode.isMalaysiaRinggit2 (p_sISOCurrency)
					|| CurrencyCode.isMalaysiaRinggit3 (p_sISOCurrency))
			{
				if (m_iTenure == 1 && m_dAmount < TCDConstants.TCD_PLACE_MIN_RM_AMT_ONE_TENURE)
				{
					throw IBErrorHandler.createBusinessException (IBBusinessException.TCD_PLACEMENT, "1",
							new String[] {FormatterUtils.formatMoneyDisplay (String.valueOf (TCDConstants.TCD_PLACE_MIN_RM_AMT_ONE_TENURE),
									p_sISOCurrency, true)});
				}
				else if (m_iTenure > 1 && m_dAmount < TCDConstants.TCD_PLACE_MIN_RM_AMT_OTHER_TENURE)
				{
					throw IBErrorHandler.createBusinessException (IBBusinessException.TCD_PLACEMENT, "1",
							new String[] {FormatterUtils.formatMoneyDisplay (String.valueOf (TCDConstants.TCD_PLACE_MIN_RM_AMT_OTHER_TENURE),
									p_sISOCurrency, true)});
				}
			}
			else
			{
				if (m_dAmount < TCDConstants.TCD_PLACE_MIN_FOREIGN_AMT)
				{
					throw IBErrorHandler.createBusinessException (IBBusinessException.TCD_PLACEMENT, "1",
							new String[] {FormatterUtils.formatMoneyDisplay (String.valueOf (TCDConstants.TCD_PLACE_MIN_FOREIGN_AMT),
									p_sISOCurrency, true)});
				}
			}

			if (m_dAmount > m_dAvailableBalance)
			{
				throw IBErrorHandler.createBusinessException (IBBusinessException.TCD_PLACEMENT, "2",
						new String[] {FormatterUtils.formatMoneyDisplay (p_sAvailableBalance,
								p_sISOCurrency, true)});
			}

		}
		catch (Exception e)
		{
			if (e instanceof IBGenericException)
			{
				throw (IBGenericException) e;
			}

			throw IBErrorHandler.createServiceException(IBServiceException.GENERAL_SERVICE_ERROR, true,
					new String[] {CommonErrorCode.GENERIC_CODE, "", "", "", IBErrorHandler.generateErrorMessage(e)}, c_oClass);
		}

		IBLogger.logBSFNMethodLeavingMessage (c_oClass, m_sMethodName, m_oTimer);
	}
}
