package com.red.circle.other.adapter.app.game;

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.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.clientobject.game.GameReceiveRedPacketCO;
import com.red.circle.other.app.dto.clientobject.game.GameRedPacketConfigCO;
import com.red.circle.other.app.dto.clientobject.game.GameRedPacketConfigV2CO;
import com.red.circle.other.app.dto.clientobject.game.GameRedPacketSenderUserCO;
import com.red.circle.other.app.dto.clientobject.game.SendRedPacketCO;
import com.red.circle.other.app.dto.cmd.game.SendRedPacketCmd;
import com.red.circle.other.app.service.game.GameRedPacketService;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.domain.model.user.ability.RegionConfig;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.enums.config.EnumConfigKey;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 红包游戏.
 *
 * @author lisizhe on 2023/9/25
 * @eo.api-type http
 * @eo.groupName 游戏服务.红包游戏
 * @eo.path /game/red-packet
 */
@Slf4j
@RestController
@RequestMapping(value = "/game/red-packet", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class GameRedPacketRestController extends BaseController {

  private final UserRegionGateway userRegionGateway;
  private final GameRedPacketService gameRedPacketService;
  private final EnumConfigCacheService enumConfigCacheService;

  /**
   * 参数配置信息.
   *
   * @eo.name 参数配置信息
   * @eo.url /config
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/config")
  public GameRedPacketConfigCO config(AppExtCommand cmd) {

    var redPacketConfig = new GameRedPacketConfigCO()
        .setPeopleNumbers(Arrays.asList(10, 20, 40, 60));

    RegionConfig regionConfig = userRegionGateway.getRegionConfigByUserId(cmd.requiredReqUserId());

    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND, regionConfig.getLangeCodes());

    if (regionConfig.getLangeCodes().contains("tr")) {
      // 阿拉伯，土耳其地区特殊处理
      redPacketConfig.setAmounts(Arrays.asList(
          BigDecimal.valueOf(1000),
          BigDecimal.valueOf(5000),
          BigDecimal.valueOf(20000),
          BigDecimal.valueOf(50000)
      ));
      return redPacketConfig;
    }

    redPacketConfig.setAmounts(Arrays.asList(
        BigDecimal.valueOf(500),
        BigDecimal.valueOf(2000),
        BigDecimal.valueOf(6000),
        BigDecimal.valueOf(9000)
    ));
    return redPacketConfig;
  }

  /**
   * 参数配置信息2.0.
   *
   * @eo.name 参数配置信息2.0
   * @eo.url /config/v2
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/config/v2")
  public GameRedPacketConfigV2CO configV2(AppExtCommand cmd) {

    if (!Objects.equals(cmd.requireReqSysOriginChildEnum(), SysOriginPlatformEnum.ASWAT)) {
      return new GameRedPacketConfigV2CO();
    }

    HashMap<String, List<String>> worldRedPacketInfo = new HashMap<>();
    HashMap<String, List<String>> countryRedPacketInfo = new HashMap<>();

    List<String> redPacketNumber = Optional.ofNullable(
            enumConfigCacheService.getValue(
                EnumConfigKey.ASWAT_RED_PACKET_NUMBER, cmd.requireReqSysOrigin())
        ).map(data -> Arrays.asList(data.split(",")))
        .orElse(Lists.newArrayList());

    List<String> worldRedPacketAmount = Optional.ofNullable(
            enumConfigCacheService.getValue(
                EnumConfigKey.ASWAT_WORLD_RED_PACKET_AMOUNT, cmd.requireReqSysOrigin())
        ).map(data -> Arrays.asList(data.split(",")))
        .orElse(Lists.newArrayList());

    worldRedPacketInfo.put("redPacketNumber", redPacketNumber);
    worldRedPacketInfo.put("redPacketAmount", worldRedPacketAmount);
    countryRedPacketInfo.put("redPacketNumber", redPacketNumber);

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

    if (Objects.equals(regionCode, "TR") || Objects.equals(regionCode, "AR")) {
      List<String> countryRedPacketAmount = Optional.ofNullable(
              enumConfigCacheService.getValue(
                  EnumConfigKey.ASWAT_COUNTRY_RED_PACKET_AMOUNT, cmd.requireReqSysOrigin())
          )
          .map(data -> Arrays.asList(data.split(","))).orElse(Lists.newArrayList());
      countryRedPacketInfo.put("redPacketAmount", countryRedPacketAmount);
    } else {
      List<String> otherCountryRedPacketAmount = Optional.ofNullable(
              enumConfigCacheService.getValue(
                  EnumConfigKey.ASWAT_OTHER_COUNTRY_RED_PACKET_AMOUNT, cmd.requireReqSysOrigin())
          )
          .map(data -> Arrays.asList(data.split(","))).orElse(Lists.newArrayList());
      countryRedPacketInfo.put("redPacketAmount", otherCountryRedPacketAmount);
    }
    return new GameRedPacketConfigV2CO()
        .setWorldRedPacketInfo(worldRedPacketInfo)
        .setCountryRedPacketInfo(countryRedPacketInfo);
  }

  /**
   * 发送红包.
   *
   * @eo.name 发送红包
   * @eo.url /send
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/send")
  public SendRedPacketCO sendRedPacketV2(@RequestBody @Validated SendRedPacketCmd cmd) {
    return gameRedPacketService.sendRedPacket(cmd);
  }

  /**
   * 获取红包发送人信息-新.
   *
   * @eo.name 获取红包发送人信息
   * @eo.url /sender
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/sender")
  public GameRedPacketSenderUserCO sender(@Validated AppIdLongCmd cmd) {
    return gameRedPacketService.sender(cmd);
  }

  /**
   * 领取红包 (2021.10.9之后只有领取红包大于1k的用户才会默认关注发送红包用户).
   *
   * @eo.name 领取红包
   * @eo.url /receive
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/receive")
  public GameReceiveRedPacketCO receiveRedPacket(@RequestBody @Validated AppIdLongCmd cmd) {
    return gameRedPacketService.receiveRedPacket(cmd);
  }

}
