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

/**
 * PROGRAMMER: Goh Kee Kun
 * COMPANY: Pixakraft
 * DATE CREATED: 15-Oct-2012
 */

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

import my.com.kfh.enterprise.services.IBEnterpriseServicesWorkspace;
import my.com.kfh.enterprise.services.common.AccountInfoTag;
import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.enterprise.services.ib112_si.IBGenericSIServices;
import my.com.kfh.error.exception.IBBusinessExceptionExtension;
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.ESBAccountInfoDetailBean;
import my.com.kfh.esb.response.ESBVerifyThirdPartyAccNoBean;
import my.com.kfh.sms.services.SMSServices;
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.ESBConstants;
import my.com.kfh.web.utils.constant.ServiceInfo;


/**
 * Sets display information of transfer details into IBDynaBean.
 */
public class IBGoldReg3rdPartyTransferConfirmServices extends
		IBEnterpriseServicesWorkspace
{
	private static Class c_oClass = IBGoldReg3rdPartyTransferConfirmServices.class;

	private static String FAV_VIEWBEAN = ThirdPartyGoldTransferTags.REG_THIRDPARTY_GOLD_TRANSFER_VIEW;

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

	/**
	 * Verify account number. Formats details of transfer for display and sets into view bean.
	 *
	 * @throws IBGenericException if any Exception are thrown
	 */
	public void verifyInput () throws IBGenericException
	{
		String m_sMethodName = "verifyInput";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);

		IBDynaBean m_oViewBean = null;
		IBDynaBean m_oServiceInfoBean = null;
		String m_sFromAccountDis = "";
		String m_sToAccount = "";
		String m_sToRegAccountDis = "";
		String m_sAmount = "";
		String m_sAmountDis = "";
		String m_sTransferRefDis = "";
		String m_sAcctHolderName = "";
		String m_sFromIsoCurrency = "";
		String m_sToIsoCurrency = "";
		StringBuffer m_sbTempBuffer = new StringBuffer ();
//		ESBAccountNumber m_oESBAccountNumber = null;
		ESBVerifyThirdPartyAccNoBean m_oVerifyThirdPartyAccNoBean = null;

		try
		{
			m_oViewBean = (IBDynaBean) getIBDynaBean ().get (FAV_VIEWBEAN) == null
				? IBDynaBeanManager.createIBDynaBean (FAV_VIEWBEAN, getIBDynaBean ().getActionMapping ())
						: (IBDynaBean) getIBDynaBean ().get (FAV_VIEWBEAN);

			if (m_oViewBean != null)
			{
				m_oServiceInfoBean = (IBDynaBean) m_oViewBean.get (ThirdPartyGoldTransferTags.SERVICE_INFO_BEAN_EXTEND);
			}

			/*
			 * call host to get AccHolderName. no need to verify a.k.a throw businessexception
			 */
			m_sToAccount = (String) getIBDynaBean ().get (ThirdPartyGoldTransferTags.TO_REG_ACCOUNT);
			//1st checking
			m_oVerifyThirdPartyAccNoBean = getESBServices ().verifyAccountNo (m_sToAccount);
			if (!ESBConstants.IB_SUCCESS_RC.equalsIgnoreCase (m_oVerifyThirdPartyAccNoBean.getResponseCode ()))
			{
				throw IBErrorHandler.createBusinessException (
						IBBusinessExceptionExtension.GOLDTRANSFER_3RDPARTY_KEY, ThirdPartyGoldTransferTags.VA_ERR_INVALID_ACCOUNT);
			}
			m_sAcctHolderName = m_oVerifyThirdPartyAccNoBean.getAccountHolderName ();
			m_sToIsoCurrency = m_oVerifyThirdPartyAccNoBean.getIsoCurrency ();


			/*
			 * verify account's currency whether it is supported for this transaction
			 */
//			m_oESBAccountNumber = new ESBAccountNumber ();
//			if (!m_oESBAccountNumber.validateSupportAccountCurrency (m_sToAccount))
//			{
//				throw IBErrorHandler.createBusinessException (IBBusinessException.ESB, String.valueOf (1));
//			}
			//2nd checking
			ESBAccountInfoDetailBean m_oTCEnquiryBean = getESBServices ().getAccountInfoDetail(m_sToAccount);
			
			if(!ESBConstants.isGoldAccount (m_oTCEnquiryBean.getAcctType())){
				throw IBErrorHandler.createBusinessException (ThirdPartyGoldTransferTags.GOLD_ESB, String.valueOf (1));
			}
			
			m_sAmount = (String) getIBDynaBean ().get (ThirdPartyGoldTransferTags.AMOUNT);

			/*
			 * get formatted from account
			 */
			Map<String, IBDynaBean> m_oFromAccountListMap = (Map<String, IBDynaBean>) m_oViewBean.get (ThirdPartyGoldTransferTags.FROM_ACCOUNT_LIST_MAP);
			if (m_oFromAccountListMap != null)
			{
				String m_sFromAccount = (String) getIBDynaBean ().get (ThirdPartyGoldTransferTags.FROM_ACCOUNT);
				IBDynaBean m_oSelectedFromAccountBean = null;
				m_oSelectedFromAccountBean = (IBDynaBean) m_oFromAccountListMap.get (m_sFromAccount);

				m_sFromIsoCurrency = (String) m_oSelectedFromAccountBean.get (CommonConstants.ISO_CURRENCY);

				m_sbTempBuffer = new StringBuffer ();
				m_sbTempBuffer.append (m_sFromAccount);
				m_sbTempBuffer.append (" ");
				m_sbTempBuffer.append ((String) m_oSelectedFromAccountBean.get (CommonConstants.ACCOUNT_NICKNAME));

				m_sFromAccountDis = m_sbTempBuffer.toString ();
			}

			/*
			 * get formatted amount
			 */
//			m_sAmountDis = FormatterUtils.formatMoneyDisplay (m_sAmount, m_sToIsoCurrency, true);
			m_sAmountDis = FormatterUtils.formatGoldUnitDisplay (m_sAmount, true);
			
			/*
			 * get formatted to registered account
			 */
			Map<String, IBDynaBean> m_oToRegAcctListMap = (Map<String, IBDynaBean>) m_oViewBean.get (ThirdPartyGoldTransferTags.TO_REG_ACCOUNT_LIST_MAP);
			if (m_oToRegAcctListMap  != null)
			{
				Map m_oSelectedToRegAcctMap = (HashMap) m_oToRegAcctListMap.get (m_sToAccount);

				m_sbTempBuffer = new StringBuffer ();
				m_sbTempBuffer.append (m_sToAccount);
				m_sbTempBuffer.append (" ");
				m_sbTempBuffer.append ((String) m_oSelectedToRegAcctMap.get (AccountInfoTag.ACCOUNT_NICKNAME));

				m_sToRegAccountDis = m_sbTempBuffer.toString ();
			}

			/*
			 * get transfer ref dis if filled by user
			 */
			m_sTransferRefDis = DataValidator.isEmptyString ((String) getIBDynaBean ().get (ThirdPartyGoldTransferTags.TRANSFER_REF)) ? ""
					: (String) getIBDynaBean ().get (ThirdPartyGoldTransferTags.TRANSFER_REF);

			/*
			 * call IBGenericSIServices
			 */
			m_oServiceInfoBean = (IBDynaBean)m_oViewBean.get (ThirdPartyGoldTransferTags.SERVICE_INFO_BEAN_EXTEND);
			
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
			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, m_sAmountDis);
				if(!DataValidator.isEmptyString(m_sAcctHolderName)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, m_sAcctHolderName.length()>8?m_sAcctHolderName.substring(0, 8):m_sAcctHolderName);
				}
				getIBSessionObjects().setHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN, m_oSMSViewBean);
			}
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
			
			if (m_oServiceInfoBean != null && ((Boolean)m_oServiceInfoBean.get (ServiceInfo.BEAN_ATT_FUTURE_TRANSACTION_FLAG)).booleanValue ())
			{
				IBGenericSIServices m_oGenericSIServices = new IBGenericSIServices (getIBSessionObjects ());
				m_oGenericSIServices.setIBDynaBean (getIBDynaBean ());
				m_oViewBean = m_oGenericSIServices.confirmSIInput (m_oViewBean);
			}

			m_oViewBean.set (ThirdPartyGoldTransferTags.ACCOUNT_HOLDER_NAME, m_sAcctHolderName);
			m_oViewBean.set (ThirdPartyGoldTransferTags.FROM_ACCOUNT_DIS, m_sFromAccountDis);
			m_oViewBean.set (ThirdPartyGoldTransferTags.TO_REG_ACCOUNT_DIS, m_sToRegAccountDis);
			m_oViewBean.set (ThirdPartyGoldTransferTags.AMOUNT_DIS, m_sAmountDis);
			m_oViewBean.set (ThirdPartyGoldTransferTags.TRANSFER_REF_DIS, m_sTransferRefDis);
			m_oViewBean.set (ThirdPartyGoldTransferTags.EMAIL_DIS, (String) getIBDynaBean ().get (ThirdPartyGoldTransferTags.EMAIL));
			m_oViewBean.set (ThirdPartyGoldTransferTags.FROM_ISO_CURRENCY, m_sFromIsoCurrency);
			m_oViewBean.set (ThirdPartyGoldTransferTags.TO_ISO_CURRENCY, m_sToIsoCurrency);

			// SMS Notification START
			m_oViewBean = SMSServices.setupConfirmViewBean(this, m_sAmount, CurrencyCode.MALAYSIA_RINGGIT, getIBDynaBean(), m_oViewBean, m_oServiceInfoBean);
			// SMS Notification END
			
			getIBDynaBean ().set (FAV_VIEWBEAN, m_oViewBean);
		}
		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);
	}
}
