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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamMember;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamProfile;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamMemberService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamProfileService;
import com.red.circle.other.infra.database.rds.entity.sys.EnumConfig;
import com.red.circle.other.infra.database.rds.entity.user.user.BaseInfo;
import com.red.circle.other.infra.database.rds.service.sys.EnumConfigService;
import com.red.circle.other.infra.database.rds.service.user.user.BaseInfoService;
import com.red.circle.other.inner.enums.team.TeamMemberRole;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.infra.database.mongo.entity.user.region.SysRegionConfig;
import com.red.circle.other.infra.database.mongo.service.user.region.SysRegionConfigService;

import java.util.*;

import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Component;

/**
 * 获得用户区域Metadata.
 *
 * @author pengliang on 2023/6/11
 */
@Component
@RequiredArgsConstructor
public class UserRegionMetadataQryExe {

  private final BaseInfoService baseInfoService;
  private final EnumConfigService enumConfigService;
  private final TeamMemberService teamMemberService;
  private final TeamProfileService teamProfileService;
  private final UserRegionGateway userRegionGateway;
  private final SysRegionConfigService sysRegionConfigService;

  public Map<String, String> execute(AppExtCommand cmd) {

    String regionId = userRegionGateway.getRegionId(cmd.requiredReqUserId());

    if (StringUtils.isBlank(regionId)) {
      return CollectionUtils.newHashMap();
    }

    Map<String, String> result = Optional.ofNullable(sysRegionConfigService.getById(regionId))
            .map(SysRegionConfig::getMetadata)
            .orElseGet(CollectionUtils::newHashMap);
    return getStringStringMap(cmd, result);
  }

  @Nullable
  private Map<String, String> getStringStringMap(AppExtCommand cmd, Map<String, String> result) {
    // 查询配置项
    EnumConfig enumConfig = enumConfigService.getByEnumConfig("INDONESIA_AGENT", cmd.getReqSysOrigin().getOrigin());
    if (enumConfig == null || StringUtils.isBlank(enumConfig.getVal())) {
      return result;
    }
    List<String> userAccounts = Arrays.asList(enumConfig.getVal().split(","));
    // 查询团队ID
    TeamMember teamMember = teamMemberService.getByMemberId(cmd.requiredReqUserId());
    if (teamMember == null) {
      return CollectionUtils.newHashMap();
    }
    if (!Objects.equals(teamMember.getRole(), TeamMemberRole.MEMBER)) {
      return result;
    }
    // 查询团队所属人
    TeamProfile teamProfile = teamProfileService.getById(teamMember.getTeamId());
    if (teamProfile == null) {
      return CollectionUtils.newHashMap();
    }
    BaseInfo baseInfo = baseInfoService.getById(teamProfile.getOwnUserId());
    if (baseInfo == null) {
      return CollectionUtils.newHashMap();
    }
    // 如果这个主播的代理属于配置里的代理,则移除
    if (userAccounts.contains(baseInfo.getAccount())) {
      result.remove("openExchangeGold");
      result.remove("openWithdraw");
    }
    return result;
  }

  public Map<String, String> executeDiamondMetadata(AppExtCommand cmd) {

    String regionId = userRegionGateway.getRegionId(cmd.requiredReqUserId());

    if (StringUtils.isBlank(regionId)) {
      return CollectionUtils.newHashMap();
    }

    return Optional.ofNullable(sysRegionConfigService.getById(regionId))
            .map(SysRegionConfig::getDiamondMetadata)
            .orElseGet(CollectionUtils::newHashMap);
  }

}
