/**
 * Copyright (c) 2010 Penril Datability (M) 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 (M) Sdn Bhd or its assignees. This software is
 * provided under the terms of a license between Penril Datability (M)
 * Sdn Bhd and the recipient, and its use is subject to the terms of that
 * license.
 */
package my.com.kfh.enterprise.services.ib104_fundtransfer.ibft;

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.IBUtils;
import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.enterprise.services.ib101_access.AccessConstants;
import my.com.kfh.enterprise.services.ib104_fundtransfer.interbank.InterbankConstants;
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.response.ESBIBFTResponseBean;
import my.com.kfh.esb.response.ESBRimRetrieveProfileBean;
import my.com.kfh.hibernate.tables.IBGSTContent;
import my.com.kfh.hibernate.utils.HibernateUtils;
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.GSTUtils;
import my.com.kfh.web.utils.IBDynaBean;
import my.com.kfh.web.utils.IBDynaBeanManager;
import my.com.kfh.web.utils.IBLoadProperty;
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.ESBConstants;
import my.com.kfh.web.utils.constant.ServiceInfo;

/**
 * <pre>
 * PROGRAMMER: Latipah Ab Rashid
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Oct 25, 2010
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

/**
 * <Class description>
 */
public class IBIBFTConfirmServices extends IBEnterpriseServicesWorkspace
{
	private static Class<IBIBFTConfirmServices> c_oClass = IBIBFTConfirmServices.class;

	public IBIBFTConfirmServices (IBSessionObjects p_oIBSessionObjects)
	{
		super (p_oIBSessionObjects);
	}

	/**
	 * Formats details of transfer for display and sets into view bean.
	 *
	 * @throws IBGenericException
	 */
	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_sAmountDis = "";
		String m_sBeneBankNameDis = "";
		String m_sBeneBankAccountNo = "";
		String m_sBeneName = "";
		String m_sBeneEmail= "";
		String m_sBeneBankCode = "";
		String m_sIbftBankMemberId = "";
		String m_sTransactionRefNo = "";
		String m_sTransactionDescription = "";
		String otherPaymentDetails = "";
		String m_sFromIsoCurrency = "";
		String m_sTransferType = "";
		StringBuffer m_sbTempBuffer = new StringBuffer ();
		String senderName = "";

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

			m_oServiceInfoBean = (IBDynaBean) m_oViewBean.get (ServiceInfo.SERVICE_INFO_BEAN);
				
			m_sTransferType = (String) m_oViewBean.get (IBFTConstants.TRANSFER_TYPE);
			
			String m_sFromAccount = (String) getIBDynaBean ().get (IBFTConstants.FROM_ACCOUNT);
			String m_sAmount = (String) getIBDynaBean ().get (IBFTConstants.AMOUNT);
			m_sBeneBankAccountNo = (String) getIBDynaBean ().get (IBFTConstants.BENE_ACCOUNT_NO);
			
			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();
			/*
			 * Get beneficiary bank, beneficiary account no, beneficiary holder name
			 */
			if (IBFTConstants.TRANSFER_TYPE_OPEN.equalsIgnoreCase (m_sTransferType))
			{
				m_sBeneBankCode = (String) getIBDynaBean ().get (IBFTConstants.BENE_BANK);
				
				Map m_oBeneficiaryBankListMap = (LinkedHashMap) m_oViewBean.get (IBFTConstants.BENE_BANK_LIST_MAP);
				Map m_oBeneficiaryBankMap = (HashMap) m_oBeneficiaryBankListMap.get (m_sBeneBankCode);

				m_sBeneBankNameDis = (String) m_oBeneficiaryBankMap.get (InterbankConstants.IB_INTERBANK_LIST.IBFT_MEMBER_NAME.toString ());

				m_sIbftBankMemberId = (String) m_oBeneficiaryBankMap.get (InterbankConstants.IB_INTERBANK_LIST.IBFT_MEMBER_ID.toString ());
			}
			else if (IBFTConstants.TRANSFER_TYPE_REG.equalsIgnoreCase (m_sTransferType))
			{
				Map m_oBeneficiaryAccountListMap = (LinkedHashMap) m_oViewBean.get (IBFTConstants.BENE_ACCOUNT_LIST_MAP);
				Map m_oBeneficiaryAccountMap = (HashMap) m_oBeneficiaryAccountListMap.get (m_sBeneBankAccountNo);

				m_sBeneBankCode = (String) m_oBeneficiaryAccountMap.get (IBFTConstants.IB_IBFT_ACCOUNTS.BANK_CODE.toString ());
				m_sBeneBankNameDis = (String) m_oBeneficiaryAccountMap.get (IBFTConstants.IB_IBFT_ACCOUNTS.BANK_NAME.toString ());

				getIBDynaBean ().set (IBFTConstants.BENE_BANK, m_sBeneBankCode);

				m_sIbftBankMemberId = (String) m_oBeneficiaryAccountMap.get (IBFTConstants.IB_IBFT_ACCOUNTS.IBFT_BANK_MEMBER_ID.toString ());
			}
			
