package com.red.circle.other.app.inner.service.user.region.impl;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.ResponseErrorCode;
import com.red.circle.other.app.inner.convertor.user.RegionConfigInnerConvertor;
import com.red.circle.other.app.inner.service.user.region.RegionAssistConfigClientService;
import com.red.circle.other.infra.database.mongo.entity.user.region.SysRegionAssistConfig;
import com.red.circle.other.infra.database.mongo.service.user.region.SysRegionAssistConfigService;
import com.red.circle.other.infra.database.mongo.service.user.region.SysRegionConfigService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.other.inner.model.cmd.user.RegionAssistConfigQryCmd;
import com.red.circle.other.inner.model.cmd.user.SysRegionAssistCmd;
import com.red.circle.other.inner.model.dto.user.reigon.SysRegionAssistConfigDTO;
import com.red.circle.other.inner.enums.user.RegionAssistTypeEnum;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 区域辅助数据 实现.
 *
 * @author pengliang on 2023/9/15
 */
@Service
@RequiredArgsConstructor
public class RegionAssistConfigClientServiceImpl implements RegionAssistConfigClientService {

  private final SysRegionConfigService regionConfigService;
  private final RegionConfigInnerConvertor regionConfigInnerConvertor;
  private final SysRegionAssistConfigService sysRegionAssistConfigService;

  @Override
  public String getAssistConfigData(RegionAssistTypeEnum type, String regionId) {
    return Optional.ofNullable(sysRegionAssistConfigService.getRegionAssistConfig(type, regionId))
        .map(SysRegionAssistConfig::getData)
        .orElse(StringUtils.EMPTY);
  }

  @Override
  public void add(SysRegionAssistCmd config) {
    sysRegionAssistConfigService.add(regionConfigInnerConvertor.toSysRegionAssistConfig(config));
  }

  @Override
  public void update(SysRegionAssistCmd config) {
    ResponseAssert.isTrue(ResponseErrorCode.REQUEST_PARAMETER_ERROR,
        StringUtils.isNotBlank(config.getId()));
    sysRegionAssistConfigService.update(regionConfigInnerConvertor.toSysRegionAssistConfig(config));
  }

  @Override
  public void deleteById(String id, Long updateUser) {
    sysRegionAssistConfigService.deleteById(id, updateUser);
  }

  @Override
  public List<SysRegionAssistConfigDTO> listByCondition(RegionAssistConfigQryCmd query) {

    List<SysRegionAssistConfig> configs = sysRegionAssistConfigService.listByCondition(query);
    if (CollectionUtils.isEmpty(configs)) {
      return List.of();
    }

    Map<String, String> mapRegionName = getMapRegionName(configs);
    if (CollectionUtils.isEmpty(mapRegionName)) {
      return List.of();
    }

    return configs.stream()
        .map(config -> {
          SysRegionAssistConfigDTO vo = regionConfigInnerConvertor.toSysRegionAssistConfigDTO(
              config);
          vo.setRegionName(mapRegionName.get(vo.getRegionId()));
          vo.setTypeName(RegionAssistTypeEnum.valueOf(vo.getType()).getDesc());
          return vo;
        })
        .collect(Collectors.toList());
  }

  private Map<String, String> getMapRegionName(List<SysRegionAssistConfig> configs) {
    return regionConfigService.mapRegionName(
        configs.stream().map(SysRegionAssistConfig::getRegionId).collect(Collectors.toSet()));
  }

}
