package info.bukova.isspst.services.numberseries;

import info.bukova.isspst.data.NumberSeries;
import info.bukova.isspst.data.Season;
import info.bukova.isspst.services.AbstractService;

import info.bukova.isspst.services.settings.SeasonService;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class NumberSeriesServiceImpl extends AbstractService<NumberSeries> implements NumberSeriesService
{
	@Autowired
	private SeasonService seasonService;

	@Transactional
	@Override
	public NumberSeries getNumberSerie(String module) {
		return getNumberSerieForSeason(module, seasonService.getActive());
	}

	@Transactional
	@Override
	public NumberSeries getNumberSerieForSeason(String module, Season season) {
		Query q = dao.getQuery("from NumberSeries where module = :m and season = :s");
		q.setParameter("m", module);
		q.setParameter("s", season);
		return (NumberSeries) q.uniqueResult();
	}

	@Transactional
	@Override
	public void increase(NumberSeries ns) {
		ns.setNumber(ns.getNumber() + 1);
		this.update(ns);
	}

	@Transactional
	@Override
	public List<NumberSeries> getSeriesForSeason(Season season) {
		Query q = dao.getQuery("from NumberSeries where season = :s");
		q.setParameter("s", season);
		return q.list();
	}
}
