/**
 * 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.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

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.web.utils.DataValidator;
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.ESBConstants;
import my.com.kfh.web.utils.constant.ServiceInfo;

/**
 * <pre>
 * PROGRAMMER:Nor Shuhaida Subri
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Oct 27, 2010
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

/**
 * <Class description>
 */
public class IBIBFTMaintAddConfirmServices extends IBEnterpriseServicesWorkspace {
	
	private static Class<IBIBFTMaintAddConfirmServices> c_oClass = IBIBFTMaintAddConfirmServices.class;
	private static String VIEWBEAN = "ibftMaintenanceView";

	public IBIBFTMaintAddConfirmServices(IBSessionObjects m_oIBSessionObjects) {
		super(m_oIBSessionObjects);
	}
	
	/**
	 * Get selected values and formats them for display
	 *
	 * @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;
		Map<String, Map> m_oBeneficiaryBankListMap = null;
		Map<String, String> m_oBeneficiaryBankMap = null;
		String m_sBeneBankCode = "";
		String m_sBeneBankName = "";
		String m_sIBFTMemberId = "";
		String m_sBeneAcctNo ="";
		String m_sBeneName = "";
		try
		{
			m_oViewBean = (IBDynaBean) getIBDynaBean ().get (VIEWBEAN) == null
				? IBDynaBeanManager.createIBDynaBean(VIEWBEAN, getIBDynaBean ().getActionMapping ())
						: (IBDynaBean) getIBDynaBean ().get (VIEWBEAN);

			m_sBeneBankCode = (String) getIBDynaBean ().get (IBFTConstants.BENE_BANK);
			m_oBeneficiaryBankListMap = (LinkedHashMap<String, Map>) m_oViewBean.get (IBFTConstants.BENE_BANK_LIST_MAP);


			m_sBeneAcctNo = (String) getIBDynaBean ().get (IBFTConstants.BENE_ACCOUNT_NO);

			m_oBeneficiaryBankMap = (HashMap<String, String>) m_oBeneficiaryBankListMap.get (m_sBeneBankCode);
			m_sBeneBankName = (String) m_oBeneficiaryBankMap.get (InterbankConstants.IB_INTERBANK_LIST.IBFT_MEMBER_NAME.toString ());
			m_sIBFTMemberId = (String) m_oBeneficiaryBankMap.get (InterbankConstants.IB_INTERBANK_LIST.IBFT_MEMBER_ID.toString ());
			
			 /* 
			  * Call ESB to verify bene account no. 
			  * In order to get successful response from powercard, cannot set amount <= 0 or null and cannot set from account as null.
			  * Pass in any account from user profile and set amount as 1. 
			  * Throw exception if not a valid acct.
			  */
			//get any account from user profile to get response msg from powercard
			String m_sFromAcctNo = getSingleFromAccount ();
			//hardcode any amount > 0 to get response msg from powercard
			String m_sAmount = "1"; 
			
			ESBIBFTResponseBean m_oESBIBFTResponseBean = getESBServices ().ibftAccountEnquiry (
					(String) getIBSessionObjects ().getHttpSessionAttrObject (AccessConstants.RIM_NO), 
					m_sFromAcctNo, m_sBeneAcctNo, m_sAmount, m_sIBFTMemberId);
			
			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 ();
			
			m_oViewBean.set (IBFTConstants.BENE_ACCOUNT_NO, (String) getIBDynaBean ().get (IBFTConstants.BENE_ACCOUNT_NO));
			m_oViewBean.set (IBFTConstants.BENE_BANK_DIS, m_sBeneBankName);
			m_oViewBean.set (IBFTConstants.BENE_NAME, m_sBeneName);
			m_oViewBean.set (IBFTConstants.BENE_NICK_NAME_DIS, DataValidator.isEmptyString ((String) getIBDynaBean ().get (IBFTConstants.BENE_NICK_NAME))?"-":(String) getIBDynaBean ().get (IBFTConstants.BENE_NICK_NAME));
			m_oViewBean.set (IBFTConstants.BANK_MEMBER_ID, m_sIBFTMemberId);
			m_oViewBean.set (IBFTConstants.EMAIL_DIS, DataValidator.isEmptyString ((String)getIBDynaBean ().get (IBFTConstants.EMAIL))?"-":getIBDynaBean ().get (IBFTConstants.EMAIL));

			getIBDynaBean ().set (VIEWBEAN, m_oViewBean);
			
			IBDynaBean m_oServiceInfoBean = (IBDynaBean) m_oViewBean.get (ServiceInfo.SERVICE_INFO_BEAN);
			if (((Boolean) m_oServiceInfoBean.get (ServiceInfo.BEAN_ATT_TAC_REQUIRED)).booleanValue ())
			{
				IBDynaBean m_oSMSViewBean = (IBDynaBean) getIBSessionObjects().getHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN);
				if(!DataValidator.isEmptyString(m_sBeneName)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, m_sBeneName.length()>25?m_sBeneName.substring(0, 25):m_sBeneName);
				}
				
				getIBSessionObjects().setHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN, m_oSMSViewBean);
			}
		}
		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);
	}
	
	/**
	 * To get any from account from user profile
	 *
	 * @return A from account from user profile
	 * @throws IBGenericException
	 */
	public String getSingleFromAccount () throws IBGenericException
	{
		String m_sFromAccount = "";
		Map<String, IBDynaBean> m_mFromAccountListMap = null;
		Iterator<Entry<String, IBDynaBean>> m_oIter = null;
		Map.Entry m_oFromAccountMapEntry = null;
		try
		{
			m_mFromAccountListMap = (LinkedHashMap<String, IBDynaBean>) IBUtils.getActiveNormalAccountBean (this, getIBDynaBean (), false);
			
			m_oIter = (Iterator<Entry<String,IBDynaBean>>) m_mFromAccountListMap.entrySet ().iterator ();
			if (m_oIter.hasNext ())
			{
				m_oFromAccountMapEntry = (Map.Entry) m_oIter.next ();
				m_sFromAccount = (String) m_oFromAccountMapEntry.getKey ();
			}
		}
		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);
		}
		return m_sFromAccount;
	}

}
