package com.rjconsultores.ventaboletos.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.rjconsultores.ventaboletos.dao.GP_PricingOcupaAntecipaDAO;
import com.rjconsultores.ventaboletos.entidad.GP_Pricing;
import com.rjconsultores.ventaboletos.entidad.GP_PricingOcupaAntecipa;

@Repository("gp_pricingOcupacionDAO")
public class GP_PricingOcupaAntecipaHibernateDAO extends GenericHibernateDAO<GP_PricingOcupaAntecipa, Integer>
		implements GP_PricingOcupaAntecipaDAO {

	@Autowired
	public GP_PricingOcupaAntecipaHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
		setSessionFactory(factory);
	}

	@Override
	public List<GP_PricingOcupaAntecipa> obtenerTodos() {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, GP_Pricing.ATIVO));
		return c.list();
	}

	@Override
	public List<GP_PricingOcupaAntecipa> buscarOcupaAntecipaPricing(Integer pricingId) {
		return buscarOcupaAntecipaPricing(pricingId, GP_Pricing.ATIVO, GP_Pricing.INATIVO);
	}

	@Override
	public List<GP_PricingOcupaAntecipa> buscarOcupaAntecipaPricingAtivos(Integer pricingId) {
		return buscarOcupaAntecipaPricing(pricingId, GP_Pricing.ATIVO, null);
	}
	
	private List<GP_PricingOcupaAntecipa> buscarOcupaAntecipaPricing(Integer pricingId, Integer ativo, Integer inativo) {
		Criteria c = getSession().createCriteria(getPersistentClass());
		
		List<Integer> pricingStatus = new ArrayList<Integer>();
		if(ativo != null) {
			pricingStatus.add(ativo);
		}
		if(inativo != null) {
			pricingStatus.add(inativo);
		}
		
		c.add(Restrictions.in("activo", pricingStatus));
		c.add(Restrictions.eq("pricing.pricingId", pricingId));
		
		return c.list();
	}

	@Override
	public List<GP_PricingOcupaAntecipa> obtenerTodoPorPricing(GP_Pricing pricing) {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq("pricing.pricingId", pricing.getPricingId()));

		return c.list();
	} 
}
