package com.ecep.contract.service;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ecep.contract.ProjectFileType;
import com.ecep.contract.converter.ProjectFileTypeStringConverter;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.ProjectFileTypeLocalViewModel;
import com.ecep.contract.vo.ProjectFileTypeLocalVo;

import javafx.util.StringConverter;

@Service
@CacheConfig(cacheNames = "project-file-type")
public class ProjectFileTypeService extends QueryService<ProjectFileTypeLocalVo, ProjectFileTypeLocalViewModel> {
    private final StringConverter<ProjectFileTypeLocalVo> stringConverter = new ProjectFileTypeStringConverter(this);

    @Cacheable(key = "#p0")
    @Override
    public ProjectFileTypeLocalVo findById(Integer id) {
        return super.findById(id);
    }

    @Cacheable(key = "'all'")
    @Override
    public List<ProjectFileTypeLocalVo> findAll() {
        return super.findAll();
    }

    @Caching(put = { @CachePut(key = "#p0.id"), @CachePut(key = "'all'") })
    @Override
    public ProjectFileTypeLocalVo save(ProjectFileTypeLocalVo entity) {
        return super.save(entity);
    }

    @Caching(put = { @CachePut(key = "#p0.id"), @CachePut(key = "'all'") })
    @Override
    public void delete(ProjectFileTypeLocalVo entity) {
        super.delete(entity);
    }

    @Cacheable
    public Map<ProjectFileType, ProjectFileTypeLocalVo> findAll(Locale locale) {
        return findAll(ParamUtils.builder().equals("lang", locale.toLanguageTag()).build(), Pageable.unpaged()).stream()
                .collect(Collectors.toMap(ProjectFileTypeLocalVo::getType, Function.identity()));
    }

    @Override
    public StringConverter<ProjectFileTypeLocalVo> getStringConverter() {
        return stringConverter;
    }

    /**
     * 根据语言标签和参数查找单个 ProjectFileTypeLocalVo 对象
     * 
     * @param locale 语言区域
     * @param key    参数键
     * @param value  参数值
     * @return 查找到的 ProjectFileTypeLocalVo 对象，未找到则返回 null
     */
    private ProjectFileTypeLocalVo findOneByLang(Locale locale, String key, Object value) {
        return findAll(ParamUtils.builder().equals("lang", locale.toLanguageTag()).equals(key, value).build(),
                Pageable.ofSize(1))
                .stream().findFirst().orElse(null);
    }

    public ProjectFileTypeLocalVo findByLocaleAndValue(Locale locale, String string) {
        return findOneByLang(locale, "value", string);
    }
    
    public ProjectFileTypeLocalVo findByLocaleAndType(Locale locale, ProjectFileType type) {
        return findOneByLang(locale, "type", type);
    }
}