			/*
			 * Call ESB to verify bene account no. Throw exception if not a valid acct.
			 */
			boolean isBypass = false;
			if(!DataValidator.isEmptyString(IBLoadProperty.getValue("ibft.bypass")))
				isBypass = IBLoadProperty.getValue("ibft.bypass").equalsIgnoreCase("T")?true:false;
					
			if(!isBypass){
			ESBIBFTResponseBean m_oESBIBFTResponseBean = getESBServices ().ibftAccountEnquiry (
					(String) getIBSessionObjects ().getHttpSessionAttrObject (AccessConstants.RIM_NO), 
					m_sFromAccount, m_sBeneBankAccountNo, m_sAmount, m_sIbftBankMemberId);
			
			if (!ESBConstants.IB_SUCCESS_RC.equalsIgnoreCase (m_oESBIBFTResponseBean.getResponseCode ())
					 || DataValidator.isEmptyString (m_oESBIBFTResponseBean.getBeneName ())
					 || "null".equals (m_oESBIBFTResponseBean.getBeneName ()))
			{
				throw IBErrorHandler.createBusinessException (
						IBBusinessException.IBFT, IBFTConstants.ERR_BENE_ACCOUNT_VERIFY_FAIL);
			}
			m_sBeneName = m_oESBIBFTResponseBean.getBeneName ();
			} else 
				m_sBeneName = "UAT Tester";
			

			/*
			 * get email display, if filled by user
			 */
			m_sBeneEmail = DataValidator.isEmptyString ((String) getIBDynaBean ().get (IBFTConstants.BENE_EMAIL)) ? "-"
					: (String) getIBDynaBean ().get (IBFTConstants.BENE_EMAIL);

			/*
			 * get formatted from account
			 */
			Map m_oFromAccountListMap = (LinkedHashMap) m_oViewBean.get (IBFTConstants.FROM_ACCOUNT_LIST_MAP);
			if (m_oFromAccountListMap != null)
			{
				IBDynaBean 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_sFromIsoCurrency, true);
			
			//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_sBeneName)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, m_sBeneName.length()>14?m_sBeneName.substring(0, 14):m_sBeneName);
				}
				getIBSessionObjects().setHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN, m_oSMSViewBean);
			}
			//Pixakarft Bank Negara Changes 30th Mar 2013 start

			/*
			 * get transaction ref number display, if filled by user
			 */
			m_sTransactionRefNo = DataValidator.isEmptyString ((String) getIBDynaBean ().get (IBFTConstants.TRANSACTION_REF_NO)) ? "-"
					: (String) getIBDynaBean ().get (IBFTConstants.TRANSACTION_REF_NO);

			/*
			 * get transaction description display, if filled by user
			 */
			m_sTransactionDescription = DataValidator.isEmptyString ((String) getIBDynaBean ().get (IBFTConstants.TRANSACTION_DESC)) ? "-"
					: (String) getIBDynaBean ().get (IBFTConstants.TRANSACTION_DESC);
			
			otherPaymentDetails = DataValidator.isEmptyString((String) getIBDynaBean().get(IBFTConstants.OTHER_PAYMENT_DETAILS)) ? "-"
					: (String) getIBDynaBean().get(IBFTConstants.OTHER_PAYMENT_DETAILS);
			
			String m_sIsStaffAccount = getESBServices().getInquiryStaffByAcctNo(m_sFromAccount).getStatus();
			
			m_oViewBean.set (IBFTConstants.IS_STAFF_ACCOUNT, m_sIsStaffAccount);
			m_oViewBean.set (IBFTConstants.FROM_ACCOUNT_DIS, m_sFromAccountDis);
			m_oViewBean.set (IBFTConstants.AMOUNT_DIS, m_sAmountDis);
			m_oViewBean.set (IBFTConstants.BENE_BANK_DIS, m_sBeneBankNameDis);
			m_oViewBean.set (IBFTConstants.BENE_ACCOUNT_NO, m_sBeneBankAccountNo);
			m_oViewBean.set (IBFTConstants.BENE_NAME, m_sBeneName);
			m_oViewBean.set (IBFTConstants.BENE_EMAIL, m_sBeneEmail);
			m_oViewBean.set (IBFTConstants.TRANSACTION_REF_NO, m_sTransactionRefNo);
			m_oViewBean.set (IBFTConstants.TRANSACTION_DESC, m_sTransactionDescription);
			m_oViewBean.set(IBFTConstants.OTHER_PAYMENT_DETAILS, otherPaymentDetails);
			m_oViewBean.set (IBFTConstants.FROM_ISO_CURRENCY, m_sFromIsoCurrency);
			m_oViewBean.set (IBFTConstants.BANK_MEMBER_ID, m_sIbftBankMemberId);
			m_oViewBean.set (IBFTConstants.SENDER_NAME, senderName);

			// SMS Notification START
			m_oViewBean = SMSServices.setupConfirmViewBean(this, m_sAmount, m_sFromIsoCurrency, getIBDynaBean(), m_oViewBean, m_oServiceInfoBean);
			// SMS Notification END
			
			getIBDynaBean ().set (IBFTConstants.IBFT_TRANSFER_VIEW, m_oViewBean);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			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);
	}
}
