package com.ym.controller;

import com.alibaba.fastjson.JSONObject;
import com.ym.mec.common.entity.ImRoomMessage;
import com.ym.pojo.IMApiResultInfo;
import com.ym.pojo.RecordNotify;
import com.ym.service.LiveRoomService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 hgw
 * Created by 2022-02-21
 */
@RestController
@RequestMapping("/liveRoom")
@Slf4j
public class LiveRoomController {

    @Autowired
    private LiveRoomService liveRoomService;

    @ApiOperation("创建直播房间")
    @PostMapping(value = "/create")
    public IMApiResultInfo createLiveRoom(String roomId, String roomName) throws Exception {
        return liveRoomService.createLiveRoom(roomId, roomName);
    }

    @ApiOperation("销毁直播房间")
    @PostMapping(value = "/destroy")
    public IMApiResultInfo destroyLiveRoom(String roomId) throws Exception {
        return liveRoomService.destroyLiveRoom(roomId);
    }

    @ApiOperation("向房间发送消息")
    @PostMapping(value = "/publishRoomMsg")
    public IMApiResultInfo publishRoomTextMsg(@RequestBody ImRoomMessage message) {
        return liveRoomService.publishRoomMessage(message);
    }

    @ApiOperation("录制结果回调")
    @RequestMapping(value = "/recordSync")
    public void recordSync(@RequestBody String body) {
        log.info("recordSync body：{}", body);
        RecordNotify recordNotify = JSONObject.parseObject(body, RecordNotify.class);
        liveRoomService.recordSync(recordNotify);
    }

    @ApiOperation("录制直播")
    @RequestMapping(value = "/startRecord")
    public void startRecord(String roomId, String videoResolution) throws Exception {
        liveRoomService.startRecord(roomId, videoResolution);
    }

    @ApiOperation("结束录制直播")
    @RequestMapping(value = "/stopRecord")
    public void stopRecord(String roomId) throws Exception {
        liveRoomService.stopRecord(roomId);
    }

    @ApiOperation("查询用户是否在聊天室")
    @RequestMapping(value = "/userExistInRoom")
    public boolean userExistInRoom(String chatroomId, String userId) {
        return liveRoomService.userExistInRoom(chatroomId, userId);
    }

    @ApiOperation("查询用户是否在聊天室")
    @PostMapping(value = "/checkOnline")
    public boolean checkOnline(String userId) {
        return liveRoomService.checkOnline(userId);
    }

    @ApiOperation("添加禁言成员-默认禁言120分钟")
    @PostMapping(value = "/addUserUnableSpeak")
    public boolean addUserUnableSpeak(String roomUid, String userId) {
        return liveRoomService.addUserUnableSpeak(roomUid, userId);
    }

    @ApiOperation("移除禁言成员")
    @PostMapping(value = "/removeUserUnableSpeak")
    public boolean removeUserUnableSpeak(String roomUid, String userId) {
        return liveRoomService.removeUserUnableSpeak(roomUid, userId);
    }

    /**
     * https://doc.rongcloud.cn/imserver/server/v1/chatroom/status
     */
    @ApiOperation("聊天室状态同步")
    @RequestMapping(value = "/syncChatRoomStatus")
    public void syncChatRoomStatus(@RequestBody String body) {
        log.info("syncChatRoomStatus body：{}", body);
    }

}
