package com.red.circle.wallet.infra.gateway;

import com.google.common.collect.Sets;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.inner.endpoint.material.props.BadgeBackpackClient;
import com.red.circle.other.inner.endpoint.material.props.BadgeSourceClient;
import com.red.circle.other.inner.model.dto.material.BadgeBackpackDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.endpoint.user.region.UserRegionClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.reigon.UserRegionDTO;
import com.red.circle.wallet.domain.gateway.FreightShipGateway;
import com.red.circle.wallet.infra.enums.FreightGoldToUsdRatioEnum;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 货运代理, 出货 实现.
 *
 * @author pengliang on 2023/12/25
 */
@Component
@RequiredArgsConstructor
public class FreightShipGatewayImpl implements FreightShipGateway {


  private final UserRegionClient userRegionClient;
  private final UserProfileClient userProfileClient;
  private final BadgeSourceClient badgeSourceClient;
  private final BadgeBackpackClient badgeBackpackClient;

  @Override
  public void sendFreightShipBadge(Long userId, BigDecimal balance) {

    // 余额如果大于1250则发送货运代理徽章
    Long freightBadgeId = ResponseAssert.requiredSuccess(
        badgeSourceClient.getFreightBadgeId()
    );
    if (Objects.isNull(freightBadgeId)) {
      return;
    }
    if (balance.compareTo(BigDecimal.valueOf(1250)) < 0) {
      return;
    }

    badgeBackpackClient.deleteUserBadge(userId, freightBadgeId);

    // 获得正在佩戴中的徽章
    List<BadgeBackpackDTO> useBadges = ResponseAssert.requiredSuccess(
        badgeBackpackClient.listUseBadgeBackpackByUserId(userId)
    );

    if (CollectionUtils.isEmpty(useBadges) || useBadges.size() < 5) {
      badgeBackpackClient.activationPermanentAndUse(userId, freightBadgeId);
      userProfileClient.removeCacheAll(userId);
      return;
    }

    //卸下部分佩戴中的徽章
    Set<Long> ids = Sets.newHashSet();
    for (int i = 0; i < useBadges.size(); i++) {
      if (i > 3) {
        ids.add(useBadges.get(i).getId());
      }
    }

    badgeBackpackClient.removeWearByIds(ids);
    //将当前徽章带上
    badgeBackpackClient.activationPermanentAndUse(userId, freightBadgeId);
    userProfileClient.removeCacheAll(userId);
  }


  @Override
  public void removeFreightShipBadge(Long userId, BigDecimal balance) {

    // 余额如果等于0则回收货运代理徽章
    Long freightBadgeId = ResponseAssert.requiredSuccess(
        badgeSourceClient.getFreightBadgeId()
    );
    if (Objects.isNull(freightBadgeId)) {
      return;
    }
    if (balance.compareTo(BigDecimal.valueOf(0)) > 0) {
      return;
    }
    badgeBackpackClient.deleteUserBadge(userId, freightBadgeId);
    userProfileClient.removeCacheAll(userId);
  }

  @Override
  public int getFreightGoldToUsdRatio(Long userId) {
    UserRegionDTO region = getUserRegionSetting(userId);
    if (region == null) {
      return 1;
    }
    int ratio = FreightGoldToUsdRatioEnum.getRatio(region.getSysOrigin());
    /*
    // 20230-40-02 刘占洋需求： 阿拉伯和土耳其地区，货运代理给用户充值的计算比例由现在的750调整到1200
    boolean isProcessRegion = isRegion(List.of("AR", "TR"), region.getRegionCode());
    if (Objects.equals(SysOriginPlatformEnum.ASWAT.name(), region.getSysOrigin())
        && isProcessRegion) {
      return 1200;
    }

    // 20230-05-03 刘占洋需求： 这几个地区的货运代理卖金币那，现在最低是750帮我调整到440，然后480金币计算成1美金累计充值
    if (isRegion(List.of("NG1", "PH1", "NG", "PH", "GHA"), region.getRegionCode())) {
      return 440;
    }
    */
    return ratio;
  }

  @Override
  public int getFreightGoldToUsdRatioCumulativeRecharge(Long userId) {
    /*
    UserRegionDTO region = getUserRegionSetting(userId);
    // 20230-06-04 刘占洋需求： 尼日利亚和菲利宾货运代理卖金币那，按照900金币=1$计算到个人用户累计充值
    if (isRegion(List.of("NG1", "NG", "PH1", "PH"), region.getRegionCode())) {
      return 900;
    }
    // 20230-05-03 刘占洋需求： 这几个地区的货运代理卖金币那，现在最低是750帮我调整到440，然后480金币计算成1美金累计充值
    if (isRegion(List.of("NG1", "PH1", "NG", "PH", "GHA"), region.getRegionCode())) {
      return 480;
    }
     */
    return getFreightGoldToUsdRatio(userId);
  }

  private UserRegionDTO getUserRegionSetting(Long userId) {
    return ResponseAssert.requiredSuccess(
        userRegionClient.getRegion(userId));
  }

  private boolean isRegion(List<String> regions, String regionCode) {
    if (CollectionUtils.isEmpty(regions) || StringUtils.isBlank(regionCode)) {
      return false;
    }
    for (String region : regions) {
      if (StringUtils.equalsIgnoreCase(region, regionCode)) {
        return true;
      }
    }

    return false;
  }
}
