package com.red.circle.other.app.command.activity;

import com.alibaba.nacos.common.utils.StringUtils;
import com.google.common.collect.Lists;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.order.inner.endpoint.UserFreightRechargeRecordClient;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.activity.ActivityUserCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.wallet.inner.endpoint.freight.FreightBalanceClient;
import com.red.circle.wallet.inner.model.dto.UserFreightBalanceDTO;
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.springframework.stereotype.Component;

/**
 * 货运代理活动.
 *
 * @author pengshigang on 2022/01/11
 */
@Component
@RequiredArgsConstructor
public class FreightUserListCmdExe {

  private final UserRegionGateway userRegionGateway;
  private final UserProfileGateway userProfileGateway;
  private final FreightBalanceClient freightBalanceClient;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final UserFreightRechargeRecordClient userFreightRechargeRecordClient;

  public List<ActivityUserCO> execute(AppExtCommand cmd) {

    String selfRegionCode = userRegionGateway.getRegionCode(cmd.requiredReqUserId());

    List<UserFreightBalanceDTO> balances = freightBalanceClient.listFreightByBalanceGt1K(
        cmd.requireReqSysOriginEnum()).getBody();

    // 孟加拉货运代理需要充值达到2000$
//    if (StringUtils.equalsIgnoreCase(selfRegionCode, "BD")
//        && Objects.equals(cmd.requireReqSysOriginEnum(), SysOriginPlatformEnum.ASWAT)) {
//      List<Long> userIds = userFreightRechargeRecordClient.listNowMonthBySysOrigin(
//              cmd.requireReqSysOrigin())
//          .getBody();
//
//      if (CollectionUtils.isEmpty(userIds)) {
//        return Lists.newArrayList();
//      }
//      balances = balances.stream().filter(balance -> userIds.contains(balance.getUserId()))
//          .toList();
//    }

    if (CollectionUtils.isEmpty(balances)) {
      return Lists.newArrayList();
    }

    Map<Long, UserProfileDTO> userMap = userProfileAppConvertor.toMapUserProfileDTO(
        userProfileGateway.mapByUserIds(getUserIds(balances))
    );

    List<ActivityUserCO> userList = balances.stream().map(balance -> {
      UserProfileDTO user = userMap.get(balance.getUserId());
      if (Objects.isNull(user)) {
        return null;
      }

      return new ActivityUserCO()
          .setUserId(balance.getUserId())
          .setCountryName(user.getCountryName())
          .setRegionCode(userRegionGateway.getRegionCode(balance.getUserId()))
          .setAccount(user.actualAccount())
          .setUserAvatar(user.getUserAvatar())
          .setUserNickname(user.getUserNickname())
          .setLanguage(userProfileGateway.getLanguage(user.getId()));
    }).filter(Objects::nonNull).toList();

    return userList.stream().filter(user -> Objects.equals(selfRegionCode, user.getRegionCode()))
        .toList();
  }


  private Set<Long> getUserIds(List<UserFreightBalanceDTO> balances) {
    return balances.stream().map(UserFreightBalanceDTO::getUserId).collect(Collectors.toSet());
  }

}
