/**
 * Copyright (c) 2009 Penril Datability (SEA) Sdn Bhd All rights reserved.
 *
 * This software is copyrighted. Under the copyright laws, this software
 * may not be copied, in whole or in part, without prior written consent
 * of Penril Datability (SEA) Sdn Bhd or its assignees. This software is
 * provided under the terms of a license between Penril Datability (SEA)
 * Sdn Bhd and the recipient, and its use is subject to the terms of that
 * license.
 */

package my.com.kfh.enterprise.services.ib104_fundtransfer.thirdparty;

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

import my.com.kfh.enterprise.services.GenericESBServicesImpl;
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.ib101_access.AccessConstants;
import my.com.kfh.enterprise.services.ib112_si.IBGenericSIServices;
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.esb.request.ESBAccountNumber;
import my.com.kfh.esb.response.ESBCompanyProfileBean;
import my.com.kfh.esb.response.ESBRimRetrieveProfileBean;
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.ServiceInfo;

/**
 * <pre>
 * PROGRAMMER: Latipah bt Abdul Rashid
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Oct 16, 2008
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

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

	private static String VIEWBEAN = ThirdPartyTransferTags.REG_THIRDPARTY_TRANSFER_VIEW;

	/**
	 * Class constructor specifying session object.
	 *
	 * @param m_oIBSessionObjects session object containing all information stored in HTTP session
	 */
	public IBReg3rdPartyTransferConfirmServices (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;
		String senderName = "";
		String otherPaymentDetails = "";

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

			if (m_oViewBean != null)
			{
				m_oServiceInfoBean = (IBDynaBean) m_oViewBean.get (ServiceInfo.SERVICE_INFO_BEAN);
			}

			/*
			 * call host to get AccHolderName. no need to verify a.k.a throw businessexception
			 */
			m_sToAccount = (String) getIBDynaBean ().get (ThirdPartyTransferTags.TO_REG_ACCOUNT);

			m_oVerifyThirdPartyAccNoBean = getESBServices ().verifyAccountNo (m_sToAccount);
			m_sAcctHolderName = m_oVerifyThirdPartyAccNoBean.getAccountHolderName ();
			m_sToIsoCurrency = m_oVerifyThirdPartyAccNoBean.getIsoCurrency ();
			GenericESBServicesImpl esbServices = new GenericESBServicesImpl (getIBSessionObjects());
			ESBRimRetrieveProfileBean profileBean = esbServices.getProfile((String) getIBSessionObjects().getHttpSessionAttrObject (AccessConstants.RIM_NO));
			StringBuffer sender = new StringBuffer(profileBean.getFirstName()).append(" ").append(profileBean.getLastName());
			senderName = sender.toString();
			
			if (!DataValidator.isEmptyString(senderName)){
				if(senderName.trim().length() > 40){
					senderName = senderName.trim().substring(0, 40);
				}
			}
			
			if (!DataValidator.isEmptyString(m_sAcctHolderName)){
				if(m_sAcctHolderName.trim().length() > 40){
					m_sAcctHolderName = m_sAcctHolderName.trim().substring(0, 40);
				}
			}


			/*
			 * 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));
			}

			m_sAmount = (String) getIBDynaBean ().get (ThirdPartyTransferTags.AMOUNT);

			/*
			 * get formatted from account
			 */
			Map m_oFromAccountListMap = (LinkedHashMap) m_oViewBean.get (ThirdPartyTransferTags.FROM_ACCOUNT_LIST_MAP);
			if (m_oFromAccountListMap != null)
			{
				String m_sFromAccount = (String) getIBDynaBean ().get (ThirdPartyTransferTags.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);

			/*
			 * get formatted to registered account
			 */
			Map m_oToRegAcctListMap = (LinkedHashMap) m_oViewBean.get (ThirdPartyTransferTags.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 (ThirdPartyTransferTags.TRANSFER_REF)) ? ""
					: (String) getIBDynaBean ().get (ThirdPartyTransferTags.TRANSFER_REF);
			otherPaymentDetails = DataValidator.isEmptyString((String) getIBDynaBean().get(ThirdPartyTransferTags.OTHER_PAYMENT_DETAILS))? ""
					: (String) getIBDynaBean().get(ThirdPartyTransferTags.OTHER_PAYMENT_DETAILS);
			/*
			 * call IBGenericSIServices
			 */
			m_oServiceInfoBean = (IBDynaBean)m_oViewBean.get (ServiceInfo.SERVICE_INFO_BEAN);
			
			//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()>18?m_sAcctHolderName.substring(0, 18):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 (ThirdPartyTransferTags.ACCOUNT_HOLDER_NAME, m_sAcctHolderName);
			m_oViewBean.set (ThirdPartyTransferTags.FROM_ACCOUNT_DIS, m_sFromAccountDis);
			m_oViewBean.set (ThirdPartyTransferTags.TO_REG_ACCOUNT_DIS, m_sToRegAccountDis);
			m_oViewBean.set (ThirdPartyTransferTags.AMOUNT_DIS, m_sAmountDis);
			m_oViewBean.set (ThirdPartyTransferTags.TRANSFER_REF_DIS, m_sTransferRefDis);
			m_oViewBean.set (ThirdPartyTransferTags.EMAIL_DIS, (String) getIBDynaBean ().get (ThirdPartyTransferTags.EMAIL));
			m_oViewBean.set (ThirdPartyTransferTags.FROM_ISO_CURRENCY, m_sFromIsoCurrency);
			m_oViewBean.set (ThirdPartyTransferTags.TO_ISO_CURRENCY, m_sToIsoCurrency);
			m_oViewBean.set (ThirdPartyTransferTags.OTHER_PAYMENT_DETAILS, otherPaymentDetails);
			m_oViewBean.set (ThirdPartyTransferTags.SENDER_NAME, senderName);

			// SMS Notification START
			
			ESBCompanyProfileBean residencyByRim = getESBServices ().getProfileByAccRim(m_sToAccount, null);
			String m_sBeneMobileNumber 	= residencyByRim.getPhone3() == null ? "" : residencyByRim.getPhone3().trim();
			m_oViewBean = SMSServices.setupConfirmView3rdBean(this, m_sAmount, m_sToIsoCurrency, getIBDynaBean(), m_oViewBean, m_oServiceInfoBean, m_sBeneMobileNumber);
			// SMS Notification END
			
			getIBDynaBean ().set (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);
	}
}
