package com.javahonk.dao;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class InterestRateDaoHandler<T> implements InterestRateDao<T> {
	
	@Autowired
	private SessionFactory sessionFactoryotherDB;
	
	@Value("${query_getInterestRates}")
	private String query_getInterestRates;
	
	@Value("${query_getIntereestRateBasedOnRicAndDate}")
	private String query_getIntereestRateBasedOnRicAndDate;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> getInterestRates() {
		
		Session session = sessionFactoryotherDB.getCurrentSession();
		Query query = session.getNamedQuery(query_getInterestRates);
		List<T> equitySwapInterestRateList = query.list();
		return equitySwapInterestRateList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> getInterestRateBasedOnRicAndDate(String ricCode, LocalDate localDate) {
		
		Session session = sessionFactoryotherDB.getCurrentSession();
		Query query = session.getNamedQuery(query_getIntereestRateBasedOnRicAndDate);
		query.setParameter("ric_code", ricCode);
		query.setParameter("libor_rate_date", Integer.valueOf(localDate.format(DateTimeFormatter.BASIC_ISO_DATE)));
		
		List<T> equitySwapInterestRateList = query.list();
		
		return equitySwapInterestRateList;
	}
	
}
