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

import com.ecep.contract.IEntityService;
import com.ecep.contract.ProjectFileType;
import com.ecep.contract.QueryService;
import com.ecep.contract.constant.ServiceConstant;
import com.ecep.contract.ds.project.repository.ProjectFileTypeLocalRepository;
import com.ecep.contract.model.ProjectFileTypeLocal;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;
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 java.util.Locale;
import java.util.Map;

@Lazy
@Service
@CacheConfig(cacheNames = "project-file-type")
public class ProjectFileTypeService implements IEntityService<ProjectFileTypeLocal>, QueryService<ProjectFileTypeLocal> {

    @Lazy
    @Autowired
    private ProjectFileTypeLocalRepository repository;

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

        if (paramsNode.has("type")) {
            spec = SpecificationUtils.and(spec, (root, query, builder) -> builder.equal(root.get("type"), ProjectFileType.valueOf(paramsNode.get("type").asText())));
        }

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

    @Override
    public ProjectFileTypeLocal findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

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

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

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

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

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