package com.red.circle.other.app.command.sys.query;

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.order.inner.endpoint.OrderPurchaseHistoryClient;
import com.red.circle.other.app.convertor.sys.SysConfigAppConvertor;
import com.red.circle.other.app.dto.clientobject.sys.BannerCO;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.infra.database.cache.entity.sys.BannerCache;
import com.red.circle.other.infra.database.cache.service.other.BannerCacheService;
import com.red.circle.other.infra.database.rds.entity.sys.BannerConfig;
import com.red.circle.other.infra.database.rds.service.sys.BannerConfigService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * banner图获取.
 *
 * @author pengliang on 2021/3/30
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class BannerQryExe {

  private final UserRegionGateway userRegionGateway;
  private final BannerCacheService bannerCacheService;
  private final BannerConfigService bannerConfigService;
  private final SysConfigAppConvertor sysConfigAppConvertor;
  private final OrderPurchaseHistoryClient orderPurchaseHistoryClient;

  public List<BannerCO> execute(AppExtCommand cmd, String bannerType) {

    String regionId = userRegionGateway.getRegionId(cmd.requiredReqUserId());
    ResponseAssert.notBlank(CommonErrorCode.REGION_NOT_SUPPORTED_ERROR, regionId);
      List<BannerCache> list = bannerCacheService.listBannerCacheBySysOrigin(cmd.requireReqSysOrigin(), () -> {
                  List<BannerConfig> configs = listEffectiveCache(cmd.requireReqSysOrigin());
                  if (CollectionUtils.isEmpty(configs)) {
                      return Lists.newArrayList();
                  }
                  return sysConfigAppConvertor.toListBannerCache(configs);
              }).stream().filter(
                      bannerCache -> StringUtils.isBlank(bannerCache.getRegions()) || bannerCache.getRegions()
                              .contains(regionId))
              .toList();
      Boolean isGoogleRecharge = orderPurchaseHistoryClient.checkIsPurchaseRechargeByUserId(cmd.requiredReqUserId()).getBody();
      log.info("用户ID = {} isGoogleRecharge:{}", cmd.requiredReqUserId(), isGoogleRecharge);
      if (StringUtils.isNotBlank(bannerType) && "WALLET".equals(bannerType) && !isGoogleRecharge) {
          return Lists.newArrayList();
      }
      // 钱包 banner 入口,只查询 bannerType
      if (StringUtils.isNotBlank(bannerType) && "WALLET".equals(bannerType) && isGoogleRecharge) {
          list = list.stream()
                  .filter(bannerCache -> bannerCache.getDisplayPosition().contains(bannerType))
                  .toList();
      }
      return sysConfigAppConvertor.toListBannerCO(list);

  }

  private List<BannerConfig> listEffectiveCache(String sysOrigin) {
    return bannerConfigService.listEffective(sysOrigin);
  }

}
