package com.red.circle.other.infra.annotation.aspect;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.framework.dto.QuantityDTO;
import com.red.circle.other.domain.gateway.props.PropsStoreGateway;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.infra.convertor.game.GiftInfraConvertor;
import com.red.circle.other.infra.convertor.material.PropsMaterialInfraConvertor;
import com.red.circle.other.infra.database.rds.service.gift.GiftBackpackService;
import com.red.circle.other.infra.database.rds.service.gift.GiftConfigService;
import com.red.circle.other.inner.enums.material.GiftCurrencyType;
import com.red.circle.other.inner.model.dto.material.GiftConfigDTO;
import com.red.circle.other.inner.model.dto.material.NewUserRewardDTO;
import com.red.circle.other.inner.model.dto.material.PropsDecorationDTO;
import com.red.circle.other.inner.model.dto.material.props.UserBackpackProps;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author pengliang on 2024/2/28
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UserPropsRewardProcess {

  private final GiftConfigService giftConfigService;
  private final GiftInfraConvertor giftInfraConvertor;
  private final PropsStoreGateway propsStoreGateway;
  private final GiftBackpackService giftBackpackService;
  private final PropsMaterialInfraConvertor propsMaterialInfraConvertor;
  private final UserRegionGateway userRegionGateway;

    public NewUserRewardDTO newUserReward(SysOriginPlatformEnum sysOrigin, Long userId, Long countryId) {

//      if (Objects.equals(sysOrigin.name(), SysOriginPlatformEnum.ASWAT.name())) {
//        return aswatNewUserReward(userId);
//      }
//      Long inCountryId = 1231833304232112130L;
//      if (Objects.equals(inCountryId, countryId)) {
//        log.warn("2countryId {}", countryId);
//        return inSystemNewUserReward(sysOrigin, userId);
//      }
      return otherSystemNewUserReward(sysOrigin, userId);
    }

  /**
   * 20240919 印度用户注册赠送特殊头像框
   * 测试环境：1836697536324743170
   * 生产环境：1836686139155193858
   * @param sysOrigin
   * @param userId
   * @return
   */
  private NewUserRewardDTO inSystemNewUserReward(SysOriginPlatformEnum sysOrigin, Long userId) {
    int days = 7;
    QuantityDTO<GiftConfigDTO> gift = sendGift(sysOrigin, userId);

    UserBackpackProps userPropsBackpack = propsStoreGateway.randomGiveawayCheapestPropsById(userId,
            7, 1836697536324743170L);

    log.warn("resources{}", userPropsBackpack);
    return new NewUserRewardDTO()
            .setGift(gift)
            .setProps(new PropsDecorationDTO()
                    .setPropsResources(
                            propsMaterialInfraConvertor.toPropsResourcesDTO(userPropsBackpack.getPropsResources()))
                    .setDays(days)
                    .setAmount(countAmount(days, userPropsBackpack))
            );
  }

  private NewUserRewardDTO otherSystemNewUserReward(SysOriginPlatformEnum sysOrigin, Long userId) {
    int days = 7;
//    QuantityDTO<GiftConfigDTO> gift = sendGift(sysOrigin, userId);
    UserBackpackProps userPropsBackpack = propsStoreGateway.randomGiveawayCheapestProps(userId, 7);
    return new NewUserRewardDTO()
        .setGift(null)
        .setProps(new PropsDecorationDTO()
            .setPropsResources(
                propsMaterialInfraConvertor.toPropsResourcesDTO(userPropsBackpack.getPropsResources()))
            .setDays(days)
            .setAmount(countAmount(days, userPropsBackpack))
        );
  }

  private BigDecimal countAmount(int days, UserBackpackProps userBackpackProps) {
    return userBackpackProps.getPropsResources().getAmount().multiply(BigDecimal.valueOf(days))
        .setScale(0, RoundingMode.DOWN);
  }

  private QuantityDTO<GiftConfigDTO> sendGift(SysOriginPlatformEnum sysOrigin,
      Long userId) {

    GiftConfigDTO giftConfig = giftInfraConvertor.toGiftConfigDTO(giftConfigService.getCheapest(sysOrigin, GiftCurrencyType.DIAMOND));

    int quantity = 10;
    // 发送到背包
    if (Objects.nonNull(giftConfig)) {
      giftBackpackService.incrGift(userId, giftConfig.getId(), quantity);
    }

    return Objects.isNull(giftConfig) ? null : QuantityDTO.of(giftConfig, quantity);
  }

  private NewUserRewardDTO aswatNewUserReward(Long userId) {

    GiftConfigDTO giftConf = giftInfraConvertor.toGiftConfigDTO(
        giftConfigService.getById(1462965235598667777L));

    if (Objects.nonNull(giftConf)) {
      QuantityDTO<GiftConfigDTO> gift = QuantityDTO.of(giftConf, 5);
      giftBackpackService.incrGift(userId, gift.getContent().getId(), gift.getQuantity());

      return new NewUserRewardDTO()
          .setGift(gift);
    }

    return new NewUserRewardDTO();
  }
}
