package com.red.circle.live.adapter.app;

import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.live.app.dto.clientobject.LiveMicrophoneCO;
import com.red.circle.live.app.dto.cmd.*;
import com.red.circle.live.app.listener.agora.callback.AgoraChannelWebhookFactory;
import com.red.circle.live.app.service.LiveMicrophoneService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 直播麦克风服务.
 *
 * @author pengliang on 2023/12/11
 * @eo.api-type http
 * @eo.groupName 直播服务.麦克风管理
 * @eo.path /live/mic
 */
@Slf4j
@Validated
@RestController
@RequestMapping(value = "/mic", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class LiveMicRestController extends BaseController {

  private final LiveMicrophoneService liveMicrophoneService;
  private final AgoraChannelWebhookFactory agoraChannelWebhookFactory;

  /**
   * 初始化麦克风.
   *
   * @eo.name 初始化麦克风.
   * @eo.url /init
   * @eo.method post
   * @eo.request-type json
   */
  @GetMapping("/init")
  public void init(@Validated AppRoomIdCmd cmd) {
    liveMicrophoneService.init(cmd);
  }

  /**
   * 上麦克风.
   *
   * @eo.name 上麦克风.
   * @eo.url /go-up
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/go-up")
  public LiveMicrophoneCO goUp(@Validated @RequestBody MicUpDownCmd cmd) {
    return liveMicrophoneService.goUp(cmd);
  }


  /**
   * 上麦克风.
   *
   * @eo.name 上麦克风.
   * @eo.url /go-up
   * @eo.method post
   * @eo.request-type json
   */
//  @GetMapping("/getInfo")
//  public void getBlackInfo(String account) {
//    liveMicrophoneService.bannedLiveUser(account);
//  }

  /**
   * 处理幽灵麦用户信息.
   *
   * @eo.name 处理幽灵麦用户信息.
   * @eo.url /shut-up
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/shut-up")
  public void shutUp(@Validated @RequestBody MicBlockCmd cmd) {
    liveMicrophoneService.bannedLiveUser(cmd);
  }

  /**
   * 下麦克风.
   *
   * @eo.name 下麦克风.
   * @eo.url /go-down
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/go-down")
  public void goDown(@Validated @RequestBody MicUpDownCmd cmd) {
    liveMicrophoneService.goDown(cmd);
  }

  /**
   * T麦克风（房主&管理员）.
   *
   * @eo.name T麦克风（房主&管理员）.
   * @eo.url /kill
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/kill")
  public void kill(@Validated @RequestBody MicKillCmd cmd) {
    liveMicrophoneService.kill(cmd);
  }

  /**
   * 获取麦克风列表.
   *
   * @eo.name 获取麦克风列表.
   * @eo.url /list
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list")
  public List<LiveMicrophoneCO> listMic(@Validated AppRoomIdCmd cmd) {
    return liveMicrophoneService.listMic(cmd);
  }

  /**
   * 麦克风锁.
   *
   * @eo.name 麦克风锁.
   * @eo.url /lock
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/lock")
  public void lock(@RequestBody @Validated MicLockCmd cmd) {
    liveMicrophoneService.lock(cmd);
  }

  /**
   * 麦克风静音.
   *
   * @eo.name 麦克风静音.
   * @eo.url /mute
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/mute")
  public void mute(@RequestBody @Validated MicMuteCmd cmd) {
    liveMicrophoneService.mute(cmd);
  }


  /**
   * <a
   * href="https://doc.shengwang.cn/api-ref/rtc/restful/channel-management/ncs-events#eventtype">声网麦克风回调</a>.
   *
   * @eo.hidden
   */
  @PostMapping("/agora/callback")
  public void agoraCallback(@RequestBody byte[] body) {
    agoraChannelWebhookFactory.execute(body);
  }

}
