package vn.azteam.tpf.service;

import io.github.jhipster.service.QueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.azteam.tpf.domain.*;
import vn.azteam.tpf.repository.TBCodeRepository;
import vn.azteam.tpf.service.dto.TBCodeCriteria;
import vn.azteam.tpf.service.dto.TBCodeDTO;
import vn.azteam.tpf.service.mapper.TBCodeMapper;

import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;

@Service
@Transactional(readOnly = true)
public class TBCodeQueryService extends QueryService<TBCode> {

    private final Logger log = LoggerFactory.getLogger(TBCodeQueryService.class);
    private final TBCodeRepository tBCodeRepository;

    private final TBCodeMapper tBCodeMapper;

    public TBCodeQueryService(TBCodeRepository tBCodeRepository, TBCodeMapper tBCodeMapper) {
        this.tBCodeRepository = tBCodeRepository;
        this.tBCodeMapper = tBCodeMapper;
    }

    @Transactional(readOnly = true)
    public Page<TBCodeDTO> findByCriteria(TBCodeCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<TBCode> specification = createSpecification(criteria);
        return tBCodeRepository.findAll(specification, page)
            .map(tBCodeMapper::toDto);
    }

    private Specification<TBCode> initializeCropSpecification() {
        return (Specification<TBCode>) (root, query, cb) -> {
            final Join<TBCode, TBDetailUser> detailUserJoin = root.join(TBCode_.deletedBy, JoinType.LEFT);
            return cb.isNull(detailUserJoin.get(TBDetailUser_.id));
        };
    }

    /**
     * Function to convert TBCodeCriteria to a {@link Specification}
     */
    private Specification<TBCode> createSpecification(TBCodeCriteria criteria) {
        Specification<TBCode> specification = initializeCropSpecification();
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBCode_.id));
            }
//            if (criteria.getQrCode() != null) {
//                specification = specification.and(buildStringSpecification(criteria.getQrCode(), TBCrop_.qrCode));
//            }
//            if (criteria.getCode() != null) {
//                specification = specification.and(buildStringSpecification(criteria.getCode(), TBCrop_.code));
//            }
//            if (criteria.getAreaM2() != null) {
//                specification = specification.and(buildRangeSpecification(criteria.getAreaM2(), TBCrop_.areaM2));
//            }
//            if (criteria.getTbCropType() != null) {
//                specification = specification.and(buildSpecification(criteria.getTbCropType(),
//                    root -> root.join(TBCrop_.tbCropType, JoinType.LEFT).get(TBCropType_.id)));
//            }
//            if (criteria.getStartDate() != null) {
//                specification = specification.and(buildRangeSpecification(criteria.getStartDate(), TBCrop_.startDate));
//            }
//            if (criteria.getEndDate() != null) {
//                specification = specification.and(buildRangeSpecification(criteria.getEndDate(), TBCrop_.endDate));
//            }
//            if (criteria.getStatus() != null) {
//                specification = specification.and(buildStringSpecification(criteria.getStatus(), TBCrop_.status));
//            }
//            if (criteria.getDescription() != null) {
//                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBCrop_.description));
//            }
//            if (criteria.getAgeDayStartAt() != null) {
//                specification = specification.and(buildRangeSpecification(criteria.getAgeDayStartAt(), TBCrop_.ageDayStartAt));
//            }
//            if (criteria.getCreatedDate() != null) {
//                specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBCrop_.createdDate));
//            }
//            if (criteria.getModifiedDate() != null) {
//                specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBCrop_.modifiedDate));
//            }
//            if (criteria.getDeletedDate() != null) {
//                specification = specification.and(buildRangeSpecification(criteria.getDeletedDate(), TBCrop_.deletedDate));
//            }
//            if (criteria.getTbSuppliesId() != null) {
//                specification = specification.and(buildSpecification(criteria.getTbSuppliesId(),
//                    root -> root.join(TBCrop_.tbSupplies, JoinType.LEFT).get(TBSupplies_.id)));
//            }
//            if (criteria.getTbGuidelineId() != null) {
//                specification = specification.and(buildSpecification(criteria.getTbGuidelineId(),
//                    root -> root.join(TBCrop_.tbGuideline, JoinType.LEFT).get(TBGuideline_.id)));
//            }
//            if (criteria.getNetHouseId() != null) {
//                specification = specification.and(buildSpecification(criteria.getNetHouseId(),
//                    root -> root.join(TBCrop_.tbEntity, JoinType.LEFT).get(TBEntity_.id)));
//            }
//            if (criteria.getCreatedById() != null) {
//                specification = specification.and(buildSpecification(criteria.getCreatedById(),
//                    root -> root.join(TBCrop_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
//            }
//            if (criteria.getModifiedById() != null) {
//                specification = specification.and(buildSpecification(criteria.getModifiedById(),
//                    root -> root.join(TBCrop_.modifiedBy, JoinType.LEFT).get(TBDetailUser_.id)));
//            }
//            if (criteria.getDeletedById() != null) {
//                specification = specification.and(buildSpecification(criteria.getDeletedById(),
//                    root -> root.join(TBCrop_.deletedBy, JoinType.LEFT).get(TBDetailUser_.id)));
//            }
//            if (criteria.getTbDetailUserId() != null) {
//                specification = specification.and(buildSpecification(criteria.getTbDetailUserId(),
//                    root -> root.join(TBCrop_.tbDetailUsers, JoinType.LEFT).get(TBDetailUser_.id)));
//            }
        }
        return specification;
    }

}
