package com.fitbank.common.security;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import com.fitbank.common.ApplicationDates;
import com.fitbank.common.exception.FitbankException;
import com.fitbank.common.hb.UtilHB;
import com.fitbank.dto.financial.FinancialRequest;
import com.fitbank.hb.persistence.acco.Taccount;

/**
 * Clase que implemeta a vadicacion de montos de transacciones por rol de usuario. 
 * @author Fitbank
 * @version 2.0
 */
public class AmountValidator {
	
	private static String HQL_SENTENCE = " from com.fitbank.hb.persistence.safe.Tschedulerole role " +
	" where role.pk.crol = :role " +
	" and role.pk.cpersona_compania = :cia " +
	" and role.pk.csubsistema = :subsystem " +
	" and role.pk.cgrupoproducto = :productgroup " +
	" and role.pk.cproducto = :product " +
	" and role.pk.ctipobanca = :banktype " +
	" and role.pk.cestatuscuenta = :status " +
	" and role.pk.ctransaccion = :transaction " +
	" and role.pk.versiontransaccion = :version " +
	" and role.pk.cmoneda = :currency " +
	" and role.pk.diasemana = :day " +
	" and :time between role.horadesde and role.horahasta " +
	" and role.montomaximo >= :amount "+
	" and fhasta = :v_timestamp ";
	
	public AmountValidator(Taccount pAccount,BigDecimal pAmount,FinancialRequest frq) throws Exception{
		this.validate(pAccount,pAmount,frq);
	}
	
	@SuppressWarnings("unchecked")
	private void validate(Taccount pAccount,BigDecimal pAmount,FinancialRequest frq) throws Exception{
		//TransactionData transactionData = TransactionHelper.getTransactionData();
	//	Date accountindate = transactionData.getAccountingdate();
		GregorianCalendar gcal = new GregorianCalendar();
		//FIXME
		gcal.setTime(ApplicationDates.getInstance().getDataBaseTimestamp());
		DecimalFormat dayf=new DecimalFormat("##");
		String day = dayf.format(gcal.get(Calendar.DAY_OF_WEEK));
		SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
		String time =sdf.format(ApplicationDates.getInstance().getDataBaseDate());
		UtilHB utiHB = new UtilHB();
		utiHB.setSentence(HQL_SENTENCE);
		utiHB.setInteger("role",frq.getRole());
		utiHB.setInteger("cia",pAccount.getPk().getCpersona_compania());
		utiHB.setString("subsystem",pAccount.getCsubsistema());
		utiHB.setString("productgroup",pAccount.getCgrupoproducto());
		utiHB.setString("product",pAccount.getCproducto());
		utiHB.setString("banktype",pAccount.getCtipobanca());
		utiHB.setString("status",pAccount.getCestatuscuenta());
		utiHB.setString("transaction",frq.getTransaction());
		utiHB.setString("version",frq.getVersion());
		utiHB.setString("currency",pAccount.getCmoneda());
		utiHB.setInteger("day",new Integer(day));
		utiHB.setString("time",time);
		utiHB.setTimestamp("v_timestamp", ApplicationDates.getDefaultExpiryTimestamp());
		utiHB.setBigDecimal("amount",pAmount);
		try {
			@SuppressWarnings("unused")
			List l  = utiHB.getList();
		} catch (FitbankException e) {
			if(e.getCode().compareTo("HB004") == 0){
				throw new FitbankException("FIN016","TRANSACCION NO PERMITIDA PARA ROL {0} SUBSISTEMA {1} TRANSACCION {2} VERSION {3} TIPO DE BANCA {4} MONTO {5,number,$ ###,###,##0.00} DIA {6}",
						frq.getRole(),pAccount.getCsubsistema(),frq.getTransaction(),frq.getVersion(),pAccount.getCtipobanca(),pAmount,day);
			}else{
				throw e;
			}
		}
		
		
	}
	
}
