package com.red.circle.other.app.command.user.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.other.app.convertor.user.RedPacketInviteAppConvertor;
import com.red.circle.other.app.dto.clientobject.user.invite.InviteRedPacketUserAuthCO;
import com.red.circle.other.app.dto.clientobject.user.invite.InviteRedPacketUserCheckAuthCO;
import com.red.circle.other.app.dto.clientobject.user.invite.InviteUserSucceedCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.common.user.RedPacketInviteUserCommon;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
import com.red.circle.other.infra.database.rds.entity.user.user.InviteRedPacketRecord;
import com.red.circle.other.infra.database.rds.entity.user.user.RelationshipFriend;
import com.red.circle.other.infra.database.rds.service.user.user.InviteRedPacketRecordService;
import com.red.circle.other.infra.database.rds.service.user.user.RelationshipFriendService;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.enums.config.EnumConfigKey;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 查询当前用户邀新红包游戏权限.
 * </p>
 *
 * @author pengshigang
 * @since 2023-12-23
 */
@Service
@AllArgsConstructor
@Slf4j
public class InviteNewRedPacketAuthorQryExe {

  private final UserProfileGateway userProfileGateway;
  private final EnumConfigCacheService enumConfigCacheService;
  private final RedPacketInviteUserCommon redPacketInviteUserCommon;
  private final RelationshipFriendService relationshipFriendService;
  private final RedPacketInviteAppConvertor redPacketInviteAppConvertor;
  private final InviteRedPacketRecordService inviteRedPacketRecordService;

  public InviteRedPacketUserAuthCO execute(AppExtCommand cmd) {

    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND,
        userProfileGateway.getByUserId(cmd.getReqUserId()));

    InviteRedPacketUserCheckAuthCO checkAuthCO = redPacketInviteAppConvertor.
        toInviteUserCheckAuthCO(redPacketInviteUserCommon.getUserCheckAuth(
            cmd.getReqUserId(), null));
    InviteRedPacketUserAuthCO authCO = new InviteRedPacketUserAuthCO();
    authCO.setRemovableRedPacket(checkAuthCO.getRemovableRedPacket());
    authCO.setClosed(checkAuthCO.getClosed());
    authCO.setInviteCodeEffective(checkAuthCO.getInviteCodeEffective());
    authCO.setShowInviteRedPacket(checkAuthCO.getShowInviteRedPacket());
    return authCO;
  }


  public List<InviteUserSucceedCO> getInviteUserSucceed(AppExtCommand cmd){
    UserProfile profile=  userProfileGateway.getByUserId(cmd.getReqUserId());
    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND,
            profile );
    InviteRedPacketUserCheckAuthCO checkAuthCO = redPacketInviteAppConvertor.
            toInviteUserCheckAuthCO(redPacketInviteUserCommon.getUserCheckAuth(
                    cmd.getReqUserId(), null));
    if (!checkAuthCO.getBayWindow()
            && checkAuthCO.getShowInviteRedPacket()) {
      return getInviteUserSucceed(profile);
    }
    return Lists.newArrayList();
  }

  private List<InviteUserSucceedCO> getInviteUserSucceed(UserProfile userProfile){
    //获取好友列表
    Set<Long> relationshipFriends = relationshipFriendService.flowSize1000(
            userProfile.getId(), null,null).stream().map(RelationshipFriend::getFriendUserId).collect(
            Collectors.toSet());
    //好友红包提现集合取3条随机
    List<InviteRedPacketRecord> runningWaters = new ArrayList<>();
    if (!relationshipFriends.isEmpty()) {
      for (Long i : relationshipFriends) {
        runningWaters.addAll(inviteRedPacketRecordService.getUserIdList(i, 1,userProfile.getOriginSys()));
      }
    }
    if (!runningWaters.isEmpty() && runningWaters.size() > 3) {
      Collections.shuffle(runningWaters);
      runningWaters = runningWaters.stream().distinct().limit(3).collect(Collectors.toList());
    }
    List<InviteRedPacketRecord> userIdList = inviteRedPacketRecordService.getUserIdList(null, 7, userProfile.getOriginSys());
    Collections.shuffle(userIdList);
    runningWaters.addAll(userIdList);
    InviteUserSucceedCO map = null;
    UserProfile userProfile1 = null;
    List<InviteUserSucceedCO> mapList = new ArrayList<>();
    //去重取前7条
    List<InviteRedPacketRecord> distinctList = runningWaters.stream()
            .distinct()
            .limit(7)
            .collect(Collectors.toList());

    for (InviteRedPacketRecord inviteRedPacketRecord : distinctList) {
      userProfile1 = userProfileGateway.getByUserId(inviteRedPacketRecord.getUserId());
      map = new InviteUserSucceedCO();
      map.setId(inviteRedPacketRecord.getUserId());
      map.setUserNickname(userProfile1.getUserNickname());
      map.setUserAvatar(userProfile1.getUserAvatar());
      map.setMoney(inviteRedPacketRecord.getCurrentAmount());
      // 添加更多需要转换的字段
      mapList.add(map);
    }
    return mapList;
  }
}
