package it.micegroup.gestioneferie.configurazionepf.service;

import java.util.Map;
import java.util.List;
import java.util.Locale;
import java.util.HashMap;
import java.util.Optional;
import java.util.ArrayList;
import java.util.Collection;
import java.util.ResourceBundle;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import org.springframework.context.i18n.LocaleContextHolder;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import it.micegroup.voila2runtime.filter.LongFilter;

import it.micegroup.gestioneferie.configurazionepf.entity.DconfigPaPfUoH;

import it.micegroup.gestioneferie.configurazionepf.entity.DconfigPaPianiFerie;

import it.micegroup.gestioneferie.configurazionepf.entity.DconfigPaPfUoH_;
import it.micegroup.gestioneferie.configurazionepf.criteria.DconfigPaPfUoHCriteria;
import it.micegroup.gestioneferie.configurazionepf.repository.DconfigPaPfUoHRepository;
import it.micegroup.voila2runtime.specification.FilterService;

import it.micegroup.gestioneferie.configurazionepf.entity.DconfigPaPfUoH_;
import it.micegroup.gestioneferie.configurazionepf.entity.DconfigPaPianiFerie_;
import lombok.RequiredArgsConstructor;
import it.micegroup.gestioneferie.libs.service.BaseServiceImpl;

import it.micegroup.voila2runtime.exception.BusinessException;
import net.sf.jasperreports.engine.JRException;

@RequiredArgsConstructor
@Service
@Transactional
public class DconfigPaPfUoHServiceImpl extends BaseServiceImpl implements DconfigPaPfUoHService {

	private final DconfigPaPfUoHRepository dconfigPaPfUoHRepository;
	private final FilterService<DconfigPaPfUoH> filterService;

