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

import com.red.circle.common.business.core.ControllerRedisKeyConstant;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.clientobject.family.RoomSettingCO;
import com.red.circle.other.app.dto.clientobject.room.RoomSudCo;
import com.red.circle.other.app.dto.cmd.room.KickOffMicrophoneCmd;
import com.red.circle.other.app.dto.cmd.room.LockedRoomCmd;
import com.red.circle.other.app.dto.cmd.room.UpdateRoomSettingCmd;
import com.red.circle.other.app.dto.cmd.room.UpdateRoomSudCmd;
import com.red.circle.other.app.service.room.RoomSettingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 房间设置 前端控制器.
 * </p>
 *
 * @author houyong on 2020/12/01
 * @eo.api-type http
 * @eo.groupName 直播服务.房间设置
 * @eo.path /room/setting
 * @since pengliang on 2023/9/21
 */
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping(value = "/room/setting", produces = MediaType.APPLICATION_JSON_VALUE)
public class RoomSettingRestController extends BaseController {

  private final RoomSettingService roomSettingService;

  /**
   * 我的房间设置.
   *
   * @eo.name 我的房间设置.
   * @eo.url
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping
  public RoomSettingCO meRoomSettingInfo(AppExtCommand cmd) {
    return roomSettingService.meRoomSettingInfo(cmd);
  }

  /**
   * 获取指定房间设置信息.
   *
   * @eo.name 获取指定房间设置信息.
   * @eo.url /specific
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/specific")
  public RoomSettingCO roomSettingInfo(@Validated AppRoomIdCmd cmd) {
    return roomSettingService.roomSettingInfo(cmd);
  }

  /**
   * 更改房间设置信息.
   *
   * @eo.name 更改房间设置信息.
   * @eo.url
   * @eo.method put
   * @eo.request-type json
   */
  @PutMapping
  public void updateSetting(@RequestBody @Validated UpdateRoomSettingCmd cmd) {
    roomSettingService.updateSetting(cmd);
  }

  /**
   * 房间上锁.
   *
   * @eo.name 房间上锁.
   * @eo.url /locked
   * @eo.method put
   * @eo.request-type json
   */
  @PutMapping("/locked")
  public void lockedRoom(@RequestBody @Validated LockedRoomCmd cmd) {
    roomSettingService.lockedRoom(cmd);
  }

  /**
   * 解锁房间.
   *
   * @eo.name 解锁房间.
   * @eo.url /unlocked
   * @eo.method get
   * @eo.request-type json
   */
  @PostMapping("/unlocked")
  public void unlockedRoom(@RequestBody @Validated AppRoomIdCmd cmd) {
    roomSettingService.unlockedRoom(cmd);
  }

  /**
   * 检测是否可以锁麦克风.
   *
   * @eo.name 检测是否可以锁麦克风.
   * @eo.url /lock-unlock/check
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/lock-unlock/check")
  public Boolean checkMicrophoneOperatingV2(@RequestBody @Validated AppRoomIdCmd cmd) {
    return roomSettingService.checkLockAndUnlock(cmd);
  }

  /**
   * 检测是否可以将用户T下麦克风.
   *
   * @eo.name 检测是否可以将用户T下麦克风.
   * @eo.url /kick_off_microphone
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/kick-off-microphone")
  public Boolean kickOffMicrophone(@RequestBody @Validated KickOffMicrophoneCmd cmd) {
    return roomSettingService.kickOffMicrophone(cmd);
  }



  /**
   * 存储SUD游戏是否开启
   * @param cmd
   * @return
   */
  @Cacheable(value = ControllerRedisKeyConstant.ROOM_SUD_LIST, key = "#cmd.getRoomId()", unless = "#result == null")
  @GetMapping("/getRoomSud")
  public RoomSudCo getRoomSud(AppRoomIdCmd cmd) {
    return roomSettingService.getRoomSud(cmd);
  }


  /**
   * 存储游戏缓存
   * @param cmd
   * @return
   */
  @Caching(evict = {
          @CacheEvict(value = ControllerRedisKeyConstant.ROOM_SUD_LIST, key = "#cmd.getRoomId()")
  })
  @PutMapping("/updateRoomSud")
  public void updateRoomSud(@RequestBody @Validated UpdateRoomSudCmd cmd) {
    roomSettingService.updateRoomSud(cmd);
  }
}
