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.voila2runtime.filter.LongFilter;

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

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

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

import it.micegroup.gestioneferie.pianificazioneferie.entity.DrichiesteApprovazPf_;
import it.micegroup.gestioneferie.pianificazioneferie.criteria.DrichiesteApprovazPfCriteria;
import it.micegroup.gestioneferie.pianificazioneferie.repository.DrichiesteApprovazPfRepository;
import it.micegroup.voila2runtime.specification.FilterService;

import it.micegroup.gestioneferie.pianificazioneferie.entity.DrichiesteApprovazPf_;
import it.micegroup.gestioneferie.pianificazioneferie.entity.DrichiesteApprovazPfKey_;
import it.micegroup.gestioneferie.pianificazioneferie.entity.DstatiRichApprovPf_;
import it.micegroup.gestioneferie.pianificazioneferie.entity.DstatiRichApprovPfKey_;

import it.micegroup.gestioneferie.pianificazioneferie.entity.DpianiFerie_;
import it.micegroup.gestioneferie.pianificazioneferie.entity.DstatiPianoFerie_;
import it.micegroup.gestioneferie.pianificazioneferie.entity.DstatiPianoFerieKey_;

import org.springframework.data.domain.PageImpl;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import it.micegroup.gestioneferie.libs.service.BaseServiceImpl;
import it.micegroup.gestioneferie.pianificazioneferie.entity.DrichiesteApprovazPfKey_;

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

@RequiredArgsConstructor
@Service
@Transactional
public class DrichiesteApprovazPfServiceImpl extends BaseServiceImpl implements DrichiesteApprovazPfService {

	private final DrichiesteApprovazPfRepository drichiesteApprovazPfRepository;
	private final FilterService<DrichiesteApprovazPf> filterService;

	// CHILD SERVICES

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

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

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

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

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

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

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

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

	@Override
	public void delete(DrichiesteApprovazPf entity) {
		drichiesteApprovazPfRepository.delete(entity);
	}

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

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

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

