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

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import my.com.kfh.enterprise.services.common.CommonConstants;
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.hibernate.tables.IbIcmClassCode;
import my.com.kfh.hibernate.utils.HibernateICMUtils;
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.ServiceInfo;

public class IBIcmPlacementConfirmServices extends IBIcmGenericServices
{
	private static Class c_oClass = IBIcmPlacementConfirmServices.class;
	private IBDynaBean m_oIcmPlacementView = null;
	private boolean isStaff = false;
	private String m_sAmount = null;
	private BigDecimal m_dAmount;
	
	
	public IBIcmPlacementConfirmServices(IBSessionObjects m_oIBSessionObjects) {
		super(m_oIBSessionObjects);
	}
	
	public void executeServices() throws IBGenericException {
		String m_sMethodName = "executeServices";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);

		
		IBDynaBean m_oAccountBean = null;
		String m_sFromAccount = null;
		String m_sFromAccountDisplay = null;
		Map m_oFromAccountMap = new LinkedHashMap();

		String m_sAmountDisplay = null;
		String m_sIsoCurrency = null;
		String m_sAvailableBalance = null;
		String m_sTerms = null;
		String m_sPeriod = "Month(s)";
		int m_iTerms = 0;

		String m_sInstructionMaturity = null;
		String m_sInstructionMaturityDisplay = null;
		Map m_oInstructionMaturityMap = new HashMap ();

		try
		{
			m_oIcmPlacementView = (IBDynaBean) getIBDynaBean ().get (ICMConstants.ICM_PLACEMENT_VIEW);
			if (m_oIcmPlacementView == null)
			{
				m_oIcmPlacementView = IBDynaBeanManager.createIBDynaBean (ICMConstants.ICM_PLACEMENT_VIEW, getIBDynaBean ().getActionMapping ());
			}
			
			m_sFromAccount = (String)m_oIcmPlacementView.get(ICMConstants.FROM_ACCOUNT);
			
			isStaff = (Boolean)m_oIcmPlacementView.get(ICMConstants.IS_STAFF);
			
			m_sTerms = (String) getIBDynaBean().get(ICMConstants.TERM);
			m_iTerms = DataValidator.isEmptyString (m_sTerms) ? 0 : Integer.parseInt(m_sTerms);
			
			m_oFromAccountMap = (Map)m_oIcmPlacementView.get (ICMConstants.FROM_ACCOUNT_MAP);
			m_oAccountBean = (IBDynaBean)m_oFromAccountMap.get (m_sFromAccount);
			m_sFromAccountDisplay = DataValidator.isEmptyString (m_sFromAccount) ? (String)m_oAccountBean.get (CommonConstants.ACCOUNT_NICKNAME) : m_sFromAccount.concat (" ").concat ((String)m_oAccountBean.get (CommonConstants.ACCOUNT_NICKNAME));
			
			m_sIsoCurrency = (String)m_oAccountBean.get (CommonConstants.ISO_CURRENCY);
			m_sIsoCurrency = DataValidator.isEmptyString (m_sIsoCurrency) ? CurrencyCode.MALAYSIA_RINGGIT : m_sIsoCurrency;
			
			m_sAvailableBalance = (String)m_oAccountBean.get (CommonConstants.AVAILABLE_BALANCE);
			m_sAvailableBalance = FormatterUtils.getAmountInNumeric (m_sAvailableBalance);
			
			m_sAmount = (String)m_oIcmPlacementView.get(ICMConstants.PRINCIPAL_AMOUNT);
//			m_dAmount = DataValidator.isEmptyString (m_sAmount) ? BigDecimal.valueOf(0.00) : BigDecimal.valueOf(Double.parseDouble (m_sAmount));
			m_sAmountDisplay = DataValidator.isEmptyString (m_sAmount) ? "0" : FormatterUtils.formatMoneyDisplay (m_sAmount, m_sIsoCurrency, true);
			
//			verifyMinPlacement (m_sIsoCurrency, m_sAvailableBalance, isStaff, m_dAmount);
			
			m_sInstructionMaturity = (String)getIBDynaBean ().get (ICMConstants.MATURITY);
			m_oInstructionMaturityMap = (Map)m_oIcmPlacementView.get (ICMConstants.INST_MAT_OPTIONS_MAP);
			m_sInstructionMaturityDisplay = (String)m_oInstructionMaturityMap.get (m_sInstructionMaturity);
			
			if (m_iTerms == 1)
			{
				m_sTerms = m_sTerms.concat(" "+ICMConstants.PERIOD_MONTH);
			}
			else
			{
				m_sTerms = m_sTerms.concat(" "+ICMConstants.PERIOD_MONTHS);
			}
			
			List<IbIcmClassCode> classList = HibernateICMUtils.getAccountTypeMap(ICMConstants.CAMPAIGN, isStaff);
			
			if (classList == null || classList.size() == 0){
				classList = HibernateICMUtils.getAccountTypeMap(ICMConstants.NORMAL, isStaff);
			}
			
			IbIcmClassCode classCode = getClassCode(classList, m_sAmount, isStaff);
			
			m_oIcmPlacementView.set (ICMConstants.TERM_DISPLAY,m_sTerms );
			m_oIcmPlacementView.set (ICMConstants.PERIOD,m_sPeriod);
			m_oIcmPlacementView.set (ICMConstants.TERM_DISPLAY,m_sTerms);
			m_oIcmPlacementView.set (ICMConstants.FROM_ACCOUNT_DISPLAY, m_sFromAccountDisplay);
			m_oIcmPlacementView.set (ICMConstants.ISO_CURRENCY, m_sIsoCurrency);
			m_oIcmPlacementView.set (ICMConstants.AMOUNT_DISPLAY, m_sAmountDisplay);
			m_oIcmPlacementView.set (ICMConstants.INST_MAT_DISPLAY, m_sInstructionMaturityDisplay);
			m_oIcmPlacementView.set (ICMConstants.PAY_METHOD, classCode.getPaymentMethod());
			m_oIcmPlacementView.set (ICMConstants.RENEWAL_METHOD, classCode.getRenewalMethod());
			
			getIBDynaBean ().set (ICMConstants.ICM_PLACEMENT_VIEW, m_oIcmPlacementView);
			
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
			IBDynaBean m_oServiceInfoBean = (IBDynaBean) m_oIcmPlacementView.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);
				String beneName = getIBSessionObjects().getVisitor().getName();
				m_oSMSViewBean.set(CommonConstants.AMOUNT, m_sAmountDisplay);
				if(!DataValidator.isEmptyString(beneName)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, beneName.length()>18?beneName.substring(0, 18):beneName);
				}
				getIBSessionObjects().setHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN, m_oSMSViewBean);
			}
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
		}
		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);
		
	}
	

	

	

}
