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

import com.broadvision.data.common.TableData;

import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.enterprise.services.ib105_billpayment.constant.BillPaymentContants;
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.web.utils.DataValidator;
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.ServiceInfo;

public class IBFavPayeeEditConfirmServices extends IBGenericPaymentServices 
{
	private static Class<IBFavPayeeEditConfirmServices> c_oClass = IBFavPayeeEditConfirmServices.class;
	
	public IBFavPayeeEditConfirmServices(IBSessionObjects sessionObjects) 
	{
		super(sessionObjects);
		// TODO Auto-generated constructor stub
	}
	
	public void execute() throws IBGenericException
	{
		String m_sMethodName = "execute";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);

		String m_sBillAcctNo = null;
		String m_sPrevBillAcctNo = null;
		String m_sPayeeCode = null;
		Boolean isCheckDigit = null;
		String m_sCheckDigitIndicator = null;
		IBDynaBean m_oPayeeDetailsBean = null;
		IBDynaBean m_oFavPayeeEditViewBean = null;
		
		TableData m_oTableData = null;
		boolean isDIGI = false;
		
		try
		{
			m_oFavPayeeEditViewBean = (IBDynaBean) getIBDynaBean().get(BillPaymentContants.FAV_PAYEE_EDIT_VIEW_BEAN);
			
			m_oPayeeDetailsBean = (IBDynaBean) m_oFavPayeeEditViewBean.get(BillPaymentContants.PAYEE_DETAILS_VIEW_BEAN);
			m_sPayeeCode = (String) m_oPayeeDetailsBean.get(BillPaymentContants.VBEAN_PAYEE_CODE);
			m_sCheckDigitIndicator = (String) m_oPayeeDetailsBean.get(BillPaymentContants.VBEAN_CHECK_DIGIT_INDI);
			m_sBillAcctNo = (String) getIBDynaBean().get(BillPaymentContants.FBEAN_BILL_ACCOUNT_NO);
			m_sPrevBillAcctNo = (String) getIBDynaBean().get(BillPaymentContants.FORM_PREV_BILL_ACCT_NO);
			
			m_oTableData = getFavPayeeTableData(false);
			
			if(!m_sPrevBillAcctNo.equalsIgnoreCase(m_sBillAcctNo))
			{
				if (m_oTableData != null && m_oTableData.size () > 0)
				{
					String m_sBillAcctNo1 = "";
					for (int i = 0; i <m_oTableData.size();  i++)
					{
						m_sBillAcctNo1 = (m_oTableData.getStringValue (i, BillPaymentContants.COL_BILL_ACCOUNT_NUMBER)).trim ();

						if (m_sBillAcctNo.equals (m_sBillAcctNo1))
						{
							throw IBErrorHandler.createBusinessException (IBBusinessException.BILL_ACCOUNT_FAV_ADD,
									BillPaymentContants.ERROR_ID_ALREADY_EXIST);
						}
					}
				}
			}
			String SHORTNAME = "";
			
			SHORTNAME = (String)m_oPayeeDetailsBean.get(BillPaymentContants.VBPAYEE_SHORT_NAME);
			if(SHORTNAME.equalsIgnoreCase("DIGIKL"))
			{
				isDIGI=true;
				m_sCheckDigitIndicator = "No";
			}
			if(isDIGI)
			{
				int length =0;
				String checkBillAccNo = "";
				boolean isNumeric = false;
				
				isNumeric = isNumeric(m_sBillAcctNo);
				length = m_sBillAcctNo.length();
				checkBillAccNo = m_sBillAcctNo.substring(0, 1);
				if(isNumeric)
				{
					//valid
					for (int j = 0; j < m_sBillAcctNo.length(); j++)
					{
						int code = m_sBillAcctNo.charAt(j);

						if (!(code >= 48 && code <= 57))
						{
							//error
							throw IBErrorHandler.createBusinessException(IBBusinessException.BILL_PAYMENT, BillPaymentContants.ERR_DIGI_BILL_ACC_NO);
						}
						
					}
				}
				else
				{
					throw IBErrorHandler.createBusinessException(IBBusinessException.BILL_PAYMENT, BillPaymentContants.ERR_DIGI_BILL_ACC_NO);
				}
				if(length != 13)
				{
					throw IBErrorHandler.createBusinessException(IBBusinessException.BILL_PAYMENT, BillPaymentContants.ERR_DIGI_BILL_ACC_LENGTH);
				}
				else
				{
					//valid
				}
				if(checkBillAccNo.equalsIgnoreCase("1"))
				{
					//valid
					System.out.println(m_sBillAcctNo.substring(0, 1));
				}
				if(checkBillAccNo.equalsIgnoreCase("2"))
				{
					//valid

					System.out.println(m_sBillAcctNo.substring(0, 1));
				}
				else if (!checkBillAccNo.equalsIgnoreCase("1") && !checkBillAccNo.equalsIgnoreCase("2"))
				{
						throw IBErrorHandler.createBusinessException(IBBusinessException.BILL_PAYMENT, BillPaymentContants.ERR_DIGI_BILL_ACC_NO);
				}
			}
			if(CommonConstants.isTrue (m_sCheckDigitIndicator))
			{
				if(!DataValidator.isEmptyString(m_sBillAcctNo))
				{
					try
					{
						isCheckDigit = BillPaymentDBUtils.BillerCheckDigit(this, m_sBillAcctNo, m_sPayeeCode);
						if(!isCheckDigit)
						{
							throw IBErrorHandler.createBusinessException (IBBusinessException.BILL_PAYMENT, "invalid.bill.no");
						}
					}
					catch (Exception e)
					{
						throw IBErrorHandler.createBusinessException (IBBusinessException.BILL_PAYMENT, "invalid.bill.no");
					}
				}
			}
			IBDynaBean m_oServiceInfoBean = (IBDynaBean) m_oFavPayeeEditViewBean.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(SHORTNAME)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, SHORTNAME.length()>25?SHORTNAME.substring(0, 25):SHORTNAME);
				}

				getIBSessionObjects().setHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN, m_oSMSViewBean);
			}
		}
		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);
	}
	public static boolean isNumeric(String s) {
	    try { 
	        double d = Double.parseDouble(s);
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    // only got here if we didn't return false
	    return true;
	}
}
