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.DregolePianiFeriePk;

import it.micegroup.gestioneferie.configurazionepf.entity.DregolePianiFeriePk_;
import it.micegroup.gestioneferie.configurazionepf.criteria.DregolePianiFeriePkCriteria;
import it.micegroup.gestioneferie.configurazionepf.repository.DregolePianiFeriePkRepository;
import it.micegroup.voila2runtime.specification.FilterService;

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

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

@RequiredArgsConstructor
@Service
@Transactional
public class DregolePianiFeriePkServiceImpl extends BaseServiceImpl implements DregolePianiFeriePkService {

	private final DregolePianiFeriePkRepository dregolePianiFeriePkRepository;
	private final FilterService<DregolePianiFeriePk> filterService;

	// CHILD SERVICES
	private final DregolePianiFerieHService dregolePianiFerieHService;

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

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

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

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

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

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

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

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

	@Override
	public void delete(DregolePianiFeriePk entity) {
		dregolePianiFeriePkRepository.delete(entity);
	}

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

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

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

		return findById(dregolePianiFeriePk.getSequIdRegolePianiFeriePk());
	}

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

		if (criteria != null) {
			// Attribute filters
			if (criteria.getSequIdRegolePianiFeriePk() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getSequIdRegolePianiFeriePk(), DregolePianiFeriePk_.sequIdRegolePianiFeriePk));
			}
			if (criteria.getCodiRegola() != null) {
				specification = specification.and(filterService.generateStringSpecification(criteria.getCodiRegola(),
						DregolePianiFeriePk_.codiRegola));
			}
			if (criteria.getDescRegola() != null) {
				specification = specification.and(filterService.generateStringSpecification(criteria.getDescRegola(),
						DregolePianiFeriePk_.descRegola));
			}
			if (criteria.getFlagEliminato() != null) {
				specification = specification.and(filterService.generateStringSpecification(criteria.getFlagEliminato(),
						DregolePianiFeriePk_.flagEliminato));
			}
			if (criteria.getUtenteUltimaModifica() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getUtenteUltimaModifica(), DregolePianiFeriePk_.utenteUltimaModifica));
			}
			if (criteria.getDataUltimaModifica() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataUltimaModifica(), DregolePianiFeriePk_.dataUltimaModifica));
			}
			if (criteria.getDataInizioValidita() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataInizioValidita(), DregolePianiFeriePk_.dataInizioValidita));
			}
			if (criteria.getDataFineValidita() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataFineValidita(), DregolePianiFeriePk_.dataFineValidita));
			}

		}
		return specification;
	}

	@Override
	@Transactional()
	public DregolePianiFeriePk bulkUpdate(DregolePianiFeriePk dregolePianiFeriePk) {
		List<DregolePianiFerieH> updateTheDregolePianiFerieH = dregolePianiFeriePk.getTheDregolePianiFerieH().stream()
				.filter(child -> !child.isDeletedEntityState()).collect(Collectors.toList());
		List<DregolePianiFerieH> deleteTheDregolePianiFerieH = dregolePianiFeriePk.getTheDregolePianiFerieH().stream()
				.filter(GenericEntity::isDeletedEntityState).collect(Collectors.toList());

		dregolePianiFeriePk.setTheDregolePianiFerieH(updateTheDregolePianiFerieH);

		DregolePianiFeriePk update = this.update(dregolePianiFeriePk);

		deleteTheDregolePianiFerieH
				.forEach(child -> dregolePianiFerieHService.deleteById(child.getSequIdRegolePianiFerieH()));
		return update;
	}
}
