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

import com.google.common.collect.Lists;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.clientobject.activity.RoomContributionCountCO;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.infra.database.mongo.entity.activity.RoomContributionActivityCount;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfile;
import com.red.circle.other.infra.database.mongo.service.activity.RoomContributionActivityCountService;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.inner.model.dto.user.reigon.SysRegionAssistProbabilityDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils;
import com.red.circle.tool.core.num.NumUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 房间贡献查询 .
 * </p>
 *
 * @author pengshigang
 * @since 2022-10-27
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RoomContributionActivityQueryCmdExe {

  private final UserRegionGateway userRegionGateway;
  private final RoomProfileManagerService roomProfileManagerService;
  private final RoomContributionActivityCountService roomContributionActivityCountService;

  public RoomContributionCountCO execute(AppRoomIdCmd cmd) {
//    Long roomId = roomProfileManagerService.getRoomId(cmd.getReqUserId());
//    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, roomId);

    RoomProfile roomProfile = roomProfileManagerService.getProfileById(cmd.getRoomId());
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, roomProfile);
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, roomProfile.getId());

    // 非本周且没有领取且积分大于最低领取要求的数据集合
    RoomContributionActivityCount activityCount = roomContributionActivityCountService.getThisWeekData(cmd.getRoomId());

    if (activityCount == null) {
      return new RoomContributionCountCO()
          .setThisWeekIntegral(NumUtils.formatLong(0L))
          .setThisWeekAmount(BigDecimal.ZERO);
    }

    return new RoomContributionCountCO()
        .setThisWeekIntegral(NumUtils.formatBigDecimal(activityCount.getContributionValue()))
        .setThisWeekAmount(activityCount.getContributionValue());
  }

  private BigDecimal getAvailableAmount(AppRoomIdCmd cmd,
      SysRegionAssistProbabilityDTO activityRatio,
      List<RoomContributionActivityCount> activityCounts) {
    if (CollectionUtils.isEmpty(activityCounts)) {
      return BigDecimal.ZERO;
    }

    return getAvailableAmount(cmd, activityRatio.getRatio(), activityCounts.stream()
        .map(RoomContributionActivityCount::getContributionValue)
        .filter(Objects::nonNull)
        .toList());
  }

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


  private BigDecimal getAvailableAmount(AppRoomIdCmd cmd, BigDecimal ratio,
      List<BigDecimal> integralList) {
    //加一个区域值 不同的区域可能会有不同的开始计算点
    String regionCode = userRegionGateway.getRegionCode(cmd.getReqUserId());
    return roomContributionActivityCountService
        .getAvailableAmount(cmd.requireReqSysOriginEnum(),regionCode, integralList, ratio);
  }

  private SysRegionAssistProbabilityDTO getAssistRoomContributionActivityRatio(
      RoomProfile roomProfile) {
    return userRegionGateway.getAssistRoomContributionActivityRatioDTO(roomProfile.getUserId());
  }

  private ImmutablePair<String, BigDecimal> getThisWeekData(AppRoomIdCmd cmd,
      BigDecimal ratio) {

    RoomContributionActivityCount count = roomContributionActivityCountService
        .getThisWeekData(cmd.getRoomId());
    if (Objects.isNull(count)) {
      return ImmutablePair.of("0", BigDecimal.ZERO);
    }

    if (Objects.isNull(count.getContributionValue())) {
      return ImmutablePair.of("0", BigDecimal.ZERO);
    }
    return ImmutablePair.of(NumUtils.formatBigDecimal(count.getContributionValue()),
        getAvailableAmount(cmd, ratio, Lists.newArrayList(count.getContributionValue())));
  }

}
