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

import com.google.common.collect.Lists;
import com.red.circle.other.app.inner.service.user.region.UserRegionClientService;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.infra.convertor.user.UserRegionInfraConvertor;
import com.red.circle.other.infra.database.cache.service.user.SysRegionCacheService;
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.inner.enums.user.RegionAssistTypeEnum;
import com.red.circle.other.inner.model.dto.user.SysExchangeGoldTipDTO;
import com.red.circle.other.inner.model.dto.user.reigon.HostApplyQuitTeamCheckDTO;
import com.red.circle.other.inner.model.dto.user.reigon.RegionConfigDTO;
import com.red.circle.other.inner.model.dto.user.reigon.SysRegionAssistProbabilityDTO;
import com.red.circle.other.inner.model.dto.user.reigon.UserRegionDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.json.JacksonUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.springframework.stereotype.Service;

/**
 * 用户区域服务.
 *
 * @author pengliang on 2023/6/11
 */
@Service
@RequiredArgsConstructor
public class UserRegionClientServiceImpl implements UserRegionClientService {

  private final UserRegionGateway userRegionGateway;
  private final SysRegionCacheService sysRegionCacheService;
  private final UserRegionInfraConvertor userRegionInfraConvertor;
  private final SysRegionAssistConfigService sysRegionAssistConfigService;

  @Override
  public String getRegionCode(Long userId) {
    return userRegionGateway.getRegionCode(userId);
  }

  @Override
  public Map<Long, String> mapRegionCode(Set<Long> userIds) {
    return userRegionGateway.mapRegionCode(userIds);
  }

  @Override
  public String getRegionId(Long userId) {
    return userRegionGateway.getRegionId(userId);
  }

  @Override
  public UserRegionDTO getRegion(Long userId) {
    return userRegionGateway.getUserRegion(userId);
  }

  @Override
  public List<SysExchangeGoldTipDTO> listRegionExchangeGoldTip(Long userId, Boolean isCoinSeller) {
    String regionId = getRegionId(userId);
    if (StringUtils.isBlank(regionId)) {
      return Lists.newArrayList();
    }

    List<SysRegionAssistConfig> assistConfigs = sysRegionAssistConfigService
            .listByTypeByRegionId(isCoinSeller ? RegionAssistTypeEnum.COIN_SELLER_WITHDRAW_PROPORTION_TIPS : RegionAssistTypeEnum.WITHDRAW_PROPORTION_TIPS, regionId);
    if (CollectionUtils.isEmpty(assistConfigs)) {
      return Lists.newArrayList();
    }

    return assistConfigs.stream()
        .map(config -> JacksonUtils.readValue(config.getData(), SysExchangeGoldTipDTO.class))
        .filter(Objects::nonNull).collect(Collectors.toList());
  }

  @Override
  public List<SysExchangeGoldTipDTO> listRegionExchangeDiamondGoldTip(Long userId) {
    String regionId = getRegionId(userId);
    if (StringUtils.isBlank(regionId)) {
      return Lists.newArrayList();
    }

    List<SysRegionAssistConfig> assistConfigs = sysRegionAssistConfigService
            .listByTypeByRegionId(RegionAssistTypeEnum.WITHDRAW_PROPORTION_DIAMOND_TIPS, regionId);
    if (CollectionUtils.isEmpty(assistConfigs)) {
      return Lists.newArrayList();
    }

    return assistConfigs.stream()
            .map(config -> JacksonUtils.readValue(config.getData(), SysExchangeGoldTipDTO.class))
            .filter(Objects::nonNull).collect(Collectors.toList());
  }

  @Override
  public List<SysExchangeGoldTipDTO> listRegionWithdrawTip(Long userId) {
    String regionId = getRegionId(userId);
    if (StringUtils.isBlank(regionId)) {
      return Lists.newArrayList();
    }

    List<SysRegionAssistConfig> assistConfigs = sysRegionAssistConfigService
            .listByTypeByRegionId(RegionAssistTypeEnum.WITHDRAW_PROPORTION_DIAMOND_USD_TIPS, regionId);
    if (CollectionUtils.isEmpty(assistConfigs)) {
      return Lists.newArrayList();
    }

    return assistConfigs.stream()
            .map(config -> JacksonUtils.readValue(config.getData(), SysExchangeGoldTipDTO.class))
            .filter(Objects::nonNull).collect(Collectors.toList());
  }

  @Override
  public RegionConfigDTO getSysRegionConfigByUserId(Long userId) {
    return userRegionInfraConvertor.toRegionConfigDTO(
        userRegionGateway.getRegionConfigByUserId(userId)
    );
  }

  @Override
  public boolean checkEqRegion(Long userId, Long compareUserId) {
    return userRegionGateway.checkEqRegion(userId, compareUserId);
  }

  @Override
  public boolean checkBusinessRegion(Long userId, Long compareUserId) {
    return userRegionGateway.checkBusinessRegion(userId, compareUserId);
  }

  @Override
  public boolean checkBusinessRegionCode(String code, String compareCode) {
    return userRegionGateway.checkBusinessRegionCode(code, compareCode);
  }

  @Override
  public boolean checkOpenDailyAutoSalary(String regionId, String sysOrigin) {
    return userRegionGateway.checkOpenDailyAutoSalary(regionId, sysOrigin);
  }

  @Override
  public HostApplyQuitTeamCheckDTO getHostApplyQuitTeamCheckByUserId(Long userId) {
    return userRegionGateway.getHostApplyQuitTeamCheckByUserId(userId);
  }

  @Override
  public HostApplyQuitTeamCheckDTO getHostApplyQuitTeamCheckByRegionId(String regionId) {
    return userRegionGateway.getHostApplyQuitTeamCheckByRegionId(regionId);
  }

  @Override
  public ImmutablePair<String, BigDecimal> getAssistRoomContributionActivityRatio(Long userId) {
    return userRegionGateway.getAssistRoomContributionActivityRatio(userId);
  }

  @Override
  public SysRegionAssistProbabilityDTO getAssistRoomContributionRatio(Long userId) {
    return userRegionGateway.getAssistRoomContributionActivityRatioDTO(userId);
  }

  @Override
  public BigDecimal getResidueTargetExchangeRatio(String regionId) {
    return userRegionGateway.getResidueTargetExchangeRatio(regionId);
  }

  @Override
  public BigDecimal getAssistGiftToOtherGoldRatio(String regionId) {
    return userRegionGateway.getAssistGiftToOtherGoldRatio(regionId);
  }

  @Override
  public BigDecimal getAssistGiftToOwnGoldRatio(String regionId) {
    return userRegionGateway.getAssistGiftToOwnGoldRatio(regionId);
  }

  @Override
  public Map<Long, RegionConfigDTO> mapUserRegionConfig(String sysOrigin, Set<Long> userIds) {
    return userRegionInfraConvertor.toMapRegionConfigDTO(
        userRegionGateway.mapUserRegionConfig(sysOrigin, userIds));
  }

  @Override
  public List<RegionConfigDTO> listSysRegionConfig(String sysOrigin) {
    return userRegionInfraConvertor.toListRegionConfigDTO(
        sysRegionCacheService.getCache(sysOrigin));
  }

}
