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

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

import my.com.kfh.enterprise.services.IBEnterpriseServices;
import my.com.kfh.enterprise.services.IBEnterpriseServicesWorkspace;
import my.com.kfh.enterprise.services.IBUtilsExtension;
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.esb.response.ESBAccountDetailsBean;
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 IBBuyGoldConfirmServices extends IBGenericGoldServices
{
	private static Class c_oClass = IBBuyGoldConfirmServices.class;

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

	@SuppressWarnings("unchecked")
	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 fromAcc="";
		String toAcc="";
		String todayPrice="";
		String debitedAmount="";
		String debitedAmountDisplay="";
		String buyAmountInGrams="";
		String m_sAccNickname="";
		String m_sAvailableBalance="";
		String fromAccNo="";
		String toAccNo="";
		String goldAccountNo=null;
		ESBAccountDetailsBean m_oAccountDetailsBean=null;
		String m_sToGoldAccBranchCode=null;
		IBEnterpriseServices p_oEnterpriseServices=null;
		String m_sToGoldAccBranchName=null;
		StringBuffer m_sFromAccountDisplay = new StringBuffer();
		Map<String, IBDynaBean> m_oFromAccMap=null;
		Map<String, IBDynaBean> m_oToAccMap=null;
		try
		{
			m_oViewBean = (IBDynaBean) getIBDynaBean().get (GoldConstants.GOLD_VIEW_BEAN);
			//temp only for own testing
			//TAC???
			
			fromAccNo=(String) getIBDynaBean().get(GoldConstants.GOLD_FROM_ACC_NO);
			toAccNo=(String) getIBDynaBean().get(GoldConstants.GOLD_TO_ACC_NO);
			//start edited 20121012
			p_oEnterpriseServices = new IBEnterpriseServicesWorkspace(getIBSessionObjects());
			
			goldAccountNo=(String) getIBDynaBean ().get(GoldConstants.GOLD_TO_ACC_NO);
			if(goldAccountNo!=null){
				m_oAccountDetailsBean = getESBServices ().getAccountDetails (goldAccountNo); //get account detail from here
				goldAccountNo=m_oAccountDetailsBean.getAccountNo();
				if(!DataValidator.isEmptyString(goldAccountNo))
					m_sToGoldAccBranchCode=super.getBranchCodeByAccNo(goldAccountNo); 			
				if (!DataValidator.isEmptyString (m_sToGoldAccBranchCode))
				{
					m_sToGoldAccBranchName=IBUtilsExtension.getGoldBranchNameByBranchCode(p_oEnterpriseServices, m_sToGoldAccBranchCode);
				}
			}
			m_oViewBean.set (GoldConstants.VBEAN_BRANCH,m_sToGoldAccBranchName);
			m_oViewBean.set (GoldConstants.VBEAN_GOLD_ACCOUNT_NO,goldAccountNo);
			//end edited 20121012
			m_oFromAccMap = (Map<String, IBDynaBean>)m_oViewBean.get (GoldConstants.VBEAN_FROM_ACCOUNT_MAP);
			m_oAccInfoBean = (IBDynaBean) m_oFromAccMap.get (fromAccNo);
			m_sAccNickname = (String) m_oAccInfoBean.get (CommonConstants.ACCOUNT_NICKNAME);
			m_sAvailableBalance = (String) m_oAccInfoBean.get (CommonConstants.AVAILABLE_BALANCE);
			todayPrice = (String) m_oViewBean.get (GoldConstants.VBEAN_TODAY_PRICE);
			if(fromAccNo != null)
			{
				m_sFromAccountDisplay.setLength(0);
				m_sFromAccountDisplay.append (fromAccNo).append (" ").append (m_sAccNickname)
				.append (" (").append (m_sAvailableBalance).append (")");
				fromAcc = m_sFromAccountDisplay.toString();
			}
			
			m_oToAccMap=(Map<String, IBDynaBean>)m_oViewBean.get (GoldConstants.VBEAN_TO_ACCOUNT_MAP);
			m_oAccInfoBean=(IBDynaBean) m_oToAccMap.get (toAccNo);
			m_sAccNickname = (String) m_oAccInfoBean.get (CommonConstants.ACCOUNT_NICKNAME);
			m_sAvailableBalance = (String) m_oAccInfoBean.get (CommonConstants.AVAILABLE_BALANCE);
			if(toAccNo != null)
			{
				m_sFromAccountDisplay.setLength(0);
				m_sFromAccountDisplay.append (toAccNo).append (" ").append (m_sAccNickname);;
				toAcc = m_sFromAccountDisplay.toString();
			}
			
			buyAmountInGrams=(String) getIBDynaBean().get(GoldConstants.VBEAN_BUY_AMOUNT_IN_GRAMS);
			debitedAmount= (new BigDecimal(todayPrice).multiply(new BigDecimal(buyAmountInGrams))).toString();
			debitedAmountDisplay = FormatterUtils.formatMoneyDisplay (debitedAmount, CurrencyCode.MALAYSIA_RINGGIT, true);
			
			m_oViewBean.set (GoldConstants.VBEAN_FROM_ACC, fromAcc);
			m_oViewBean.set (GoldConstants.VBEAN_TO_ACC, toAcc);
			m_oViewBean.set (GoldConstants.VBEAN_DEBITED_AMOUNT,debitedAmount);
			m_oViewBean.set (GoldConstants.VBEAN_DEBITED_AMOUNT_DISPLAY,debitedAmountDisplay);
			m_oViewBean.set (GoldConstants.VBEAN_BUY_AMOUNT_IN_GRAMS,buyAmountInGrams);
			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()>14?m_sAccNickname.substring(0, 14):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);
	}
	
	
		
	
}
