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

import com.red.circle.common.business.core.ControllerRedisKeyConstant;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.dto.AppCommand;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.framework.web.props.EnvProperties;
import com.red.circle.mq.business.model.event.game.GameSudProperties;
import com.red.circle.other.app.dto.clientobject.sys.AppCountryCodeCO;
import com.red.circle.other.app.dto.clientobject.sys.AppStartPageCO;
import com.red.circle.other.app.dto.clientobject.sys.BannerCO;
import com.red.circle.other.app.dto.clientobject.sys.CountryCodeCO;
import com.red.circle.other.app.dto.clientobject.sys.GameListConfigCO;
import com.red.circle.other.app.dto.clientobject.sys.GameMatchModelConfigGroupCO;
import com.red.circle.other.app.dto.clientobject.sys.NoticeMessageCO;
import com.red.circle.other.app.dto.clientobject.sys.SudCodeCO;
import com.red.circle.other.app.dto.clientobject.game.GameRankingCO;
import com.red.circle.other.app.dto.cmd.game.GameListQryCmd;
import com.red.circle.other.app.dto.cmd.game.GameRankingQry;
import com.red.circle.other.app.service.sys.AppConfigService;
import com.red.circle.other.app.service.game.GameRankingService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserConsumptionLevel;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
import com.red.circle.other.inner.enums.config.EnumConfigKey;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;

import java.util.*;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.sud.mgp.auth.api.SudCode;
import tech.sud.mgp.auth.api.SudMGPAuth;

/**
 * app配置 接口.
 *
 * @author pengliang on 2021/1/22
 * @eo.api-type http
 * @eo.groupName 系统服务.App配置
 * @eo.path /sys/config
 */
@Slf4j
@RestController
@RequestMapping(value = "/sys/config", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class AppConfigRestController extends BaseController {

  private final EnvProperties evnProperties;
  private final AppConfigService appConfigService;
  private final GameSudProperties gameSudProperties;
  private final UserProfileGateway userProfileGateway;
  private final EnumConfigCacheService enumConfigCacheService;
  private final RedisTemplate<String,Object> redisTemplate;
  private final GameRankingService gameRankingService;

  /**
   * 房间内-快捷入口-游戏.
   */
  @GetMapping("/room/shortcut-game")
  public GameListConfigCO getRoomShortcutGame(GameListQryCmd cmd) {

    String gameIdStr = enumConfigCacheService.getValue(EnumConfigKey.ROOM_SHORTCUT_GAME_ID,
        cmd.requireReqSysOrigin());
    if (StringUtils.isBlank(gameIdStr)) {
      return null;
    }
    try {
      Long gameId = Long.parseLong(gameIdStr);
      List<GameListConfigCO> gameListConfigs = appConfigService.listGameConfig(cmd);
      if (CollectionUtils.isEmpty(gameListConfigs)) {
        return null;
      }
      gameListConfigs = gameListConfigs.stream().filter(v -> Objects.equals(v.getId(), gameId))
          .collect(Collectors.toList());
      if (CollectionUtils.isEmpty(gameListConfigs)) {
        return null;
      }
      return gameListConfigs.get(0);
    } catch (Exception ex) {
      return null;
    }
  }

  /**
   * 房间内-游戏列表配置.
   *
   * @eo.name 房间内-游戏列表配置.
   * @eo.url /list-game-config
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list-game-config")
  public List<GameListConfigCO> listGameConfig(GameListQryCmd cmd, @RequestParam(required = false) String roomId) {
      cmd.setRoomSessionId(roomId);
      return appConfigService.listGameConfig(cmd);
  }

  /**
   * 房间内-sud游戏.
   */
  @GetMapping("/room/getSudCode")
  public SudCodeCO getRoomGameSud(AppExtCommand cmd) {
    SudMGPAuth sudMGPAuth = new SudMGPAuth(gameSudProperties.getAppId(), gameSudProperties.getAppSecret());
    //过期时间毫秒级，24小时
    SudCode sudCode= sudMGPAuth.getCode(cmd.getReqUserId().toString(),86400000L);
    return new SudCodeCO().setCode(sudCode.getCode()).setExpireDate(sudCode.getExpireDate());
  }

  /**
   * 匹配-游戏列表配置.
   *
   * @eo.name 匹配-游戏列表配置.
   * @eo.url /match/game-config
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/match/game-config")
  public GameMatchModelConfigGroupCO getMatchGameConfig(AppExtCommand cmd) {
    return appConfigService.getMatchGameConfig(cmd);
  }

  /**
   * 推荐国家列表.
   *
   * @eo.name 推荐国家列表.
   * @eo.url /country/top-six
   * @eo.method get
   * @eo.request-type formdata
   */
  @Cacheable(value = ControllerRedisKeyConstant.COUNTRY, key = "'"
      + ControllerRedisKeyConstant.COUNTRY_TOP_SIX
      + "'", unless = "#result == null || #result.isEmpty()")
  @GetMapping("/country/top-six")
  public List<CountryCodeCO> openTopSix() {
    return appConfigService.listOpenTopSix();
  }


  /**
   * 国家列表.
   *
   * @eo.name 国家列表.
   * @eo.url /country
   * @eo.method get
   * @eo.request-type formdata
   */
  @Cacheable(value = ControllerRedisKeyConstant.COUNTRY, key = "'"
      + ControllerRedisKeyConstant.APP_COUNTRY_CODE + "'", unless = "#result == null")
  @GetMapping("/country")
  public AppCountryCodeCO appCountryCode() {
    return appConfigService.getAppCountryCode();
  }

  /**
   * 获得客服.
   *
   * @eo.name 获得客服.
   * @eo.url /customer-service
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/customer-service")
  public UserProfileDTO getCustomerService(AppExtCommand cmd) {
    return appConfigService.getCustomerService(cmd);
  }

  /**
   * 获取平台banner.
   *
   * @eo.name 获取平台banner.
   * @eo.url /banner
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/banner")
  public List<BannerCO> listBanner(AppExtCommand cmd, @RequestParam(required = false) String bannerType) {
      return appConfigService.listBanner(cmd, bannerType);
  }

  /**
   * app启动页.
   *
   * @eo.name app启动页.
   * @eo.url /banner/start-page
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/banner/start-page")
  public List<AppStartPageCO> getAppPageStart(AppExtCommand cmd) {
    return appConfigService.listAppPageStart(cmd);
  }

  /**
   * 系统参数配置列表.
   *
   * @eo.name 系统参数配置列表.
   * @eo.url /enum/config
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/enum/config")
  public Map<String, String> listSysEnumConfig(AppCommand cmd) {
    return appConfigService.listSysEnumConfig(cmd);
  }

  /**
   * 最近活动通知列表.
   *
   * @eo.name 最近活动通知列表.
   * @eo.url /sys/notice-message
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/sys/notice-message")
  public List<NoticeMessageCO> listLastOneMonth(AppExtCommand cmd) {
    return appConfigService.listNoticeMessageLastMonth(cmd);
  }

  /**
   * 热门游戏排行榜.
   */
  @GetMapping("/game/ranking")
  public PageResult<GameRankingCO> gameRanking(GameRankingQry qry) {
    return gameRankingService.pageRanking(qry);
  }

}
