package my.com.kfh.enterprise.services.ib115_gold;

import java.math.BigDecimal;
import java.util.Map;

import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.enterprise.services.ib115_gold.constant.GoldConstants;
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.IBLogger;
import my.com.kfh.web.utils.IBSessionObjects;
import my.com.kfh.web.utils.IBTimer;
import my.com.kfh.web.utils.Constant.ServiceInfoExtend;
import my.com.kfh.web.utils.constant.CurrencyCode;
import my.com.kfh.web.utils.constant.ServiceInfo;

public class IBOpenGoldConfirmServices extends IBGenericGoldServices
{
	private static Class c_oClass = IBOpenGoldConfirmServices.class;

	public IBOpenGoldConfirmServices (IBSessionObjects m_oIBSessionObjects)
	{
		super (m_oIBSessionObjects);
	}

	public void executeServices () throws IBGenericException
	{
		String m_sMethodName = "executeServices";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);
		IBDynaBean m_oViewBean = null;
		IBDynaBean m_oAccInfoBean = null;
		String fromAccNo="";

		String debitedAmount="";
		String debitedAmountDisplay="";
		String buyAmountInGrams="";
		String preferredBranch="";
		String m_sAccNickname="";
		String m_sAvailableBalance="";
		String m_sBranchName="";
		String todayPrice="";
		StringBuffer m_sFromAccountDisplay = new StringBuffer();
		StringBuffer m_sBranchDisplay = new StringBuffer();
		try
		{
			m_oViewBean = (IBDynaBean) getIBDynaBean().get (GoldConstants.GOLD_VIEW_BEAN);
			
			fromAccNo = (String) getIBDynaBean().get(GoldConstants.GOLD_FROM_ACC_NO);
			if(fromAccNo != null)
			{
				Map m_oAccountsMap = (Map)m_oViewBean.get (GoldConstants.VBEAN_FROM_ACCOUNT_MAP);
				m_oAccInfoBean = (IBDynaBean) m_oAccountsMap.get (fromAccNo);
				m_sAccNickname = (String) m_oAccInfoBean.get (CommonConstants.ACCOUNT_NICKNAME);
				m_sAvailableBalance = (String) m_oAccInfoBean.get (CommonConstants.AVAILABLE_BALANCE);
				m_sFromAccountDisplay.append (fromAccNo).append (" ").append (m_sAccNickname)
				.append (" (").append (m_sAvailableBalance).append (")");
			}
			
			buyAmountInGrams=(String) getIBDynaBean().get(GoldConstants.VBEAN_BUY_AMOUNT_IN_GRAMS);
			todayPrice = (String) m_oViewBean.get (GoldConstants.VBEAN_TODAY_PRICE);
			debitedAmount= (new BigDecimal(todayPrice).multiply(new BigDecimal(buyAmountInGrams))).toString();
			debitedAmountDisplay = FormatterUtils.formatMoneyDisplay (debitedAmount, CurrencyCode.MALAYSIA_RINGGIT, true);
			
			preferredBranch=(String) getIBDynaBean().get(GoldConstants.VBEAN_BRANCH);
			Map m_oBranchMap = (Map)m_oViewBean.get (GoldConstants.VBEAN_BRANCH_INFO_MAP);
			Map m_oBranch2Map = (Map)m_oBranchMap.get (preferredBranch);
			m_sBranchName = (String) m_oBranch2Map.get(CommonConstants.COL_BRANCH_NAME);
			if(m_sBranchName != null)
			{
				m_sBranchDisplay.append (m_sBranchName);
			}
			
			m_oViewBean.set (GoldConstants.VBEAN_FROM_ACC, m_sFromAccountDisplay.toString());
			m_oViewBean.set (GoldConstants.VBEAN_DEBITED_AMOUNT,debitedAmount);
			m_oViewBean.set (GoldConstants.VBEAN_BUY_AMOUNT_IN_GRAMS,buyAmountInGrams);
			m_oViewBean.set (GoldConstants.VBEAN_BRANCH,m_sBranchDisplay.toString());
			m_oViewBean.set (GoldConstants.VBEAN_DEBITED_AMOUNT_DISPLAY,debitedAmountDisplay);
			getIBDynaBean ().set (GoldConstants.GOLD_VIEW_BEAN, m_oViewBean);
			
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
			IBDynaBean m_oServiceInfoBean = (IBDynaBean) m_oViewBean.get(ServiceInfoExtend.SERVICE_INFO_EXTEND);
			if (((Boolean) m_oServiceInfoBean.get (ServiceInfo.BEAN_ATT_TAC_REQUIRED)).booleanValue ())
			{
				IBDynaBean m_oSMSViewBean = (IBDynaBean) getIBSessionObjects().getHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN);
				m_oSMSViewBean.set(CommonConstants.AMOUNT, debitedAmountDisplay);
				if(!DataValidator.isEmptyString(m_sAccNickname)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, m_sAccNickname.length()>8?m_sAccNickname.substring(0, 8):m_sAccNickname);
				}
				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;
			}

			e.printStackTrace();
			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);
	}
}