	// CHILD SERVICES

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LogManager.getLogger(DconfigPaPfUoHServiceImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * findAll(org. springframework.data.domain.Pageable)
	 */
	@Override
	@Transactional(readOnly = true)
	public Page<DconfigPaPfUoH> findAll(Pageable pageable) {
		return dconfigPaPfUoHRepository.findAll(pageable);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * findAll()
	 */
	@Override
	@Transactional(readOnly = true)
	public List<DconfigPaPfUoH> findAll() {
		return dconfigPaPfUoHRepository.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * findByCriteria(java. lang.Object, org.springframework.data.domain.Pageable)
	 */
	@Override
	@Transactional(readOnly = true)
	public Page<DconfigPaPfUoH> findByCriteria(DconfigPaPfUoHCriteria criteria, Pageable pageable) {
		final Specification<DconfigPaPfUoH> specification = createSpecification(criteria);
		return dconfigPaPfUoHRepository.findAll(specification, pageable);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * findByCriteria(java. lang.Object)
	 */
	@Override
	@Transactional(readOnly = true)
	public List<DconfigPaPfUoH> findByCriteria(DconfigPaPfUoHCriteria criteria) {
		final Specification<DconfigPaPfUoH> specification = createSpecification(criteria);
		return dconfigPaPfUoHRepository.findAll(specification);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * findById(java.lang. Object)
	 */
	@Override
	@Transactional(readOnly = true)
	public Optional<DconfigPaPfUoH> findById(Long id) {
		return dconfigPaPfUoHRepository.findBySequIdConfigPaPfUoH(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * exists(java.lang. Object)
	 */
	@Override
	public boolean exists(Long id) {
		return dconfigPaPfUoHRepository.existsById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * insert(java. lang.Object)
	 */
	@Override
	public DconfigPaPfUoH insert(@Valid DconfigPaPfUoH entity) {
		return dconfigPaPfUoHRepository.save(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * update(java. lang.Object)
	 */
	@Override
	public DconfigPaPfUoH update(@Valid DconfigPaPfUoH entity) {
		return dconfigPaPfUoHRepository.save(entity);
	}

	@Override
	public void delete(DconfigPaPfUoH entity) {
		dconfigPaPfUoHRepository.delete(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * deleteById(java.lang. Object)
	 */
	@Override
	public void deleteById(Long id) {
		dconfigPaPfUoHRepository.deleteById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * deleteByIdIn(java.lang. Object)
	 */
	@Override
	public void deleteByIdIn(Collection<Long> ids) {
		dconfigPaPfUoHRepository.deleteByIdIn(ids);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * findByObjectKey(java.lang.String)
	 */
	@Override
	@Transactional(readOnly = true)
	public Optional<DconfigPaPfUoH> findByObjectKey(String id) {
		DconfigPaPfUoH dconfigPaPfUoH = new DconfigPaPfUoH(id);

		return findById(dconfigPaPfUoH.getSequIdConfigPaPfUoH());
	}

	@Override
	public Page<DconfigPaPfUoH> findByTheDconfigPaPianiFerie(DconfigPaPianiFerie parentEntity, Pageable pageable) {
		return dconfigPaPfUoHRepository.findByTheDconfigPaPianiFerie(parentEntity, pageable);
	}

	/**
	 * Convert DconfigPaPfUoHCriteria to a {@link Specification}
	 * 
	 * @param criteria
	 * @return
	 */
	private Specification<DconfigPaPfUoH> createSpecification(DconfigPaPfUoHCriteria criteria) {
		Specification<DconfigPaPfUoH> specification = Specification.where(null);

		if (criteria != null) {
			// Attribute filters
			if (criteria.getSequIdConfigPaPfUoH() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getSequIdConfigPaPfUoH(), DconfigPaPfUoH_.sequIdConfigPaPfUoH));
			}
			if (criteria.getFlagValidoUoSubordinate() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getFlagValidoUoSubordinate(), DconfigPaPfUoH_.flagValidoUoSubordinate));
			}
			if (criteria.getFkConfigPaPianoFerie() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getFkConfigPaPianoFerie(), DconfigPaPfUoH_.fkConfigPaPianoFerie));
			}
			if (criteria.getFkUnitaFunzionale() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getFkUnitaFunzionale(), DconfigPaPfUoH_.fkUnitaFunzionale));
			}
			if (criteria.getFkUnitaOrganizzativaPk() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getFkUnitaOrganizzativaPk(), DconfigPaPfUoH_.fkUnitaOrganizzativaPk));
			}
			if (criteria.getUtenteUltimaModifica() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getUtenteUltimaModifica(), DconfigPaPfUoH_.utenteUltimaModifica));
			}
			if (criteria.getFlagEliminato() != null) {
				specification = specification.and(filterService.generateStringSpecification(criteria.getFlagEliminato(),
						DconfigPaPfUoH_.flagEliminato));
			}
			if (criteria.getDataInizioValidita() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataInizioValidita(), DconfigPaPfUoH_.dataInizioValidita));
			}
			if (criteria.getDataFineValidita() != null) {
				specification = specification.and(filterService
						.generateRangeSpecification(criteria.getDataFineValidita(), DconfigPaPfUoH_.dataFineValidita));
			}
			if (criteria.getDataUltimaModifica() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataUltimaModifica(), DconfigPaPfUoH_.dataUltimaModifica));
			}

			if (criteria.getTheDconfigPaPianiFerieObjectKey() != null) {
				DconfigPaPianiFerie dconfigPaPianiFerie = new DconfigPaPianiFerie(
						criteria.getTheDconfigPaPianiFerieObjectKey());

				specification = specification.and(
						filterService.generateEqualsSpecification(dconfigPaPianiFerie.getSequIdConfigPaPianoFerie(),
								r -> r.get(DconfigPaPfUoH_.theDconfigPaPianiFerie)
										.get(DconfigPaPianiFerie_.sequIdConfigPaPianoFerie)));
			}

		}
		return specification;
	}

	@Override
	@Transactional()
	public DconfigPaPfUoH bulkUpdate(DconfigPaPfUoH dconfigPaPfUoH) {

		DconfigPaPfUoH update = this.update(dconfigPaPfUoH);

		return update;
	}
}
