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

import com.red.circle.common.business.dto.cmd.LastIdCmd;
import com.red.circle.common.business.dto.cmd.UserIdCmd;
import com.red.circle.framework.core.dto.PageCommand;
import com.red.circle.framework.core.response.ResponseErrorCode;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.framework.web.util.ValidatorUtils;
import com.red.circle.game.fruit.application.dto.cmd.GameIdCmd;
import com.red.circle.game.fruit.application.dto.cmd.TokenQryCmd;
import com.red.circle.game.fruit.application.dto.cmd.UserBetCmd;
import com.red.circle.game.fruit.application.dto.enums.MessageTypeTopic;
import com.red.circle.game.fruit.application.service.GameInfoService;
import com.red.circle.game.fruit.application.service.UserBetService;
import com.red.circle.game.fruit.application.service.UserRankingService;
import com.red.circle.game.fruit.infrastructure.websocket.message.MessageSender;
import com.red.circle.game.fruit.infrastructure.websocket.message.TopicMessage;
import java.security.Principal;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;

/**
 * 获取游戏信息.
 *
 * @author lisizhe on 2023/7/19
 */
@Slf4j
@Controller
@RequiredArgsConstructor
public class GameFruitRestController {

  private final MessageSender messageSender;
  private final UserBetService userBetService;
  private final GameInfoService gameInfoService;
  private final UserRankingService userRankingService;

  /**
   * 进入游戏查询首页数据.
   */
  @MessageMapping("/enter-game")
  public void enterGame(TokenQryCmd cmd, Principal principal) {
    cmd.setUid(Long.valueOf(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.GAME_FRUIT_MSG.getTopicId(cmd.getUid()), ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    gameInfoService.getGameInfo(cmd);
  }

  /**
   * 用户下注.
   */
  @MessageMapping("/user-bet")
  public void userBet(UserBetCmd cmd, Principal principal) {
    cmd.setUid(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.USER_BET_MSG.getTopicId(cmd.getUid()), ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    userBetService.userBet(cmd);
  }

  /**
   * 开奖信息
   */
  @MessageMapping("/draw-lottery")
  public void drawLottery(GameIdCmd gameIdCmd, Principal principal) {
    UserIdCmd cmd = new UserIdCmd();
    cmd.setUserId(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.DRAW_LOTTERY_MSG.getTopicId(cmd.getUserId()), ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    // gameInfoService.drawLottery(Long.parseLong(principal.getName()), gameIdCmd.getGameId());
    gameInfoService.drawLottery(gameIdCmd.getGameId(),Long.parseLong(principal.getName()));
  }


  /**
   * 奖品下注热度
   */
  @MessageMapping("/prize-bet-heat")
  public void prizeBetHeat(Principal principal) {
    UserIdCmd cmd = new UserIdCmd();
    cmd.setUserId(Long.parseLong(principal.getName()));
    gameInfoService.prizeBetHeat(cmd.getUserId());
  }

  /**
   * 每日榜单top10.
   */
  @MessageMapping("/list-top10")
  public void listTop10(Principal principal) {
    UserIdCmd cmd = new UserIdCmd();
    cmd.setUserId(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.USER_RANKING_MSG.getTopicId(cmd.getUserId()), ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    userRankingService.listRankingTop(cmd.getUserId());
  }


  /**
   * 每日榜单top3.
   */
  @MessageMapping("/list-top3")
  public void listTop3(Principal principal) {
    UserIdCmd cmd = new UserIdCmd();
    cmd.setUserId(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.RANKING_TOP3_MSG.getTopicId(cmd.getUserId()), ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    userRankingService.listRankingTop3(cmd.getUserId());
  }

  /**
   * 玩家下注记录.
   */
  @MessageMapping("/players-bet-history")
  public void playersBetHistory(LastIdCmd lastIdCmd, Principal principal) {
    UserIdCmd cmd = new UserIdCmd();
    cmd.setUserId(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.USER_BET_HISTORY_MSG.getTopicId(cmd.getUserId()),
              ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    gameInfoService.playersBetHistory(lastIdCmd.getLastId(), cmd.getUserId());
  }

}
