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

import com.red.circle.common.business.dto.cmd.UserIdCmd;
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.GameFruitOpenBoxCmd;
import com.red.circle.game.fruit.application.dto.cmd.GameFruitReceiveTaskAwardCmd;
import com.red.circle.game.fruit.application.dto.enums.MessageTypeTopic;
import com.red.circle.game.fruit.application.service.GameFruitTaskService;
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 zongpubin on 2023/12/11
 */
@Slf4j
@Controller
@RequiredArgsConstructor
public class GameFruitTaskRestController {

  private final MessageSender messageSender;
  private final GameFruitTaskService gameFruitTaskService;

  /**
   * 宝箱奖励数据.
   */
  @MessageMapping("/fruit-box-reward")
  public void boxRewards(Principal principal) {
    UserIdCmd cmd = new UserIdCmd();
    cmd.setUserId(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.GAME_FRUIT_BOX_REWARD_MSG.getTopicId(cmd.getUserId()),
              ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    gameFruitTaskService.boxRewards(cmd.getUserId());
  }

  /**
   * 开宝箱.
   */
  @MessageMapping("/fruit-open-box")
  public void openBox(GameFruitOpenBoxCmd cmd, Principal principal) {
    cmd.setUid(Long.valueOf(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.GAME_FRUIT_OPEN_BOX_REWARD_MSG.getTopicId(cmd.getUid()),
              ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    gameFruitTaskService.openBox(cmd);
  }

  /**
   * 用户玩今天玩游戏次数.
   */
  @MessageMapping("/fruit-game-times")
  public void gameTimes(Principal principal) {
    UserIdCmd cmd = new UserIdCmd();
    cmd.setUserId(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.USER_GAME_FRUIT_TIMES_MSG.getTopicId(cmd.getUserId()),
              ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    gameFruitTaskService.gameTimes(cmd.getUserId());
  }

  /**
   * 任务资源信息.
   */
  @MessageMapping("/fruit-game-task-info")
  public void gameTaskInfo(Principal principal) {
    UserIdCmd cmd = new UserIdCmd();
    cmd.setUserId(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.GAME_FRUIT_TASK_INFO_MSG.getTopicId(cmd.getUserId()),
              ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    gameFruitTaskService.gameTaskInfo(cmd.getUserId());
  }

  /**
   * 领取了奖励的人-limit20.
   */
  @MessageMapping("/fruit-game-receiver-user")
  public void gameFruitReceiveUser(Principal principal) {
    UserIdCmd cmd = new UserIdCmd();
    cmd.setUserId(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.GAME_FRUIT_RECEIVE_USER_INFO_MSG.getTopicId(cmd.getUserId()),
              ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    gameFruitTaskService.gameFruitReceiveUser(cmd.getUserId());
  }


  /**
   * 领取任务奖励
   */
  @MessageMapping("/fruit-game-receiver-task-award")
  public void gameFruitReceiveTaskAward(GameFruitReceiveTaskAwardCmd cmd, Principal principal) {
    cmd.setUid(Long.parseLong(principal.getName()));
    if (!ValidatorUtils.validateFastPass(cmd)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.GAME_FRUIT_RECEIVE_TASK_AWARD_MSG.getTopicId(cmd.getUid()),
              ResultResponse.failure(
                  ResponseErrorCode.REQUEST_PARAMETER_ERROR)));
      return;
    }
    gameFruitTaskService.gameFruitReceiveTaskAward(cmd);
  }

}
