package it.micegroup.gestioneferie.pianificazioneferie.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.pianificazioneferie.entity.DstatiRichApprovPf;

import it.micegroup.gestioneferie.pianificazioneferie.entity.DstatiRichApprovPfKey;

import it.micegroup.gestioneferie.pianificazioneferie.entity.DstatiRichApprovPf_;
import it.micegroup.gestioneferie.pianificazioneferie.criteria.DstatiRichApprovPfCriteria;
import it.micegroup.gestioneferie.pianificazioneferie.repository.DstatiRichApprovPfRepository;
import it.micegroup.voila2runtime.specification.FilterService;

import it.micegroup.gestioneferie.pianificazioneferie.entity.DstatiRichApprovPf_;
import it.micegroup.gestioneferie.pianificazioneferie.entity.DstatiRichApprovPfKey_;
import lombok.RequiredArgsConstructor;
import it.micegroup.gestioneferie.libs.service.BaseServiceImpl;
import it.micegroup.gestioneferie.pianificazioneferie.entity.DstatiRichApprovPfKey_;

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

@RequiredArgsConstructor
@Service
@Transactional
public class DstatiRichApprovPfServiceImpl extends BaseServiceImpl implements DstatiRichApprovPfService {

	private final DstatiRichApprovPfRepository dstatiRichApprovPfRepository;
	private final FilterService<DstatiRichApprovPf> filterService;

	// CHILD SERVICES
	private final DrichiesteApprovazPfService drichiesteApprovazPfService;

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

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

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

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

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.micegroup.gestioneferie.pianificazioneferie.service.GenericEntityService#
	 * findById(java.lang. Object)
	 */
	@Override
	@Transactional(readOnly = true)
	public Optional<DstatiRichApprovPf> findById(DstatiRichApprovPfKey id) {
		return dstatiRichApprovPfRepository.findByDstatiRichApprovPfKey(id);
	}

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

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

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

	@Override
	public void delete(DstatiRichApprovPf entity) {
		dstatiRichApprovPfRepository.delete(entity);
	}

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

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

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

		return findById(dstatiRichApprovPf.getDstatiRichApprovPfKey());
	}

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

		if (criteria != null) {
			// Attribute filters
			if (criteria.getSequIdStatoRichApprovPf() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getSequIdStatoRichApprovPf(), DstatiRichApprovPf_.sequIdStatoRichApprovPf));
			}
			if (criteria.getCodiStatoRichiestaApprov() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getCodiStatoRichiestaApprov(), DstatiRichApprovPf_.codiStatoRichiestaApprov));
			}
			if (criteria.getDescStatoRichiestaApprov() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getDescStatoRichiestaApprov(), DstatiRichApprovPf_.descStatoRichiestaApprov));
			}
			if (criteria.getFlagEliminato() != null) {
				specification = specification.and(filterService.generateStringSpecification(criteria.getFlagEliminato(),
						DstatiRichApprovPf_.flagEliminato));
			}
			if (criteria.getDataFineRecord() != null) {
				specification = specification.and(filterService.generateRangeSpecification(criteria.getDataFineRecord(),
						DstatiRichApprovPf_.dataFineRecord));
			}
			if (criteria.getDataUltimaModifica() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataUltimaModifica(), DstatiRichApprovPf_.dataUltimaModifica));
			}
			if (criteria.getDataInizioRecord() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataInizioRecord(), DstatiRichApprovPf_.dataInizioRecord));
			}
			if (criteria.getUtenteUltimaModifica() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getUtenteUltimaModifica(), DstatiRichApprovPf_.utenteUltimaModifica));
			}

			// CompositeKey Filters

		}
		return specification;
	}

	@Override
	@Transactional()
	public DstatiRichApprovPf bulkUpdate(DstatiRichApprovPf dstatiRichApprovPf) {
		List<DrichiesteApprovazPf> updateTheDrichiesteApprovazPf = dstatiRichApprovPf.getTheDrichiesteApprovazPf()
				.stream().filter(child -> !child.isDeletedEntityState()).collect(Collectors.toList());
		List<DrichiesteApprovazPf> deleteTheDrichiesteApprovazPf = dstatiRichApprovPf.getTheDrichiesteApprovazPf()
				.stream().filter(GenericEntity::isDeletedEntityState).collect(Collectors.toList());

		dstatiRichApprovPf.setTheDrichiesteApprovazPf(updateTheDrichiesteApprovazPf);

		DstatiRichApprovPf update = this.update(dstatiRichApprovPf);

		deleteTheDrichiesteApprovazPf
				.forEach(child -> drichiesteApprovazPfService.deleteById(child.getDrichiesteApprovazPfKey()));
		return update;
	}
}
