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

import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.enterprise.services.ib115_gold.constant.GoldConstants;
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.web.utils.DataValidator;
import my.com.kfh.web.utils.FormatterUtils;
import my.com.kfh.web.utils.IBDynaBean;
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.ServiceInfoExtend;
import my.com.kfh.web.utils.constant.ServiceInfo;

public class IBGoldSellConfirmServices extends IBGenericGoldServices{
	private static Class<IBGoldSellNextServices> c_oClass = IBGoldSellNextServices.class;
	IBDynaBean m_oGoldSellView = null;


	Double m_dAmountToBeCredit=null;
	Double m_dCurrentGram=null;
	Double m_dAvailableGram=null;
	Double m_dNewGram=null;
	Double m_dGoldPrice=null;

	public IBGoldSellConfirmServices(IBSessionObjects m_oIBSessionObjects) {
		super(m_oIBSessionObjects);
	}
	
	public void calculateGoldPrice() throws IBGenericException{
		String m_sMethodName = "calculateGoldPrice";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);
		Double m_dGram=null;

		
		
		try{
			m_oGoldSellView=(IBDynaBean)getIBDynaBean().get(GoldConstants.VIEW_BEAN_GOLD_SELL);
			m_dGram= new Double(getIBDynaBean().get(GoldConstants.FORM_BEAN_GRAM_AMOUNT).toString());
			m_dAmountToBeCredit=getTotalSellGoldAmount(m_dGram,getIBDynaBean().get(GoldConstants.FORM_BEAN_TO_ACCOUNT).toString());
			m_dGoldPrice=getGoldBuyPrice(getIBDynaBean().get(GoldConstants.FORM_BEAN_TO_ACCOUNT).toString());
			m_oGoldSellView.set(GoldConstants.VIEW_BEAN_BUY_PRICE, 
					FormatterUtils.
					formatMoneyDisplay(m_dGoldPrice.toString()
							, null, true));
			m_oGoldSellView.set(GoldConstants.VIEW_BEAN_AMOUNT_TO_BE_CREDIT,FormatterUtils.formatMoneyDisplay(m_dAmountToBeCredit.toString(), null, true));
			getIBDynaBean().set(GoldConstants.VIEW_BEAN_GOLD_SELL, m_oGoldSellView);
			
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
			IBDynaBean m_oServiceInfoBean = (IBDynaBean) m_oGoldSellView.get(ServiceInfoExtend.SERVICE_INFO_EXTEND);
			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, FormatterUtils.formatMoneyDisplay(m_dAmountToBeCredit.toString(), null, true));
				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
			
			IBLogger.logBSFNMethodLeavingMessage (c_oClass, m_sMethodName, m_oTimer);
		}
		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);
		}
	}	
}