package com.ecep.contract.ds.contract.service;

import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.context.annotation.Lazy;
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.util.StringUtils;

import com.ecep.contract.ContractFileType;
import com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.ds.contract.repository.ContractFileTypeLocalRepository;
import com.ecep.contract.model.ContractFileTypeLocal;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "contract-file-type")
public class ContractFileTypeService
        implements IEntityService<ContractFileTypeLocal>, QueryService<ContractFileTypeLocal> {
    @Lazy
    @Autowired
    private ContractFileTypeLocalRepository repository;

    @Override
    public Page<ContractFileTypeLocal> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<ContractFileTypeLocal> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }

        // field
        spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "lang", "value", "suggestFileName");
        return findAll(spec, pageable);
    }

    @Cacheable(key = "'all-'+#p0.getLanguage()")
    public Map<ContractFileType, ContractFileTypeLocal> findAll(Locale locale) {
        return repository.getCompleteMapByLocal(locale.getLanguage());
    }

    @Cacheable(key = "#p0")
    @Override
    public ContractFileTypeLocal findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Page<ContractFileTypeLocal> findAll(Specification<ContractFileTypeLocal> spec, Pageable pageable) {
        return repository.findAll(spec, pageable);
    }

    @Override
    public Specification<ContractFileTypeLocal> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("type"), "%" + searchText + "%"),
                    builder.like(root.get("suggestFileName"), "%" + searchText + "%"));
        };
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'all-'+#p0.getLang()")
    })
    @Override
    public void delete(ContractFileTypeLocal entity) {
        repository.delete(entity);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'all-'+#p0.getLang()")
    })
    @Override
    public ContractFileTypeLocal save(ContractFileTypeLocal entity) {
        return repository.save(entity);
    }

}