		return findById(drichiesteApprovazPf.getDrichiesteApprovazPfKey());
	}

	@Override
	public Page<DrichiesteApprovazPf> findByTheDstatiRichApprovPf(DstatiRichApprovPf parentEntity, Pageable pageable) {
		return drichiesteApprovazPfRepository.findByTheDstatiRichApprovPf(parentEntity, pageable);
	}

	@Override
	public Page<DrichiesteApprovazPf> findByTheDpianiFerie(DpianiFerie parentEntity, Pageable pageable) {
		return drichiesteApprovazPfRepository.findByTheDpianiFerie(parentEntity, pageable);
	}

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

		if (criteria != null) {
			// Attribute filters
			if (criteria.getSequIdRichiestaApprovazPf() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getSequIdRichiestaApprovazPf(), DrichiesteApprovazPf_.sequIdRichiestaApprovazPf));
			}
			if (criteria.getCodiRichiestaApprovazione() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getCodiRichiestaApprovazione(), DrichiesteApprovazPf_.codiRichiestaApprovazione));
			}
			if (criteria.getDataRichiestaApprovazione() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataRichiestaApprovazione(), DrichiesteApprovazPf_.dataRichiestaApprovazione));
			}
			if (criteria.getFkPianoFerie() != null) {
				specification = specification.and(filterService.generateRangeSpecification(criteria.getFkPianoFerie(),
						DrichiesteApprovazPf_.fkPianoFerie));
			}
			if (criteria.getFkStatoRichApprovPf() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getFkStatoRichApprovPf(), DrichiesteApprovazPf_.fkStatoRichApprovPf));
			}
			if (criteria.getDataInizioRecord() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataInizioRecord(), DrichiesteApprovazPf_.dataInizioRecord));
			}
			if (criteria.getDataFineRecord() != null) {
				specification = specification.and(filterService.generateRangeSpecification(criteria.getDataFineRecord(),
						DrichiesteApprovazPf_.dataFineRecord));
			}
			if (criteria.getFlagEliminato() != null) {
				specification = specification.and(filterService.generateStringSpecification(criteria.getFlagEliminato(),
						DrichiesteApprovazPf_.flagEliminato));
			}
			if (criteria.getDataUltimaModifica() != null) {
				specification = specification.and(filterService.generateRangeSpecification(
						criteria.getDataUltimaModifica(), DrichiesteApprovazPf_.dataUltimaModifica));
			}
			if (criteria.getUtenteUltimaModifica() != null) {
				specification = specification.and(filterService.generateStringSpecification(
						criteria.getUtenteUltimaModifica(), DrichiesteApprovazPf_.utenteUltimaModifica));
			}

			// CompositeKey Filters

			if (criteria.getTheDstatiRichApprovPfObjectKey() != null) {
				DstatiRichApprovPf dstatiRichApprovPf = new DstatiRichApprovPf(
						criteria.getTheDstatiRichApprovPfObjectKey());

			}
			if (criteria.getTheDpianiFerieObjectKey() != null) {
				DpianiFerie dpianiFerie = new DpianiFerie(criteria.getTheDpianiFerieObjectKey());

				specification = specification
						.and(filterService.generateEqualsSpecification(dpianiFerie.getSequIdPianoFerie(),
								r -> r.get(DrichiesteApprovazPf_.theDpianiFerie).get(DpianiFerie_.sequIdPianoFerie)));
			}

		}
		return specification;
	}

	/*
	 * 
	 * @see it.micegroup.gestioneferie.pianificazioneferie.service.
	 * DrichiesteApprovazPfService#findDstatiRichApprovPfBytheDpianiFerie(it.
	 * micegroup.gestioneferie.pianificazioneferie.entity.DpianiFerie,
	 * org.springframework.data.domain.Pageable)
	 */
	@Override
	@Transactional(readOnly = true)
	public Page<DstatiRichApprovPf> findDstatiRichApprovPfByTheDpianiFerie(DpianiFerie dpianiFerie, Pageable pageable) {
		Page<DrichiesteApprovazPf> drichiesteApprovazPfPage = drichiesteApprovazPfRepository
				.findByTheDpianiFerie(dpianiFerie, pageable);
		List<DstatiRichApprovPf> content = drichiesteApprovazPfPage.getContent().stream()
				.map(DrichiesteApprovazPf::getTheDstatiRichApprovPf).collect(Collectors.toList());
		Page<DstatiRichApprovPf> result = new PageImpl<DstatiRichApprovPf>(content, pageable,
				drichiesteApprovazPfPage.getTotalElements());
		return result;
	}

	/*
	 * 
	 * @see it.micegroup.gestioneferie.pianificazioneferie.service.
	 * DrichiesteApprovazPfService#findDpianiFerieBytheDstatiRichApprovPf(it.
	 * micegroup.gestioneferie.pianificazioneferie.entity.DstatiRichApprovPf,
	 * org.springframework.data.domain.Pageable)
	 */
	@Override
	@Transactional(readOnly = true)
	public Page<DpianiFerie> findDpianiFerieByTheDstatiRichApprovPf(DstatiRichApprovPf dstatiRichApprovPf,
			Pageable pageable) {
		Page<DrichiesteApprovazPf> drichiesteApprovazPfPage = drichiesteApprovazPfRepository
				.findByTheDstatiRichApprovPf(dstatiRichApprovPf, pageable);
		List<DpianiFerie> content = drichiesteApprovazPfPage.getContent().stream()
				.map(DrichiesteApprovazPf::getTheDpianiFerie).collect(Collectors.toList());
		Page<DpianiFerie> result = new PageImpl<DpianiFerie>(content, pageable,
				drichiesteApprovazPfPage.getTotalElements());
		return result;
	}

	@Override
	@Transactional()
	public DrichiesteApprovazPf bulkUpdate(DrichiesteApprovazPf drichiesteApprovazPf) {

		DrichiesteApprovazPf update = this.update(drichiesteApprovazPf);

		return update;
	}
}
