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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.game.GameLudoRunCO;
import com.red.circle.other.app.dto.cmd.game.GameLudoCreateCmd;
import com.red.circle.other.app.dto.cmd.game.GameLudoEndCmd;
import com.red.circle.other.app.dto.cmd.game.GameLudoJoinCmd;
import com.red.circle.other.app.dto.cmd.game.GameLudoStartCmd;
import com.red.circle.other.app.dto.clientobject.game.GamePlayHistoryCO;
import com.red.circle.other.app.dto.cmd.game.GamePlayHistoryRecordCmd;
import com.red.circle.other.app.service.game.GameLudoService;
import com.red.circle.other.app.service.game.GamePlayHistoryService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Arrays;
import java.util.List;
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.PathVariable;
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;

/**
 * ludo游戏.
 *
 * @author lisizhe on 2023/9/19
 * @eo.api-type http
 * @eo.groupName 游戏服务.Ludo游戏
 * @eo.path /game/ludo
 */
@Slf4j
@RestController
@RequestMapping(value = "/game/ludo", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class GameLudoAppRestController {

  private final GameLudoService gameLudoService;
  private final GamePlayHistoryService gamePlayHistoryService;
  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;

  /**
   * 获取游戏信息.
   *
   * @eo.name 获取游戏信息
   * @eo.url /price
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/price")
  public List<Integer> listPrice() {
    return Arrays.asList(0, 50, 100, 200, 300, 500);
  }

  /**
   * 获取房间运行ludo游戏.
   *
   * @eo.name 获取房间运行ludo游戏
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping
  public GameLudoRunCO getRoomGame(@Validated AppRoomIdCmd cmd) {
    return gameLudoService.getRoomGame(cmd);
  }

  /**
   * 使用/room_voice/run-game 代替
   * <p>
   * 房间是否存在游戏.
   *
   * @eo.name 房间是否存在游戏
   * @eo.url /exists
   * @eo.method get
   * @eo.request-type formdata
   */
//  @Deprecated
//  @GetMapping("/exists")
  public Boolean roomExistsGame(@Validated AppRoomIdCmd cmd) {
    return gameLudoService.roomExistsGame(cmd);
  }

  /**
   * 创建游戏.
   *
   * @eo.name 创建游戏
   * @eo.url /create
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/create")
  public GameLudoRunCO create(@RequestBody @Validated GameLudoCreateCmd cmd) {
    return gameLudoService.create(cmd);
  }

  /**
   * 开始游戏.
   *
   * @eo.name 开始游戏
   * @eo.url /start
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/start")
  public void changeStatus(@RequestBody @Validated GameLudoStartCmd cmd) {
    gameLudoService.start(cmd);
  }

  /**
   * 加入游戏.
   *
   * @eo.name 加入游戏
   * @eo.url /join
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/join")
  public void join(@RequestBody @Validated GameLudoJoinCmd cmd) {
    gameLudoService.join(cmd);
  }

  /**
   * 主动解散游戏.
   *
   * @eo.name 主动解散游戏
   * @eo.url /disband
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/disband")
  public void disband(@RequestBody @Validated AppIdLongCmd cmd) {
    gameLudoService.disband(cmd);
  }

  /**
   * 结束游戏.
   *
   * @eo.name 结束游戏
   * @eo.url /end
   * @eo.method post
   * @eo.request-type json
   */
//  @PostMapping("/end")
  public void end(@RequestBody @Validated GameLudoEndCmd cmd) {
    gameLudoService.end(cmd);
  }

  /**
   * 用户资料(临时兼容).
   *
   * @eo.name 用户资料(临时兼容)
   * @eo.url /user-profile/{userId}
   * @eo.method post
   * @eo.request-type json
   */
  @GetMapping("/user-profile/{userId}")
  public UserProfileDTO getProfile(@PathVariable("userId") Long userId) {
    return userProfileAppConvertor.toUserProfileDTO(
        userProfileGateway.getByUserId(userId)
    );
  }

  /**
   * 记录游玩历史.
   *
   * @eo.name 记录游玩历史
   * @eo.url /history/record
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/history/record")
  public void recordHistory(@RequestBody @Validated GamePlayHistoryRecordCmd cmd) {
    gamePlayHistoryService.record(cmd);
  }

  /**
   * 查询最近游玩历史（最多5条）.
   *
   * @eo.name 查询最近游玩历史
   * @eo.url /history/recent
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/history/recent")
  public List<GamePlayHistoryCO> listRecentHistory(@Validated AppExtCommand cmd) {
    return gamePlayHistoryService.listRecent(cmd.requireReqSysOrigin(), cmd.requiredReqUserId());
  }

}
