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.gestioneferie.configurazionepf.entity.DpercAutPianFeriePk;

import it.micegroup.gestioneferie.configurazionepf.entity.DpercAutPianFeriePk_;
import it.micegroup.gestioneferie.configurazionepf.criteria.DpercAutPianFeriePkCriteria;
import it.micegroup.gestioneferie.configurazionepf.repository.DpercAutPianFeriePkRepository;
import it.micegroup.voila2runtime.specification.FilterService;

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

import java.util.stream.Collectors;
import it.micegroup.gestioneferie.configurazionepf.entity.DpercAutPianFerieH;
import it.micegroup.voila2runtime.entity.GenericEntity;
import it.micegroup.voila2runtime.exception.BusinessException;
import net.sf.jasperreports.engine.JRException;

@RequiredArgsConstructor
@Service
@Transactional
public class DpercAutPianFeriePkServiceImpl extends BaseServiceImpl implements DpercAutPianFeriePkService {

	private final DpercAutPianFeriePkRepository dpercAutPianFeriePkRepository;
	private final FilterService<DpercAutPianFeriePk> filterService;

	// CHILD SERVICES
	private final DpercAutPianFerieHService dpercAutPianFerieHService;

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

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.configurazionepf.service.GenericEntityService#
	 * findAll()
	 */
	@Override
	@Transactional(readOnly = true)
	public List<DpercAutPianFeriePk> findAll() {
		return dpercAutPianFeriePkRepository.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<DpercAutPianFeriePk> findByCriteria(DpercAutPianFeriePkCriteria criteria, Pageable pageable) {
		final Specification<DpercAutPianFeriePk> specification = createSpecification(criteria);
		return dpercAutPianFeriePkRepository.findAll(specification, pageable);
	}

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

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

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

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

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

	@Override
	public void delete(DpercAutPianFeriePk entity) {
		dpercAutPianFeriePkRepository.delete(entity);
	}

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

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

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

		return findById(dpercAutPianFeriePk.getSequIdPercAutPianFeriePk());
	}

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

		if (criteria != null) {
			// Attribute filters
			if (criteria.getSequIdPercAutPianFeriePk() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getSequIdPercAutPianFeriePk(), DpercAutPianFeriePk_.sequIdPercAutPianFeriePk));
			}
			if (criteria.getFlagEliminato() != null) {
				specification = specification.and(filterService.generateStringSpecification(criteria.getFlagEliminato(),
						DpercAutPianFeriePk_.flagEliminato));
			}
			if (criteria.getUtenteUltimaModifica() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getUtenteUltimaModifica(), DpercAutPianFeriePk_.utenteUltimaModifica));
			}
			if (criteria.getDataUltimaModifica() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataUltimaModifica(), DpercAutPianFeriePk_.dataUltimaModifica));
			}

		}
		return specification;
	}

	@Override
	@Transactional()
	public DpercAutPianFeriePk bulkUpdate(DpercAutPianFeriePk dpercAutPianFeriePk) {
		List<DpercAutPianFerieH> updateTheDpercAutPianFerieH = dpercAutPianFeriePk.getTheDpercAutPianFerieH().stream()
				.filter(child -> !child.isDeletedEntityState()).collect(Collectors.toList());
		List<DpercAutPianFerieH> deleteTheDpercAutPianFerieH = dpercAutPianFeriePk.getTheDpercAutPianFerieH().stream()
				.filter(GenericEntity::isDeletedEntityState).collect(Collectors.toList());

		dpercAutPianFeriePk.setTheDpercAutPianFerieH(updateTheDpercAutPianFerieH);

		DpercAutPianFeriePk update = this.update(dpercAutPianFeriePk);

		deleteTheDpercAutPianFerieH
				.forEach(child -> dpercAutPianFerieHService.deleteById(child.getSequIdPercAutPianFerieH()));
		return update;
	}
}
