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

import it.micegroup.gestioneferie.configurazionepf.entity.DconfigPaPianiFerie_;
import it.micegroup.gestioneferie.configurazionepf.criteria.DconfigPaPianiFerieCriteria;
import it.micegroup.gestioneferie.configurazionepf.repository.DconfigPaPianiFerieRepository;
import it.micegroup.voila2runtime.specification.FilterService;

import it.micegroup.gestioneferie.configurazionepf.entity.DconfigPaPianiFerie_;
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.gestioneferie.configurazionepf.entity.DconfigPaPfUoH;
import it.micegroup.voila2runtime.entity.GenericEntity;
import it.micegroup.voila2runtime.exception.BusinessException;
import net.sf.jasperreports.engine.JRException;

@RequiredArgsConstructor
@Service
@Transactional
public class DconfigPaPianiFerieServiceImpl extends BaseServiceImpl implements DconfigPaPianiFerieService {

	private final DconfigPaPianiFerieRepository dconfigPaPianiFerieRepository;
	private final FilterService<DconfigPaPianiFerie> filterService;

	// CHILD SERVICES
	private final DpercAutPianFerieHService dpercAutPianFerieHService;
	private final DconfigPaPfUoHService dconfigPaPfUoHService;

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

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

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

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

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

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

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

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

	@Override
	public void delete(DconfigPaPianiFerie entity) {
		dconfigPaPianiFerieRepository.delete(entity);
	}

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

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

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

		return findById(dconfigPaPianiFerie.getSequIdConfigPaPianoFerie());
	}

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

		if (criteria != null) {
			// Attribute filters
			if (criteria.getSequIdConfigPaPianoFerie() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getSequIdConfigPaPianoFerie(), DconfigPaPianiFerie_.sequIdConfigPaPianoFerie));
			}
			if (criteria.getCodiConfigPaPianoFerie() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getCodiConfigPaPianoFerie(), DconfigPaPianiFerie_.codiConfigPaPianoFerie));
			}
			if (criteria.getDescConfigPaPianoFerie() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getDescConfigPaPianoFerie(), DconfigPaPianiFerie_.descConfigPaPianoFerie));
			}
			if (criteria.getFkAmministrazionePk() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getFkAmministrazionePk(), DconfigPaPianiFerie_.fkAmministrazionePk));
			}
			if (criteria.getFkTipoOrganigramma() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getFkTipoOrganigramma(), DconfigPaPianiFerie_.fkTipoOrganigramma));
			}
			if (criteria.getDataUltimaModifica() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataUltimaModifica(), DconfigPaPianiFerie_.dataUltimaModifica));
			}
			if (criteria.getUtenteUltimaModifica() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getUtenteUltimaModifica(), DconfigPaPianiFerie_.utenteUltimaModifica));
			}
			if (criteria.getFlagEliminato() != null) {
				specification = specification.and(filterService.generateStringSpecification(criteria.getFlagEliminato(),
						DconfigPaPianiFerie_.flagEliminato));
			}
			if (criteria.getDataInizioRecord() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataInizioRecord(), DconfigPaPianiFerie_.dataInizioRecord));
			}
			if (criteria.getDataFineRecord() != null) {
				specification = specification.and(filterService.generateRangeSpecification(criteria.getDataFineRecord(),
						DconfigPaPianiFerie_.dataFineRecord));
			}

		}
		return specification;
	}

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

		dconfigPaPianiFerie.setTheDpercAutPianFerieH(updateTheDpercAutPianFerieH);
		List<DconfigPaPfUoH> updateTheDconfigPaPfUoH = dconfigPaPianiFerie.getTheDconfigPaPfUoH().stream()
				.filter(child -> !child.isDeletedEntityState()).collect(Collectors.toList());
		List<DconfigPaPfUoH> deleteTheDconfigPaPfUoH = dconfigPaPianiFerie.getTheDconfigPaPfUoH().stream()
				.filter(GenericEntity::isDeletedEntityState).collect(Collectors.toList());

		dconfigPaPianiFerie.setTheDconfigPaPfUoH(updateTheDconfigPaPfUoH);

		DconfigPaPianiFerie update = this.update(dconfigPaPianiFerie);

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