package com.ecep.contract.service;

import com.ecep.contract.SpringApp;
import com.ecep.contract.VendorType;
import com.ecep.contract.converter.VendorTypeStringConverter;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.VendorTypeLocalViewModel;
import com.ecep.contract.vo.VendorTypeLocalVo;
import javafx.util.StringConverter;
import org.springframework.cache.annotation.*;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

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

@Service
@CacheConfig(cacheNames = "vendor-type")
public class VendorTypeService extends QueryService<VendorTypeLocalVo, VendorTypeLocalViewModel> {
    private final StringConverter<VendorTypeLocalVo> stringConverter = new VendorTypeStringConverter(this);

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

    @Cacheable(key = "'type-'+#p0.ordinal()", condition = "#p0 != null")
    public VendorTypeLocalVo findByType(VendorType type) {
        return findAll(ParamUtils.builder().equals("type", type).build(), Pageable.ofSize(1)).stream().findFirst()
                .orElse(null);
    }

    public VendorTypeLocalVo findByValue(String string) {
        return findAll(ParamUtils.builder().equals("value", string).build(), Pageable.ofSize(1)).stream().findFirst()
                .orElse(null);
    }

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

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

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

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

    @Cacheable
    public Map<VendorType, VendorTypeLocalVo> findAll(Locale locale) {
        return findAll(ParamUtils.builder().equals("lang", locale.toLanguageTag()).build(), Pageable.unpaged()).stream()
                .collect(Collectors.toMap(VendorTypeLocalVo::getType, Function.identity()));
    }
    
    /**
     * 根据语言标签和参数查找单个 VendorTypeLocalVo 对象
     * 
     * @param locale 语言区域
     * @param key    参数键
     * @param value  参数值
     * @return 查找到的 VendorTypeLocalVo 对象，未找到则返回 null
     */
    private VendorTypeLocalVo 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 VendorTypeLocalVo findByLocaleAndValue(Locale locale, String string) {
        return findOneByLang(locale, "value", string);
    }
    
    public VendorTypeLocalVo findByLocaleAndType(Locale locale, VendorType type) {
        return findOneByLang(locale, "type", type);
    }

    public StringConverter<VendorType> getTypeStringConverter() {
        return new StringConverter<>() {
            final VendorTypeService service = SpringApp.getBean(VendorTypeService.class);

            @Override
            public String toString(VendorType object) {
                if (object == null) {
                    return "";
                }
                VendorTypeLocalVo localVo = service.findByType(object);
                return localVo.getValue();
            }

            @Override
            public VendorType fromString(String string) {
                VendorTypeLocalVo localVo = service.findByValue(string);
                return localVo.getType();
            }
        };
    }
}
